C + + function call and function return value

Source: Internet
Author: User

function arguments and function calls

1, function call the role:

• Transfer data to formal parameters using real parameters;

• Allocating storage space for data parameters and variables declared in the function body;

• Interrupt the current (call) function, and start the process to the entry of the called function and execute the called function.

When the parameter table is empty, it means that the function does not accept data from the calling function.

2, numerical parameters

Pass-value invocation: The parameter is allocated to the memory area only when the function is called, and remains until the function is returned. Formal parameters and arguments are just the same names, each possessing their own memory space and life cycle, so he does not change the variables in the main function.

3, Address parameters

Address calls: The addresses of the variables are transmitted, although the main function and the called functions operate on their own variables, but their addresses are the same, so the modulated function can change the value of the variable in the calling function.

function that returns a pointer

In C, the return type of a function can be any type other than a divisor group and a function, including data that returns a pointer type.

Example 1://the original function of a specified character in a lookup string

extern char *strchr(char *str,char character)
{
while(*str!=character)
str++;
return(str);
}
··

Attention... Unlike an address call, a function that returns a pointer returns data to the calling function in the form of a pass value. Therefore, you cannot use the address of an automatic variable as the return value because such a variable will be revoked upon return. Global variables do not need to return addresses. Therefore, the returned address variable should be local variable or dynamic.

Example 2://wrong get is garbled

char *strchr(char *str,char character)
{
char* substr;
substr[0]=str[0];
return substr;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.