"Finishing"--c++ three kinds of parameter passing way

Source: Internet
Author: User

In C + +, there are three ways to pass parameters:

    • Passed by value (pass by value)
    • Address delivery (pass by pointer)
    • Reference Pass (pass by reference)

(1) The process of passing by value is: First calculate the value of the actual parameter expression, and then assign a storage space to the corresponding shape parametric, the space is equal to the parameter type, and then the value one by one of the argument expression is saved into the storage space of the shape parametric allocation, and becomes the initial value of the parameter variable. Used by the called function when it executes. This transfer is to pass the value of the argument expression to the corresponding parameter variable, so the transfer mode is called "Passing by value".

In this way, the calling function does not operate on the argument, that is, even if the value of the parameter changes in the function, the value of the argument is not affected at all, and is still the value before the call.

Example:

/*
Pass by value
*/
#include <iostream>
using namespace Std;
void swap (Int,int);
int main ()
{
int a = 3, b = 4;
cout << "a =" << a << ", B ="
<< b << Endl;
Swap (A, b);
cout << "a =" << a << ", B ="
<< b << Endl;
return 0;
}
void swap (int x, int y)
{
int t = x;
x = y;
y = t;
}

Output:

A=3,b=4

A=3,b=4

(2) If a formal parameter is described as a pointer when the function is defined, a call to such a function requires an argument in the form of an address value. The way to pass this argument is to pass the address.

The difference between address delivery and by-value is that it passes the stored address of the argument to the corresponding parameter, so that the parameter pointer and the argument pointer point to the same address. Therefore, any change in the contents of the address that the parameter pointer points to in the called function will affect the argument.

#include <iostream>
using namespace Std;
void swap (int*,int*);
int main ()
{
int a = 3, b = 4;
cout << "a =" << a << ", B ="
<< b << Endl;
Swap (&A,&B);
cout << "a =" << a << ", B ="
<< b << Endl;
System ("pause");
return 0;
}
void swap (int *x,int *y)
{
int t = *x;
*x = *y;
*y = t;
}

Output:

A=3,b=4

A=4,b=3

Analysis:

The parameter passed in Mian () is an address

Swap (int * x, int * y) pointer variable with x, y as int type. *x,*y the store of the address corresponding to the exchange, equivalent to the X address of the original Y value, y address of the original X value

Personal understanding: &x as a whole corresponds to a pointer to the memory address

(3) If the reference is a parameter, it can make any operation on the formal parameter can change the corresponding data, and make the function call appear convenient and natural. A reference is passed by the reference operator "&" in front of the parameter when the function is defined.

#include <iostream>
using namespace Std;
void swap (int&,int&);
int main ()
{
int a = 3, b = 4;
cout << "a =" << a << ", B ="
<< b << Endl;
Swap (A, b);
cout << "a =" << a << ", B ="
<< b << Endl;
System ("pause");
return 0;
}
void swap (int &rx,int &ry)
{
int t = RX;
Rx = ry;
ry = t;
}

Output:

A=3,b=4

A=4,b=3

Analysis:

The RX ry in swap is equivalent to the alias of X y in main, although the variable name is different but corresponds to the same in-memory variable

Different names, the same variable.

"Finishing"--c++ three kinds of parameter passing way

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.