The difference between the function form and the actual parameter in C language

Formal parameters : the parameters specified when the function is defined, they do not occupy the storage unit in the memory when the function call does not occur. Only when a function call occurs, the formal parameters in the function are allocated memory units. After the function call ends, the memory unit occupied by the formal parameters is also released.

Actual parameters : the parameters you pass when you call the function. The actual parameters can be constants, variables, expressions.

Note : The formal and actual parameters actually occupy two different storage units.

Insert picture description here
Transfer between formal and actual parameters

In the C language, the data transmission of the actual participation formal parameter is "value transmission". One-way transmission can only be passed from the actual parameter to the formal parameter, not from the formal parameter to the actual parameter. If the value of the formal parameter changes, it will not change the value of the actual parameter of the calling function.
Classification of the transfer between the actual parameters

1. Pass by value (no connection for real form)

Pass-by-pass is commonly used in ordinary programming. A variable of a basic data type is defined, and the variable is passed to the function as an argument of the function when a function is called. This transfer method uses one-way value transfer, the real form has no connection, and the change of the formal parameter does not affect the actual parameter.

2. Transfer by address (actual parameters may be changed by operating formal parameters)

Passing by address mainly occurs when the function parameters are pointer variables, arrays, etc.

note:

In the case of using a pointer as a function parameter in essence, when the function is called, the actual parameter variable is passed to the formal parameter variable, and the still taken isOne-way value passing. If the value of the formal parameter pointer variable is simply changed in the called function, these formal parameters are destroyed after the function call is over, which will not affect the value of the actual parameter pointer variable when the function is called.

Only when you change the value of the variable pointed to by the pointer by operating the formal parameter pointer variable in the called function, you can change the value of the variable pointed to by the actual parameter pointer variable. Only in this case can the formal parameter change affect the actual parameter.

Published 28 original articles · praised 7 · visits 1177

Guess you like

Origin blog.csdn.net/haduwi/article/details/105330270