Home >Backend Development >C++ >What is the relationship between actual parameters and formal parameters in C language

What is the relationship between actual parameters and formal parameters in C language

下次还敢
下次还敢Original
2024-05-02 19:42:311385browse

In C language, actual parameters are passed to the value of the function, while formal parameters receive the parameters of the function. There is the following relationship between them: Type matching: the formal parameter type should be compatible with the actual parameter type. Quantity matching: The number of formal parameters must be equal to the number of actual parameters. Passing by value (default): The actual parameter value is passed to the formal parameter, and modifying the formal parameter does not affect the actual parameter; Address passing (optional): Using a pointer or array actual parameter can achieve address transfer, and modifying the formal parameter can also modify the actual parameter; memory Allocation: The formal parameters are allocated memory on the stack, and the actual parameter values ​​are copied to the formal parameter memory.

What is the relationship between actual parameters and formal parameters in C language

The relationship between actual parameters and formal parameters in C language

In C language, the relationship between actual parameters and formal parameters Ginseng plays different roles, but they are closely related.

Actual parameters

  • The value actually passed to the function when the function is called.
  • can be the value of a constant, variable or expression.

Formal parameters

  • The parameters accepted by the function are used to represent the values ​​of the actual parameters.
  • Usually declared when the function is defined.
  • In the function body, initialize with the value of the actual parameter.

Relationship

The relationship between actual parameters and formal parameters is as follows:

  • Type matching: The type of the formal parameter must be compatible with the corresponding actual parameter type.
  • Quantity matching: The number of formal parameters must match the number of actual parameters.
  • Value passing: By default, the value of the actual parameter is passed to the formal parameter. Modifying the formal parameters does not affect the value of the actual parameters, and vice versa.
  • Address passing: Address passing can be achieved by using pointers or array arguments. This allows you to modify the value of the actual parameter.
  • Memory allocation: Formal parameters are allocated memory on the stack during the function call, and the value of the actual parameter is copied to the memory of the formal parameter.

Example

For example, consider the following function:

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

In this example, the actual parameters are two integers when the function is called variable. The formal parameters a and b are two pointers to integers. When the function is called, the values ​​of the actual parameters are copied into the memory of the formal parameters. Operations in the function body modify the values ​​of the formal parameters, thereby modifying the values ​​of the actual parameters.

The above is the detailed content of What is the relationship between actual parameters and formal parameters 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