Home  >  Article  >  Backend Development  >  What do formal parameters and actual parameters mean in C language?

What do formal parameters and actual parameters mean in C language?

下次还敢
下次还敢Original
2024-05-02 19:45:27990browse

Formal parameters are parameters declared in the function definition and receive the input of the function; actual parameters are the actual values ​​passed when calling the function. In C language, a value transfer mechanism is used between formal parameters and actual parameters, that is, the value of the actual parameter is copied to the formal parameter without modifying the value of the actual parameter itself. The type of formal parameters can be declared as any data type, and the number and type of actual parameters must be consistent with those in the function definition.

What do formal parameters and actual parameters mean in C language?

Formal parameters and actual parameters: Basic concepts of C language

What are formal parameters and actual parameters ?

In C language, formal parameter and actual parameter are two closely related concepts:

  • Formal parameter: is a variable declared in the function definition, indicating the input parameters received by the function.
  • Actual parameters: is the value passed to the formal parameter when calling the function.

The relationship between formal parameters and actual parameters

When calling a function, the value of the actual parameter will be assigned to the corresponding formal parameter, so that inside the function use. Therefore, the role of formal parameters is to receive the values ​​of actual parameters and make these inputs accessible within the function.

Type of formal parameters

Formal parameters can be declared as any data type, including basic data types, structures, pointers, etc.

Value passing

In C language, the value passing mechanism is used between formal parameters and actual parameters. This means that the value of the actual parameter is copied into the formal parameter, but the value of the actual parameter itself is not modified.

Example

The following is an example of a C language function that receives two formal parameters:

<code class="c">void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}</code>

In this function, the formal parametersa and b point to two integer variables respectively. When the function is called, the values ​​of the actual parameters are assigned to a and b, thus exchanging the values ​​of the two integers within the function.

Note:

  • Formal parameters and actual parameters can be different data types.
  • The number and type of formal parameters must be consistent with those in the function definition.
  • Actual parameters can be represented by variables, constants or expressions.

The above is the detailed content of What do formal parameters and actual parameters mean in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn