Parameter passing in C + + function call-3

Source: Internet
Author: User

Parameter passing is an argument to the formal parameter to pass information, so that the formal parameters to obtain corresponding storage space and initial value, C + + function transfer is mainly 3 ways:

1, by value delivery.

The process of parameter passing by value is: First, the value of the argument is computed, then the corresponding shape parametric is allocated storage space, the space size equals the length of the parameter type, and then the actual parameter value is stored in the storage space allocated for the parameter, which is the initial value of the formal parameter, which is used when the function is called.The function called in this way has no effect on the value change of the argument,That is, even if the value of the formal parameter is changed in the function, it does not have any effect on the argument value, and the argument value remains the value before the call. The reason is that the actual parameters and the parameters of the separate storage units, each other between the non-impact.

To pass an example by value:
#include <iostream>
using namespace Std;
void swap (int a,int b);
int main (void)
{
int a=1,b=2;
cout<< "a=" <<a<< ", b=" <<b<<endl;
Swap (A, b);
cout<< "a=" <<a<< ", b=" <<b<<endl;
return 0;
}
void swap (int a,int b)
{
int c=a;
A=b;
B=c;

}

The result of the operation is:

a=1,b=2

a=1,b=2

Note that although both the formal parameter and the argument are a B, they are assigned to different storage spaces, which involves a scope problem.

Summary: (1) only when the function is called, the parameter is allocated memory space and given the initial value, when the function call ends, the memory space of the formal parameter is freed, and the function returns the value (if any);

(2) The parameter argument types must be identical when calling the function, the int type corresponds to the int type, the double type corresponds to the double type, and so on, otherwise the program will make an error;

(3) The Order of evaluation of function parameters in C + + is passed from right to left, for example:

#include <iostream>
using namespace Std;
int sum (int x,int y);
int main (void)
{
int A, B;
a=1;b=2;
Cout<<sum (++a,a+b) <<endl;
a=1;b=2;
Cout<<sum (a+b,++a) <<endl;
return 0;
}
int sum (int x,int y)
{
return x+y;
}

The result of the operation is:

5

6

2, and address delivery.

When defining a function, if the parameter value is defined as a pointer variable, the argument should be an address when the function is called, which is the delivery of the address. Unlike pass-by-value delivery, address delivery is the passing of the stored address of the argument to the corresponding parameter, so that the pointer to the parameter pointer and the argument to the same address, so the change in the contents of the address that the parameter pointer points to in the called function will change the contents of the argument.

Address Transfer Example:

#include <iostream>
using namespace Std;
void swap (int *,int *);
int main (void)
{
int a=1,b=2;
cout<< "a=" <<a<< ", b=" <<b<<endl;
Swap (&AMP;A,&AMP;B);
cout<< "a=" <<a<< ", b=" <<b<<endl;
return 0;
}
void swap (int *,int *)
{
int c=*a;
*a=*b;
*b=c;
}

Operation Result:

a=1,b=2

A=2,b=1

Note: The arguments and parameters at this point still occupy different storage units, except that their pointers point to the same address.

3. Reference delivery

A reference is a new feature that C + + provides, and a reference can be seen as a variable or an object alias, and when a reference is made, the program initializes it with the name of a variable or object so that the reference can be used as an alias for that variable or object. So the change to the reference is the change to the variable or the object itself. The syntax format for the reference is as follows:

Variable type & reference name = variable name;

For example, define a reference to an integer variable:

int A;

int& Refa=a; \\refa as an alias of a

refa=10;

a-=40;

cout<<a<<endl;

The output is-30. At this time the system does not allocate storage space for REFA, it and a represent the same memory space, the change of Refa is to change a. Examples of establishing and using references:

#include <iostream>
using namespace Std;
int main (void)
{
int a=3,b=2;
int &a=refa,&b=refb;
refa=a+2;
b=refa+1;
cout<<refa<< "" <<a<<endl;
cout<<refb<< "" <<b<<endl;
Refa=b;
cout<<refa<< "" <<a<<endl;
return 0;
}
The result of the operation is:

5 5

3 3

3 3

Note: (1) The reference must be initialized at the time of definition and must specify which variable or object is the alias, and the type corresponds to the same;

(2) A reference is simply an alias of a variable or object, and the system does not allocate memory space for it;

(3) Once a reference is established, it cannot be changed, that is, it has been set up with the variable or object, and cannot be used as an alias for other variables or objects.

Pass-by-value easy to understand, but changing the parameters can not affect the actual parameters, although address delivery is possible, but multiple pointers in the function of the indirect access can make the program difficult to read, and easy to produce errors, and the reference perfectly solves the above two problems. Reference passing is a "&" in front of the parameter when the function is defined.

Reference Delivery Example:

#include <iostream>
using namespace Std;
void swap (int &,int &);
int main (void)
{
int a=1,b=2;
cout<< "a=" <<a<< ", b=" <<b<<endl;
Swap (A, b);
cout<< "a=" <<a<< ", b=" <<b<<endl;
return 0;
}
void swap (int &,int &)
{
int c=a;
A=b;
B=c;
}

These are the three ways to pass a C + + function call.

Parameter passing when a C + + function is called

Parameter passing in C + + function call-3

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.