In C language formal parameters and real parameters are used to solve and instance codes

  • 2020-05-19 05:25:58
  • OfStack

Formal parameters and actual parameters

The parameters of the function are divided into formal parameters and arguments. In this section, step 1 introduces the characteristics of formal parameter and argument and the relationship between them. Formal parameters appear in the function definition and can be used throughout the function, but they cannot be used without the function. Arguments appear in the main function, into the function called, the real variables can not be used. The function of parameter and argument is data transfer. When the function is called, the main function transfers the value of the argument to the parameter of the function being called so as to transfer the data from the main function to the function being called.

Function parameters and arguments have the following characteristics:

1. The parameter only allocates the memory unit when it is called. At the end of the call, the allocated memory unit is immediately released. Therefore, formal parameters are only valid inside the function. The parameter can no longer be used after the call to the function returns the main function.

Arguments can be constants, variables, expressions, functions, etc., no matter what type of quantity the arguments are, they must have certain values when a function is called in order to pass these values to the parameters. Therefore, you should use assignment, input and so on in advance to get the argument to a certain value.

3. The number, type and order of arguments should be strictly 1, otherwise the error of "type mismatch" will occur.

4. The data transfer in the function call is one-way. That is, the value of the argument can only be passed to the argument, and the value of the argument cannot be passed back to the argument. Thus, the value of the parameter changes during the function call, while the value of the argument does not.

This problem can be illustrated.


">#include<stdio.h> 
int add(int num) 
{ 
  int i; 
  for(i=0;i<100;i++) 
  { 
    num=num+i; 
  } 
  printf("num=%d\n",num); 
} 
int main() 
{ 
  int num; 
  printf(" The input 1 The number of \n"); 
  scanf("%d",&num); 
  add(num); 
  printf("n=%d\n",num); 
  return 0; 
} 

This procedure defines a function add, the function of which is to find the cumulative value of num+i. Enter the num value in the main function as an argument and pass the shape parameter nuum to the add function at call time. In the main function, the printf statement is used to output the n value once. This num value is the value of the argument num. In the function add, the printf statement is used to output the n value once. The num value is the n value 0 obtained by the formal parameter. At run time, enter the num value of 6. The argument num has a value of 6. When this value is passed to the function add, the initial value of num is also 6. During the execution of the function, the value of num becomes 4956. After returning to the main function, the value of the output argument num is still 6. The value of the visible argument does not change with the shape parameter.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: