In C language, formal parameters are parameters specified in the function definition. They are used to accept actual parameters from function calls. Their functions include serving as placeholders for actual parameters, allowing function reuse, and controlling functions. Interaction with external data. In addition, the type and number of formal parameters must be consistent with those specified in the function declaration, limited to within the function, and passed by value (changing the formal parameters will not affect the actual parameters), but passing by reference can also be achieved through pointers.
Formal parameters: function parameters
In C language, formal parameters refer to the columns in the function definition or declaration variables that accept the actual parameters passed from the function call.
The role of formal parameters
Type and number of formal parameters
Formal parameters can be of any data type, and their number and type must be consistent with those specified in the function declaration.
The scope of formal parameters
The scope of formal parameters is limited to the inside of the function. They do not exist before the function call and are destroyed after the function returns.
Pass by value
Pass by value is used in C language, which means that the value of the actual parameter is copied into the formal parameter. Therefore, changes to the formal parameters do not affect the actual parameters.
Pass by reference
To pass parameters by reference (that is, changing the formal parameters will also change the actual parameters), you can use pointers as formal parameters.
Formal parameter example
<code class="c">void sum(int x, int y) { int sum = x + y; // 对形参 `x` 和 `y` 的更改不会影响函数调用处的实际参数 }</code>
In this example, x
and y
are formal parameters. When function sum
is called, the values of the actual arguments are copied into x
and y
.
The above is the detailed content of What does formal parameter mean in C language?. For more information, please follow other related articles on the PHP Chinese website!