Detailed explanation of the difference between formal parameter and actual parameter transmission in C language

Time:2021-12-30

Parameter transfer refers to the process that the actual parameters will transfer the parameter value to the corresponding formal parameters during the running of the program, and then realize the data processing and return in the function. There are three ways to pass parameters:

  • pass by value
  • Address delivery
  • Reference passing

tips:

  1. The formal parameters of the called function will temporarily allocate storage units only when the function is called. Once the call is completed, the occupied memory will be released
  2. Both value passing and address passing pass a copy of the argument
  3. In C language, the data transfer between arguments and formal parameters is one-way “value transfer”. One-way transfer can only be passed from arguments to formal parameters, and vice versa
  4. The output format is% d instead of% P, because decimal numbers look more intuitive

1 value transfer

If you only pass values:

#include <stdio.h>
 
//Value transfer: value transfer
void swap( int x, int y);
 
int main()
{
  int a=2,b=3;
  Printf ("before: argument is a, B \ Na =% D, address is% d \ NB =% D, address is% d \ n \ n", a, & A, B, & B);
  swap(a,b);
  Printf ("after: argument is a, B \ Na =% D, address is% d \ NB =% D, address is% d \ n \ n", a, & A, B, & B);
  return 0;
}
 
void swap(int x,int y)
{
  int tmp;
  tmp=x;
  x=y;
  y=tmp;
  Printf ("inside: formal parameter is x, y \ NX =% D, address is% d \ NY =% D, address is% d \ n \ n", x, & X, y, & Y);
}

Execution result: from the address of formal parameters X and y, the passed are copies of arguments a and B. Modifications to the copy do not change the value of the argument..

If the value is passed, what is the address passed?

#include <stdio.h>
 
//Value Passing: address passing
void swap( int *x, int *y);
 
int main()
{
  int a=2,b=3;
  Printf ("before: argument is & A, & B \ Na =% D, address is% d \ NB =% D, address is% d \ n \ n", a, & A, B, & B);
  swap(&a,&b);
  Printf ("after: argument is & A, & B \ Na =% D, address is% d \ NB =% D, address is% d \ n \ n", a, & A, B, & B);
  return 0;
}
 
void swap(int *x,int *y)
{
  int *tmp= NULL;
  tmp=x;
  x=y;
  y=tmp;
    Printf ("inside: formal parameters are * x, * y \ n * x =% D, x =% D, address is% d \ n * y =% D, y =% D, address is% d \ n \ n", * x, x, & X, * y, y, & Y);
}

Execution result: the formal parameters are the addresses of a and B, but the copies of a and B are accessed through the addresses of a and B (i.e. X and y). Direct exchange of addresses of a and B failed.

2 address transfer

#include <stdio.h>
  
//Address delivery
void swap( int *x, int *y);
 
int main()
{
  int a=2,b=3;
  Printf ("before: argument is & A, & B \ Na =% D, address is% d \ NB =% D, address is% d \ n \ n", a, & A, B, & B);
  swap(&a,&b);
  Printf ("after: argument is & A, & B \ Na =% D, address is% d \ NB =% D, address is% d \ n \ n", a, & A, B, & B);
  return 0;
}
 
void swap(int *x,int *y)
{
  int tmp;
  tmp=*x;
  *x=*y;
  *y=tmp;
    Printf ("inside: formal parameters are * x, * y \ n * x =% D, x =% D, address is% d \ n * y =% D, y =% D, address is% d \ n \ n", * x, x, & X, * y, y, & Y);
}

The execution result is: the formal parameter is the address of a and B, but the copy of a and B is accessed through the addresses of a and B (i.e. X and y). Although the addresses of a and B cannot be exchanged directly, the value pointed to by the addresses of a and B (i.e. X and y) can be modified. That is, the pointer cannot be modified directly, but the value pointed to by the pointer can be modified. So this time, the values of a and B are exchanged.

3 reference transfer

#include <stdio.h>
  
//Reference passing
void swap( int &x, int &y);
 
int main()
{
  int a=2,b=3;
  Printf ("before: argument is a, B \ Na =% D, address is% d \ NB =% D, address is% d \ n \ n", a, & A, B, & B);
  swap(a,b);
  Printf ("after: argument is a, B \ Na =% D, address is% d \ NB =% D, address is% d \ n \ n", a, & A, B, & B);
  return 0;
}
 
void swap(int &x,int &y)
{
  int tmp;
  tmp=x;
  x=y;
  y=tmp;
    Printf ("inside: formal parameter is & X, & Y \ NX =% D, address is% d \ NY =% D, address is% d \ n \ n", x, & X, y, & Y);
}

void swap( int &x, int &y); // Indicates that an argument is passed in, not a copy.

The execution result is that the argument is passed, not a copy of the argument. The modification of the argument will successfully change the value.

supplement

int x=1;
int *y=&x; // For pointer passing, y has its own independent memory address, the stored content is the address of X, * y is the value of X
int &z=x; // For reference passing, it can be understood that z is x and X is Z, but the names are different

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.