function parameter passing c++

A function call causes two things to happen:

If the function has been declared inline, the function body may have been expanded at its call site during compilation. If not declared inline, the function is called at run time. A function call causes program control to be transferred to the calling site. The called function while the execution of the currently active function is suspended The calling function resumes execution on the statement following the calling statement when the called function completes After executing the last statement of the function body or encountering the return statement return statement Finish.

All functions use memory allocated in the run-time stack of the program. This memory remains
associated with the function until the end of the function, at which point the memory is automatically freed so that the
entire It is called an activation record.

 

  1. The default initialization method for parameter passing in C++ is to copy the value of the actual parameter into the parameter storage area. This is called pass-by-value .

    When pass by value, the function does not access the currently called arguments. The value handled by the function is its local copy. These copies
    are stored on the runtime stack, so changing these values ​​does not affect the value of the arguments.

  2. But pass-by-value is not suitable in all situations. Situations that are not suitable include:
  • When a large class object must be passed as a parameter for the actual application to allocate the object and copy it to

The time and space overhead in the stack is often too large

  • When the value of the actual parameter has to be changed e.g. in the function swap() the user wants to change the value of the actual parameter but in

Can't do it with pass by value

 

 

#include <iostream>
using namespace std;
void swap (int,int);
int main() {

int i=10,j=20;
cout<<"Before swap "<<i<<' '<<j<<endl;
swap( i, j );
cout<<"After swap "<<i<<' '<<j<<endl;

}
void swap(int v1, int v2)
{
int temp = v2;
v2 = v1;
v1 = temp;
};

output:

Before swap 10 20
After swap 10 20 //swap() swaps a local copy of the argument representing the variable of the swap() argument has not been changed

Solution:

1. The parameter is declared as a pointer

 

#include <iostream>
using namespace std;
void swap (int*,int* ); //function declaration should also be changed
int main() {
    
    int i=10,j=20; cout<<"Before swap "<<i<<' '<<j<<endl; swap( &i, &j ); cout<<"After swap "<<i<<' '<<j<<endl; } void swap(int *v1, int *v2) { int temp = *v2; *v2 = *v1; *v1 = temp; };
输出:

Before swap 10 20
After swap 20 10

2. The second method is to declare the parameter as a reference

// rswap() swaps the values ​​referenced by v1 and v2
void rswap( int &v1, int &v2 ) {
int tmp = v2;
v2 = v1;
v1 = tmp;
}

 

The call to rswap() in main() looks like the original swap() calls
rswap( i, j );

#include <iostream>
using namespace std;
void swap (int &,int &);     //函数声明也要相应改变!
int main() {

int i=10,j=20;
cout<<"Before swap "<<i<<' '<<j<<endl;
swap( i, j );
cout<<"After swap "<<i<<' '<<j<<endl;

}
void swap(int &v1, int &v2)
{
int temp = v2;
v2 = v1;
v1 = temp;
};

 

output:

Before swap 10 20
After swap 20 10

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324605882&siteId=291194637