The difference between formal parameters and actual parameters

arguments (argument):
  The full name "actual parameter" is the parameter passed to the function when it is called. The actual parameter can be a constant, variable, expression, function, etc. No matter what type of quantity the actual parameter is, when the function is called, they must have certain values ​​to pass to the formal parameters. Therefore, methods such as assignment and input should be used in advance to obtain definite values ​​for actual parameters.      

Formal parameters:
The full name is "formal parameter" because it is not a real variable, so it is also called a dummy variable. It is the parameter used when defining the function name and function body. The purpose is to receive the parameters passed in when the function is called. When the function is called, the actual parameter will be assigned to the formal parameter. Therefore, we must pay attention to the number of actual parameters, the type should correspond to the formal parameter one-to-one, and the actual parameter must have a definite value.

Formal parameters: A formal parameter is a variable that is used to receive the value of the actual parameter when the function is called.

Optional according to actual needs. When there are no formal parameters, parentheses are also optional; multiple parameters should be separated by commas. Parameters include parameter name and parameter type.

The type specification of the formal parameter can be in the following two formats:

  int max(int ​​a,int b)/*The type of the formal parameter is directly explained in the formal parameter table*/

    {  return (a>b?a:b);}     

 or

   int max(a,b)

   inta,b; /*The type of the formal parameter is specified before the function body and after the function name*/

   { return(a>b?a:b); }

The former is a standard format, the latter is a traditional format, and the former is usually used.


The difference between formal parameters and actual parameters

Formal parameters appear in the function definition and can be used throughout the function body,  but not outside the function.

The actual parameter appears in the calling function. After entering the called function, the actual parameter variable cannot be used . 

The function of formal parameters and actual parameters is for data transfer. When a function call occurs, the calling function transfers the value of the actual parameter to the formal parameter of the called function to realize the data transfer from the calling function to the called function . 

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

2. The actual parameters can be constants, variables, expressions, functions, etc.  No matter what type of the actual parameters are, when the function is called, they must have definite values  ​​so that these values ​​can be passed to the formal parameters.  Therefore, methods such as assignment and input should be used in advance to obtain definite values ​​for actual parameters. 

3. The number, type, and order of actual and formal parameters should be strictly consistent,  otherwise a " type mismatch " error will occur. 

4. The data transfer that occurs in the function call is one-way .  That is, only the value of the actual parameter can be passed to the formal parameter, and the value of the formal parameter cannot be passed to the actual parameter in reverse.  Therefore, during the function call, the value of the formal parameter changes, but the value of the actual parameter does not change.

5. When the formal parameter and the actual parameter are not pointer types, when the function is running, the formal parameter and the actual parameter are different variables, they are located in different locations in the memory, the formal parameter will copy the content of the actual parameter, in When the function finishes running, the formal parameters are released, but the actual parameter content does not change .

If the parameter of the function is a pointer type variable , in the process of calling the function, the address of the actual parameter is passed to the function, and the address of the actual parameter is also used inside the function body, that is, the actual parameter itself is used . So the value of the actual parameter can be changed inside the function body.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325932265&siteId=291194637