The difference between formal parameters and actual parameters in C language

下次还敢
Release: 2024-04-27 22:27:32
Original
757 people have browsed it

Formal parameters and actual parameters are concepts in function calls. Formal parameters are used to receive actual data, and actual parameters are used to pass actual data. The formal parameters are located in the function header, and the actual parameters are located when the function is called; the formal parameters and the actual parameters establish a one-way relationship, and the actual parameters can modify the formal parameters but not vice versa; the scope of the formal parameters is limited to the function but the actual parameters are independent of the function; data type The upper formal parameter must specify a clear type, and the actual parameter type must be compatible with the formal parameter.

The difference between formal parameters and actual parameters in C language

Formal parameters and actual parameters

In C language, formal parameters and actual parameters are involved in function calls Important concepts. There are obvious differences between them:

Meaning

  • Formal Parameters:Parameters declared in the function definition , used to receive the actual data passed to the function.
  • Actual Arguments:The actual data value passed to the function when the function is called.

Position

  • Formal parameters are located in the function header and are declared in the form of variables.
  • The actual parameters are located when the function is called, enclosed in parentheses immediately after the function name.

Association

  • Formal parameters and actual parameters are associated during function calls, allowing actual parameters to pass data to formal parameters.
  • The association is one-way, that is, the actual parameters can modify the formal parameters, but the formal parameters cannot change the actual parameters.

Scope

  • The scope of formal parameters is limited to the inside of the function.
  • The scope of actual parameters is independent of the function and is not affected by the internal function.

Data type

  • The formal parameter must specify an explicit data type.
  • The data type of the actual parameter must be compatible with the type of the formal parameter.

Example

The following function definition:

void print_name(char *name);
Copy after login

Among them,nameis a formal parameter and is a character pointer .

When calling this function, you can pass an actual parameter as follows:

char *myName = "John Doe"; print_name(myName);
Copy after login

In this example,myNameis the actual parameter passed to the function. It is associated with the formal parametername, allowing the function to access and print the string "John Doe".

Note:

  • In a function call, the number and type of actual parameters must match the formal parameter list.
  • The default value of the formal parameter can only be specified in the function definition, while the value of the actual parameter can be passed at call time.

The above is the detailed content of The difference between 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!