Do you know function calling and nesting in C language? Function must learn module, you have to know!

Time:2021-9-18

1、 Function call

What is a function call? Usually in C language, a complete project program cannot realize all functions in one function. It is implemented by several functions with different functions, and there will be calls between functions.

Of course, it doesn’t mean that you must write the function modules separately, but if you write as follows:

 

I’m sure your project manager will advise you in the future.

 

 

2、 Function call mode

Function is the basic element of C language. If you want to realize the function of function, you must learn to call function correctly. The syntax format of the call is as follows:

 

As can be seen from the above syntax format, when we call a function, we need to specify the function name and argument list. The parameters in the argument list can be constants, variables, expressions or empty, and the parameters should be separated by English commas.

 

According to the position of the function in the program, there are three function calling methods:

1. Call a function as an expression

When a function is called as an expression, the return value of the function participates in the operation of the expression. At this time, the function must have a return value. The example code is as follows:

 

2. Call a function as a statement

When a function appears as a statement, it can be called as a statement. The example code is as follows:

 

3. Call a function as an argument

When a function is used as an argument to another function, the function is required to have a return value. The example code is as follows:

 

In the above statement, the return value of the function max () is used as an argument to the printf () function.

 

3、 Nested call

In C language, the definition of function is independent, that is, one function cannot be defined inside another function.

But when you call a function, you can call another function in one function, which is nested call of function. Next, we use a case to demonstrate the nested call of functions.

Case 1:

 

Operation results:

 

In this case, the max4 () function is invoked in the main () function and the max2 () function is called in the max4 () function.

In order to better understand the execution process of this program, we describe it through a diagram:

 

This figure shows that the program contains three levels of function call nesting, which is divided into nine steps, as follows:

(1)   Execute the beginning of the main() function;

(2)   When a function call statement is encountered, call the max4() function, and the process turns to the max4() function entry;

(3)   Execute the beginning of the max4() function;

(4)   When a function call statement is encountered, call the max2() function, and the process turns to the max2() function entry;

(5)   Execute max2() function. If there are no other nested functions, complete all operations of max2() function;

(6)   Returns the location of the max4 function in the max2 () function.

(7)   Continue to execute the unexecuted part of max4() function until the end of max4() function;

(8)   Returns the location of the max4 () function in the main () function.

(9)   Continue to execute the rest of the main () function until the end.

&   Learn more: how many layers can you nest when calling a function?

You will certainly ask: “since function nested calls look no different from ordinary calls, can you make infinite function nested calls?”

Unfortunately, how many layers a function can be nested is determined by a data structure called “stack” at the run time of the program.

Generally speaking, the default stack size of programs on windows is about 8KB, and each function call occupies at least 8 bytes. Therefore, under rough calculation, function calls can only be nested about 1000 layers. If the nested functions contain many variables and parameters, the actual value is much less than this number.

Of course, it is impossible to write a thousand layers of nested function calls simply by writing code manually, but a method called “recursion” can easily reach this upper limit.

 

4、 Recursive call

In mathematical operations, we will encounter the situation of calculating the sum between multiple continuous natural numbers.

For example, to calculate the sum of natural numbers between 1 ~ n, you need to first calculate the result of 1 + 2, add 3 to this result, then get a result, add 4 to the newly obtained result, and so on, until you add n to the sum of all numbers between 1 ~ (n-1).

In program development, in order to complete the above functions, we need to use the recursive call of the function. The so-called recursive call is the process of calling itself within the function.

It should be noted that recursion must have an end condition, otherwise it will fall into the state of infinite recursion and will never end the call. Next, learn about recursive calls through a case of calculating the sum of natural numbers.

Case 2:

 

Operation results:

 

In case 2, we defined a getsum () function to calculate the sum of natural numbers between 1 ~ n.

The code in line 9 of the case is equivalent to calling itself inside the getsum () function, which is the recursion of the function. The whole recursion process ends when n = = 1.

Because the recursive calling process of the function is very complex, the whole calling process is analyzed through a legend.

 

This figure describes the recursive call process. In the whole recursive process, the getsum () function is called four times. Each time, the value of n decreases.

When the value of n is 1, all recursively called functions will end one after another in the opposite order, and all return values will be accumulated, and the final result is 10.

 

Finally, whether you are changing careers, beginners or advanced, if you want to learn programming~

[noteworthy] myC / C + + programming learning exchange club![Click to enter]

Q & A, learning and communication, technical discussion, and a great collection of programming resources. Zero basic videos are also great~