C language function pass value and pass pointer

What is a function?

  • A function is a reusable functional code block, a closed (space), which can be called at will in the code. Using function encapsulation can reduce the development of repetitive code, improve code utilization, and make code more concise and easier to understand.
  • We can also understand the function as a factory. The parameters of the function are equivalent to the raw materials in the factory. The code inside the function (function body) is equivalent to the production line of the factory to produce a mobile phone, and the result obtained by processing the function parameters is reused. return Return, the return value is the mobile phone produced.

Let's write a function that implements the sum of two numbers:

int add(int a, int b){
    
    
	return a + b;
}
int main()
{
    
    
	int a = 5;
	int b = 7;
	printf("%d\n",add(a, b));
	system("pause");
	return 0;
}

Function call

add(a,b)
//是对上述函数的调用
-Function call process

When the code is executed to the position of the function call, it will enter the internal execution of the function, and at the same time assign the parameters, execute sequentially from top to bottom according to the internal order of the function, until it encountersreturnThe statement and function end, return to the called position after the end, and continue to execute.

  • If only a function is defined, the content of the function body will not be executed.
  • A function can be called multiple times, and each time it is called, uncommon parameters can be passed.
  • The formal parameter of the function is a'copy' of the actual parameter
int add(int a, int b){
    
    
	return a + b;
}
int main()
{
    
    
	int a = 5;
	int b = 7;
	printf("%d\n",add(a, b));---------->
	printf("%d\n", add(2, 9));--------->
	system("pause");
	return 0;
}
//执行结果
12
11
请按任意键继续. . .

Pass by value

== We write a function that exchanges values ​​of a and b ==

void swap(int x, int y)
{
    
    
	int temp = x;
	x = y;
	y = temp;
	
}
int main()
{
    
    
	int a = 10;
	int b = 16;
	swap(a, b);
	printf("a = %d b = %d\n", a, b);
	system("pause");
	return 0;
}

The results are as follows

a = 10 b = 16
请按任意键继续. . .

It can be seen that the values ​​of a and b are not exchanged in the end. At the beginning, the values ​​of a and b are 10 and 16, but in the end they are still the same value. The reason for this is that the x and y accessed inside the function are not the body of a and b.

Pass pointer

  • So in order to solve the above problems, we need to pass pointers,
void swap(int* a, int* b)
{
    
    
	int temp =*a;
	*a = *b;
	*b = temp;
	
}
int main()
{
    
    
	int a = 10;
	int b = 16;
	swap(&a,&b);
	printf("a = %d b = %d\n",a, b);
	system("pause");
	return 0;
}

The result of the operation is

a = 16 b = 10
请按任意键继续. . .

It can be seen that in this case, the values ​​of a and b are actually exchanged. https://editor.csdn.net/md/?articleId=109286880

Guess you like

Origin blog.csdn.net/supermanman_/article/details/109278097