Language: C++

Numerical transmission

  • Value passed
  • Pointer passed
  • reference

define

Value passed

Parameters are copies of arguments, and changing the value of a parameter does not affect the value of the external argument. From the point of view of the called function, value passing is one-way (argument -> parameter), the value of the argument can only be passed in,

Can’t get out. Value passing is used when parameters need to be modified internally in a function and the change is not expected to affect the caller.

Pointer passed

A parameter is a pointer to the address of the argument. A pointer to a parameter is an operation on the argument itself.

Pointer passing parameters is essentially value passing, which is an address value. In the process of value passing, the formal parameters of the called function are treated as local variables of the called function, that is, memory space is opened on the stack to store the value of the argument put in by the calling function, thus becoming a copy of the argument. The characteristic of value passing is that any operation of the called function on formal parameters is carried out as a local variable and does not affect the value of the argument variable of the calling function. The address of the argument pointer itself does not change.

reference

Parameters are aliases for the arguments. Operations on parameters are operations on the arguments. In the reference passing process, the formal parameters of the called function are also used as local variables to create memory space in the stack, but the address of the argument variable put in by the calling function is stored. Any operation on parameters by the called function is treated as indirection, that is, by accessing the argument variable in the calling function via an address stored on the stack. Because of this, anything the called function does to the parameter affects the argument variable in the calling function.

The instance

#include<iostream>
using namespace std;
/ / value
 void change1(int n){
    cout<<"Value passing -- address of function operation"<<&n<<endl;         // Display the copied address instead of the source address
    n++;
}

// Reference passing
void change2(int & n){
    cout<<"Reference passing -- address of function operation"<<&n<<endl; 
    n++;
}
 // Pointer passing
void change3(int *n){
     cout<<"Pointer passing -- function operating address"<<n<<endl; 
    *n=*n+1;
 } 
int     main(a){
    int n=10;
    cout<<"Address of argument"<<&n<<endl;
    change1(n);
    cout<<"after change1() n="<<n<<endl;
    change2(n);
    cout<<"after change2() n="<<n<<endl;
    change3(&n);
    cout<<"after change3() n="<<n<<endl;
    return true;
}
Copy the code

Running results:

Address of the argument 0x7FFee9a7a9b8 value pass -- function operation address 0x7FFee9a7a96c after Change1 () n=10 Reference pass -- function operation address 0x7FFee9a7a9b8 after Change2 () n=11 0x7ffEE9a7a9b8 after Change3 () n=12Copy the code

Reference

  • C++ value pass, pointer pass, reference pass in detail