Understanding Function Parameters and Arguments in C Language in One Sentence

Understanding Function Parameters and Arguments in C Language in One SentenceKey SummaryAfter a function call, the operation is performed on the initialized parameter, not the argument!Function Parameters and ArgumentsIn C language, depending on the different states of the function, the parameters can be roughly divided into two categories: one is formal parameters, and the other is actual parameters.Formal parametersare the parameters declared in the function definition or declaration in C language, generally referred to as formal parameters, and are often called placeholders, similar to the sockets in daily life.The main function is to specify the type of parameters and receive the values of the parameters.Another way to understand it is that when defining a function, the type of the parameter is declared, meaning that this is a local variable that belongs only to the local scope of the function. Then, when the function is called, the formal parameter is defined and initialized based on the form of the argument passed, which means that after the function call, the operation is performed on the initialized formal parameter, not the actual parameter (this is my personal understanding, for reference only).There are two ways to receive values—by value and by address (for details, see the next article in this column on the detailed explanation of passing values and addresses in function parameters).For example, in the function declared below, a is the formal parameter:

int addOne( int a )

Actual parametersWhen a C language program calls a function, the variable passed to the function as a parameter is the actual parameter. Although what is generally passed is a copy of the variable’s value, the actual parameter is not the value of the variable, but the variable itself (this is my personal understanding, which may not be correct, for reference only). For example, in the following code, a is the actual parameter.

#include <stdio.h>// Here, a is the formal parametervoid addOne(int a) {    a++;}int main() {    // Here, x is the actual parameter    int x = 1;    addOne(x);    printf("x's value has not been modified, still %d\n", x);    return 0;}

The code compiles and runs, producing the output:

x's value has not been modified, still 1

Please open in the WeChat client

Disclaimer:The content is for reference only, does not guarantee correctness, and should not be used as the basis for any decisions!

Leave a Comment