Home >Backend Development >C++ >The difference between actual parameters and formal parameters in C language

The difference between actual parameters and formal parameters in C language

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

The actual parameters are the actual values ​​passed when the function is called, while the formal parameters are placeholder variables declared in the function definition to receive the actual parameter values. The actual parameters are determined when calling, and the formal parameters are determined when defining; the actual parameters can be changed, but the formal parameters can only be modified within the function body.

The difference between actual parameters and formal parameters in C language

The difference between actual parameters and formal parameters in C language

In C language, the difference between actual parameters and formal parameters Parameters are two important concepts in function calls, and there are key differences between them.

Actual parameters

  • Actual parameters are the values ​​or variables that are actually passed to the function .
  • They appear inside parentheses when calling a function.
  • Actual parameters can be constants, variables, expressions or the function call itself.

Formal parameters

  • Formal parameters are placeholder variables declared in function definition.
  • They appear as type and name in the function header.
  • When the function is called, the value of the actual parameter is passed to the corresponding formal parameter.

Difference

The main difference is:

  • The actual parameter is the actual value passed when the function is called, while the formal parameter is Placeholder used when defining functions.
  • The actual parameters are determined when the function is called, while the formal parameters are determined when the function is defined.
  • The actual parameters can be changed, but the formal parameters can only be used and modified within the function body.

Example

Consider the following function definitions:

<code class="c">int sum(int a, int b)
{
    return a + b;
}</code>
  • a and b It is a formal parameter and a placeholder variable.

When calling a function, the actual parameters are passed to the formal parameters:

<code class="c">int x = 5, y = 10;
int result = sum(x, y);</code>
  • x and y are actual parameters, use to actually pass to the function.
  • a and b will receive the values ​​of x and y respectively.

The above is the detailed content of The difference 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