当前位置:网站首页>Formal and actual parameters, value passing and address passing

Formal and actual parameters, value passing and address passing

2022-06-30 02:56:00 @sen

Shape parameter : Formal parameters , It can be understood by the formal subject in English ;

Actual parameters : The actual parameter , It can be understood by the actual subject in English ;

int  sum(int n)  // Formal parameters 
{
	int result = 0;
	do
	{
		result += n;
	} while (n-- > 0);
	return result;
}

int main()
{

	result = sum(n);  // The actual parameter 
  }

Pass value : The passed in variable does not contain a pointer , In variables that do not contain pointers , Arguments do not affect main The value of the inside , stay c Each function in the language has its own scope , The interior is not interlinked

#include<stdio.h>

void swap(int x, int y);   //swap: The meaning of exchange , Exchange with each other , No need to return 

void swap(int x, int y)
{
	int temp;
	printf("in swap, Before switching :x=%d,y=%d\n", x, y);
	temp = x;
	x = y;
	y = temp;
	printf("in swap, After exchange :x=%d,y=%d\n", x, y);

}

int main()
{
	int x = 3, y = 5;
	printf("in main, Before switching :x=%d,y=%d\n", x, y);
	swap(x, y);                                                  
	printf("in main, After exchange :x=%d,y=%d\n", x, y);                  
	return 0;
}

Code above ,x and y Value , stay swap There are changes before and after the exchange of functions , But it doesn't affect main In the function x and y Value

Byref : Variables with pointers , The pointer holds the address , Only the address needs to be passed between the two functions , You can affect the values of different functions , Equivalent to an arbitrary gate

#include<stdio.h>

void swap(int *x, int *y);   

void swap(int *x, int *y)
{
	int temp;
	printf("in swap, Before switching :x=%d,y=%d\n", *x, *y);              
	temp = *x;                                                 
	*x = *y;
	*y = temp;
	printf("in swap, After exchange :x=%d,y=%d\n", *x, *y);

}

int main()
{
	int x = 3, y = 5;
	printf("in main, Before switching :x=%d,y=%d\n", x, y);
	swap(&x, &y);
	printf("in main, After exchange :x=%d,y=%d\n", x, y);
	return 0;
}

Code above , Put the corresponding x,y prefix *, It turns a variable into a pointer , At this point, the values of the two functions interact with each other , So in swap Function x,y After exchange ,main Medium x,y The value has changed accordingly

Pass array : Not all parameters will be passed in , In fact, only the address of the first element is passed

#include<stdio.h>

void get_array(int a[10]);    //get_array Array name 

void get_array(int a[10])
{
	int i;
	a[5] = 520;
	for (i = 0; i < 10; i++)
	{
		printf(" a[%d]=%d\n",i,a[i]);
	}
}

int main()
{
	int a[10] = { 1,2,3,4,5,6,7,8,9,0 };
	int i;

	printf(" stay main Function to print again \n");
		for (i = 0; i < 10; i++)
		{
			printf(" a[%d]=%d", i, a[i]);
		}

	get_array(a);

	return 0;
}

If you are not clear, look at the following example

because void get_array Medium b It's just an address , So only 4 Bytes , and main Function a It's a plastic variable , Each of these elements accounts for 4 Bytes , So the total is 4*10=40 Bytes

#include<stdio.h>

void get_array(int b[10]);

void get_array(int b[10])    
{
	printf("sizeof b:%d\n", sizeof(b));
}

int main()
{
	int a[10] = { 1,2,3,4,5,6,7,8,9,0 };  
	printf("sizeof a:%d\n", sizeof(a));
	get_array(a);
	return 0;
}

原网站

版权声明
本文为[@sen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202160642016300.html

随机推荐