Understanding C Language: Recursive Functions

This is the 21st article in the introduction to C language.

A function has a definition and a call; defining a function tells the system what functionality the function has, while calling it is equivalent to implementing that functionality.

Defining a function is like writing a script, telling the director what the play is about.

Calling a function is like filming a movie, showcasing the story of the script.

The syntax format for defining a function is:

DataType FunctionName( ParameterList )

{

FunctionBody;

}

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

The syntax format for calling a function is:

FunctionName( );

Or:

FunctionName( ActualParameterList );

If there are no parameters defined in the function, there will be no corresponding actual parameters when calling it.

If there are parameters defined in the function, there should also be corresponding actual parameters when calling it.

Defined parameters must be variables, while actual parameters can be constants or variables.

A function can be called by other functions and can also call other functions.

1. Ordinary Function Call

Let’s look at a simple program.

Program 1:

Understanding C Language: Recursive Functions

Function f2 calls function f1, and the main function calls function f2.

Program output:

Understanding C Language: Recursive Functions

When calling a function, the program transfers to the called function, executes the statements inside, and then returns to the calling function after completion.

2. Recursive Function Call

In Program 1, function f2 calls function f1, and function main calls function f1. They are all calling other functions.

When a function calls itself, it is called a recursive call.

A recursive call is when a function calls itself.

Let’s look at an example:

There are 5 people sitting together, and Teacher Wang asks the person on the far right (let’s call him the 5th person) how old he is.

The 5th person says he is 2 years older than the person sitting to his left (let’s call him the 4th person).

So Teacher Wang asks the 4th person how old he is. The 4th person says the person sitting to his left (let’s call him the 3rd person) is 2 years older.

Teacher Wang asks the 3rd person how old he is. The 3rd person says the person sitting to his left (let’s call him the 2nd person) is 2 years older.

Teacher Wang asks the 2nd person how old he is. The 2nd person says the person sitting to his left (let’s call him the 1st person) is 2 years older.

Teacher Wang asks the 1st person how old he is. The 1st person looks to his left, sees no one, and can only answer that he is 8 years old this year.

Question: How old is the 5th person?

To calculate the age of the 5th person, we need to:

Age of the 5th person = Age of the 4th person + 2

Age of the 4th person = Age of the 3rd person + 2

Age of the 3rd person = Age of the 2nd person + 2

Age of the 2nd person = Age of the 1st person + 2

Age of the 1st person = 8

A recursive function is like the nested dolls above, one layer inside another, until the innermost doll is found.

If the function int age(int n) can determine the age of the nth person, then if n equals 1 (the 1st person), we can directly know that his age is 8 years.

If n is greater than 1, then the age of the nth person equals the age of the (n-1)th person + 2.

Understanding C Language: Recursive Functions

If we use a function age(n) to represent the age of the nth person, then the above formula can be expressed as:

Understanding C Language: Recursive Functions

The function age has a return value, which is the age of the nth person.

Therefore, the function type is int, and the function age is defined as follows:

Understanding C Language: Recursive Functions

return returns an integer value, which is the age of the nth person.

When n equals 1 (the 1st person), it returns 8; otherwise, it returns the age of the previous person + 2.

The complete program is as follows:

Program 1:

Understanding C Language: Recursive Functions

Program 1 output:

Understanding C Language: Recursive Functions

The 5th person is 16 years old.

Let’s look at another example, input an integer n, and calculate n!

n! refers to the factorial of n, which is the product of all integers from 1 to n.

This program can also be implemented using a regular loop function without using a recursive function.

First, we will implement it using a regular function.

Program 2:

Understanding C Language: Recursive Functions

We write the calculation of n! in a function fact, which returns the factorial of the parameter n.

In the main function, we call the fact function; for example, to calculate the factorial of 5, the call statement is: fact(5).

However, the factorial can also be calculated using a recursive function.

We know that the factorial of 0 is 1, and the factorial of 1 is also 1.

When n equals 0 or n equals 1, the factorial of n is 1.

If n is greater than 1, then the factorial of n equals n multiplied by the factorial of (n-1).

Thus, the formula for calculating the factorial is:

Understanding C Language: Recursive Functions

If we use the function fact(n) to represent the factorial of n, then the above formula can be expressed as:

Understanding C Language: Recursive Functions

The function fact has a return value, which is the factorial of n.

The function type is defined as int, and the function fact is defined as follows:

Understanding C Language: Recursive Functions

Changing the definition of the fact function in Program 2 to a recursive function will yield the same result.

If we need to input an integer in the main function to calculate the factorial of that integer, we can write it as:

Program 3:

Understanding C Language: Recursive Functions

Output result:

Understanding C Language: Recursive FunctionsInput 5, the factorial of 5 is 120.

However, when the input integer is very large, for example, if the input integer is 100, let’s see the output result:

Understanding C Language: Recursive Functions

If the input is 32, the output is:

Understanding C Language: Recursive Functions

The factorial of 100 is 0, and the factorial of 32 is negative? This is clearly incorrect.

This is because the factorial of 32 is too large and has exceeded the value range of the int type, resulting in an overflow error.

This is equivalent to the int type room, which has a minimum and maximum range for the numbers it can hold; exceeding this range means it cannot hold the number, and the room is too small, so a larger room is needed to accommodate the number; otherwise, an overflow error will occur.

Therefore, we should define the function fact as double (double precision type), so that there will be no problem.

Understanding C Language: Recursive Functions

Output result:

Understanding C Language: Recursive Functions

We see that the output result has 6 decimal places; in fact, factorials do not have decimal places, so we can change the format specifier %f corresponding to output x in the program to %.0f to keep 0 decimal places, i.e., no decimal places.

3. Conclusion

A function can call any function and can also call itself.

A function that calls itself is called a recursive function.

Leave a Comment