[C++] How to understand the passing by value and address in function calls

1. Code use case

        This article uses the following code for illustration:

#include <Windows.h>

void SwapValue(int x,int y)
{
	int t = 0;
	t = x;
	x = y;
	y = t;
	printf("x = %d,y = %d\n",x,y);
}

void SwapAddress(int* x,int* y)
{
	int t = 0;
	t = *x;
	*x = *y;
	*y = t;
	printf("x = %d,y = %d\n", *x, *y);
}

int main()
{
	int a = 3;
	int b = 2;
	
	SwapValue(a,b);
	//SwapAddress(&a,&b);

	printf("a = %d,b = %d\n",a,b);
	system("pause");

    return 0;
}

2. Formal parameters and actual parameters

        What are formal parameters? For example, the following exchange value function:

         

         Among them, int x and int y are formal parameters , why are they called formal parameters? Because they are parameters without values , like two bottles with nothing in them:

        

        The function defines how the two bottles need to be used (in the example, the contents of the two bottles are exchanged). This behavior is the same as the function expression in mathematics :

         For example, the formula: ax+by=c, x and y are formal parameters, only after determining the exact value of x and y, and then calculating according to the formula can the result be obtained:

        In the figure, the specific value is transferred to the exchange function in the main function , which is the actual parameter :

 

 3. Pass value

        In fact, the specific core of value passing has been explained above. When the main function copies the values ​​of a and b to X and Y , this results in four values ​​in the four bottles:

        Immediately afterwards, the bottles x and y are exchanged according to the function rules, so that the values ​​​​of the two bottles are exchanged:

        So the final result can be drawn:

         Summary: Passing a value in a function call is to copy the data in the actual parameter to the formal parameter, so the two parameters are irrelevant to each other .

Four. Address

        The meaning of passing address is to directly give the address of the actual parameter to the formal parameter, and let the formal parameter modify the real value through the address. In the example, it is equivalent to two formal parameters x and y, and a and b are filled with data and bottles. into the bottle:

        Then swap the values ​​in the bottle according to the function:

        code show as below:

        After the output, you will find the following results:

        Summary: The address in the function call is to put the body of the actual parameter into the formal parameter, so the two parameters will interfere with each other.

        Off-topic: If you change the code to this:

        The effect will become the same as passing the value:

        Because this means swapping bottle a and bottle b in bottle X and bottle Y:

            If you don't know why, then quickly pick up your notebook and learn about pointers~~ 

Guess you like

Origin blog.csdn.net/qq_41884002/article/details/126662940