Void, RecursionVoid, Recursion

Void

In C, functions that are declared with a specific return type must contain a return statement to return a value of the appropriate type. But there are times when your functions don't have to return anything, for example, the function just prints text to the screen. For such cases, the function must be of type void:

c

Main.c

The keyword void is used to indicate the absence of a function return value or in pointers. When you try to create a variable of type void, you will get an error because the compiler will not be able to "identify" how much memory to allocate:

Mistake made to show example

c

Main.c

Recursion

The C language allows function to call itself. This technique is used for recursion.

You can think of recursion as stretching and tightening a rubber band. Recursion can be used for mathematical calculations, such as factorial.

3! = 3 * 2 * 1

Factorials are used, for example, to approximate the values of functions by expanding these functions into a Taylor/Maclaurin series.

Note

The author began to write this chapter immediately after passing the laboratory work at the university. In the laboratory work, it was required to find the approximate value of the cotangent with a predetermined accuracy, expanding it into a Taylor series, and, accordingly, the factorial could not have been done without.

c

Main.c

In this case, the function will call itself until the condition n == 0 || n == 1. When the condition is met, the values obtained from new calls to the factorial() function are returned in reverse order, like an accordion. When step 6 is reached, the first call to factorial(3) returns 6.

question-icon

You see an advertisement with a girl holding a bottle of milk featuring an image of a girl holding a bottle of milk with an image of a girl... How can we name this?

Select the correct answer

Everything was clear?

Section 5. Chapter 6