How function parameters are passed

Source: Internet
Author: User

1. Use variable names as formal parameters and arguments

In this case, the value of the variable is passed to the parameter. The pass is unidirectional, that is, if the value of the parameter changes during the execution of the function and does not pass back to the argument, this is how the value is passed. Because the formal parameters and arguments are not the same storage unit during the call to a function.

int main () {

void swap (Int,int);//parameter is an integer variable

int i=3,j=4;

cout<< "i=" <<i<< ", j=" <<j<<endl;

Swap (I,J);//variable name

cout<< "i=" <<i<< ", j=" <<j<<endl;

System ("PAUSE");

return 0;

}

void swap (int a,int b) {//parameter is an integer variable

int temp;

Temp=a;

A=b;

B=temp;

}

Results:

I=3,j=4

I=3,j=4

It can be found that the changes of parameters A and B do not affect the values of arguments I and J after the function swap is performed.

2. Passing Variable pointers

A parameter is a pointer variable, an argument is the address of a variable, and a parameter (pointer variable) points to a real parametric cell when the function is called.

int main () {

void swap (int*,int*);//parameter is an integer pointer variable

int i=3,j=4;

cout<< "i=" <<i<< ", j=" <<j<<endl;

Swap (&I,&J);//variable address

cout<< "i=" <<i<< ", j=" <<j<<endl;

System ("PAUSE");

return 0;

}

void swap (int *p1,int *p2) {//parameter is an integer pointer variable

int temp;

TEMP=*P1;

*P1=*P2;

*p2=temp;

}

Results:

I=3,j=4

I=4,j=3

When the function is called, the addresses of the variables I and J are transmitted to the parameters P1 and P2, so *p1 and I are the same memory unit, *P2 and J are the same inner deposit cells.

This is also a "value pass", except that the value of the argument is the address of the variable. Instead of changing the value of the argument in the function (that is, the address, which does not affect the argument), it is the value of the variable to which the argument address points.

3. "Reference parameter"

int main () {

void swap (int&,int&);//argument is a reference to an integer variable

int i=3,j=4;

cout<< "i=" <<i<< ", j=" <<j<<endl;

Swap (I,J);//variable name

cout<< "i=" <<i<< ", j=" <<j<<endl;

System ("PAUSE");

return 0;

}

void swap (int &a,int &b) {//parameter is a reference type

int temp;

Temp=a;

A=b;

B=temp;

}

Results:

I=3,j=4

I=4,j=3

When the main function calls the SWAP function, the variable name is passed to the formal parameter by the argument. I pass the name of the reference variable A,J to the reference variable B. At this point A and B occupy the same memory space separately from the i,j. This passes the address of the actual parameter to the formal parameter, so that the address of the formal parameter takes the address of the actual parameter, so that the form participates in the actual parameter sharing the same unit way, is the address delivery mode.

It is to be explained here that

[1] Mode 2 passes the pointer variable to separate the memory unit, its content is the address, and the mode 3 reference is not an independent variable, not exclusive to the inner deposit element.

[2] in mode 3, when the main function calls the SWAP function, the argument does not have to use the address of the function (that is, &i,&j), but directly using the variable name. The system passes the address of the argument to the parameter instead of the value of the argument.

How function parameters are passed

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.