What are formal parameters and actual parameters in C language?

下次还敢
Release: 2024-04-29 17:30:26
Original
773 people have browsed it

In C language, formal parameters are variables declared in the function definition and are used to receive data; actual parameters are the actual data passed to the function when the function is called. The formal parameter gets a copy of the actual parameter value, so modifications to the formal parameter do not affect the actual parameter and vice versa.

What are formal parameters and actual parameters in C language?

Formal parameters and actual parameters

In C language, formal parameters and actual parameters are two important Concepts that are used to pass data into functions.

Formal parameters

  • Formal parameters are variables declared in the function definition and are used to receive the data actually passed to the function.
  • Formal parameters can have any legal variable name.
  • The type of the formal parameter must be the same as the data type actually passed to the function.

Actual parameters

  • Actual parameters are the data actually passed to the function when the function is called.
  • The actual parameter can be a constant, variable or expression.
  • The type of the actual parameter must be the same as the type of the formal parameter.

Data passing

When the function is called, the actual parameters are passed to the formal parameters. The value of the formal parameter is obtained from the actual parameter by copying it. This means that any modifications to the formal parameters will not affect the values ​​of the actual parameters and vice versa.

Purpose of formal parameters and actual parameters

  • Formal parameters are used to specify the data required by the function in the function definition.
  • Actual parameters are used to pass actual data to the function.
  • The separation between formal parameters and actual parameters allows a function to work independently of the specific set of parameters it is called with.

Example

The following example demonstrates the use of formal parameters and actual parameters:

<code class="c">// 函数定义
int sum(int a, int b) {
  // a 和 b 是形参
  return a + b;
}

// 函数调用
int result = sum(5, 10); // 5 和 10 是实参</code>
Copy after login

In the above example, a and b are the formal parameters of the function sum, and 5 and 10 are the actual parameters when the function is called. The function returns 15, which is the sum of the two arguments passed to the formal parameters a and b.

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template