Understanding C Language: Formal Parameters and Actual Parameters in Functions

This is the 20th article in the introduction to C language.

We will first review the definition and syntax of function calls, and then focus on formal parameters and actual parameters in functions.

1 Functions

A C program consists of functions, and a program can contain multiple functions, but there can only be one main function, which is themain function.

Functions are divided intosystem functions and user-defined functions.

Both scanf( ) and printf( ) are system functions; scanf( ) is an input function, and printf( ) is an output function, which we can call directly.

User-defined functions are those that we define ourselves, and their functionality is implemented by our own programming.

The functions mentioned below refer to user-defined functions.

A C language program can consist of multiple functions, but there can only be one main function, which is the main( ) function.

No matter how many functions the program consists of, the program always starts executing from the main( ) function.

2 Four Types of Functions

The syntax format for defining a function is:

DataType FunctionName( FormalParameterList )

{

FunctionBody;

}

The above formal parameter list is optional; not every function has formal parameters.

There are four types of functions:

Type 1: Function with no return value and no parameters

void FunctionName( ) { }

The following statement defines a function with no return value and no parameters; the return type of the function is void, and the function name can be anything, for example, f1:

Understanding C Language: Formal Parameters and Actual Parameters in Functions

Type 2: Function with no return value and with parameters

void FunctionName(FormalParameterList) { }

The following statement defines a function with no return value and with parameters; the parameters are the formal parameters, the return type of the function is void, and the function name can be anything, for example, f2, with one parameter of type int, named m:

Understanding C Language: Formal Parameters and Actual Parameters in Functions

Type 3: Function with a return value and no parameters

DataType FunctionName( ) { }

If a function has a return value, there must be a statement in the function body:

return Value;

This value must be of the same type as the function’s data type.

For example, if the function header is: int f3( )

Then the function body must contain a statement:

return Integer;

The following statement defines a function with a return value and no parameters; the function type is int, indicating that the function will return an integer, and the function name can be anything, for example, f3, with no parameters.

Understanding C Language: Formal Parameters and Actual Parameters in Functions

Generally, functions with a return value and no parameters are rare because they are not very meaningful. The most common type is the fourth type.

Type 4: Function with a return value and with parameters

DataType FunctionName(FormalParameterList) { }

The following statement defines a function with a return value and with parameters; the function type is int, indicating that the function will return an integer, and the function name can be anything, for example, f4, with two integer parameters x and y.

Understanding C Language: Formal Parameters and Actual Parameters in Functions

3 Function Calls

The purpose of defining a function is to call it in other functions, such as calling a user-defined function in the main function.

The syntax format for calling a function is:

FunctionName( );

Or:

FunctionName( ActualParameterList );

If it is a type 1 or type 3 function, with no return value and no parameters or with a return value and no parameters, then there are no corresponding parameters when calling.

However, if it is a type 2 or type 4 function, with no return value and with parameters or with a return value and with parameters, then corresponding parameters must be provided when calling.

Program 1:

Understanding C Language: Formal Parameters and Actual Parameters in Functions

The output result is that the first line outputs three * symbols, the second line outputs three # symbols, and the third line outputs three * symbols:

Understanding C Language: Formal Parameters and Actual Parameters in Functions

Program 2:

Understanding C Language: Formal Parameters and Actual Parameters in Functions

The function f3 is of typeint, indicating that this function has a return value, and the return value is of type int (returns an integer).

That is to say, the function f3 must have a statement like:

return Integer;

The function f3 has two formal parameters: a and b, both of type int.

The main function can call the function f3, and it can be called multiple times.

When calling, the actual parameters must correspond to the formal parameters.

For example, if the calling statement is f3(10,20), the actual parameters are 10 and 20;

When the calling statement is f3(100,200), the actual parameters are 100 and 200.

What is the function of f3?

It is to add two integers and return their sum.

The output result of program 2 is:

Understanding C Language: Formal Parameters and Actual Parameters in Functions

Formal parameters can only be variables, but actual parameters can be constants or variables.

4 Formal Parameters and Actual Parameters

The parameters in the function definition are called formal parameters (abbreviated as formal params), and the parameters during the call are called actual parameters (abbreviated as actual params).

Formal parameters are like characters in a movie script, while actual parameters are like the actors playing those characters.

A character can be played by multiple actors; for example, the character Yang Guo can be remade multiple times, resulting in different actors playing Yang Guo, such as Andy Lau, Louis Koo, Huang Xiaoming, etc.

Program 3:

Understanding C Language: Formal Parameters and Actual Parameters in Functions

In program 3, the function f3 has two formal parameters: two int variables: a and b, and the actual parameters are two int variables n and m.

When running, if you input 100 and 200, then n=100 and m=200.

Calling the function f3(n,m) will pass 100 to the formal parameter a and 200 to the formal parameter b; the return result of f3 is a+b, which is 100+200, meaning that the function f3 returns 300 to the main function, so the result of x=f3(n,m) is x=300.

Finally, it outputs 300.

The output result of program 3:

Understanding C Language: Formal Parameters and Actual Parameters in Functions

5 Changes in Formal Parameters Do Not Affect Actual Parameters

As mentioned earlier, when calling a function, the value of the actual parameter is passed to the formal parameter, and changes to the formal parameter do not affect the corresponding actual parameter.

This is like the character Yang Guo in the script losing an arm, but the actor playing this character does not lose an arm.

The character does not affect the actor; once the actor passes the value to the character, no matter how the character changes, after the movie is finished, the actor will do what they need to do without any impact.

Program 4:

Understanding C Language: Formal Parameters and Actual Parameters in Functions

In program 4, the function f4 has two formal parameters: two int variables: a and b, and the actual parameters are two int variables x and y.

Initially, x=1 and y=2, outputting 1 and 2.

Then calling the function f4(x,y) passes the values of actual parameters x and y, which are 1 and 2, to the formal parameters a and b.

In the function f4, the value of formal parameter a is changed to 100, and the value of formal parameter b is changed to 200, outputting the values of a and b as 100 and 200.

Then the changes to the formal parameters do not affect the actual parameters; returning to the main function, it outputs x and y, which are still 1 and 2.

The output result of program 4:

Understanding C Language: Formal Parameters and Actual Parameters in Functions

6 Summary

(1) A C language program can consist of multiple functions, but there can only be one main function (the main function). The program always starts executing from the main( ) function.

(2) The four types of functions:

Function with no return value and no parameters;

Function with no return value and with parameters;

Function with a return value and no parameters;

Function with a return value and with parameters.

(3) The parameters in the function definition are called formal parameters (abbreviated as formal params), and the parameters during the call are called actual parameters (abbreviated as actual params).

(4) Formal parameters can only be variables, but actual parameters can be constants or variables.

(5) When calling a function, the value of the actual parameter is passed to the formal parameter. Changes to the formal parameter do not affect the corresponding actual parameter.

Leave a Comment