Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Void, Recursion
course content

Course Content

C Basics

Void, RecursionVoid, Recursion

The void Return Type in C

In the C programming language, functions that are declared with a specific return type are expected to have a return statement, delivering a value matching that type. However, there are scenarios where a function doesn't need to return anything – perhaps it's simply displaying text on the screen. For such instances, the function should be declared with the void type.

c

Main.c

The void keyword specifies that a function doesn't return a value, and it can also be used in the context of pointers. Attempting to declare a variable of type void will result in an error because the compiler won't understand how much memory to allocate.

Here's an intentional error for illustration.

c

Main.c

Delving into Recursion

C supports the ability for a function to call itself, a technique known as recursion.

Imagine recursion as the action of stretching and then releasing a rubber band. It's commonly leveraged in mathematical operations, such as calculating factorials.

For instance: 3! = 3 x 2 x 1

Factorials are often utilized in approximating function values by expanding these functions into Taylor or Maclaurin series.

Note

This chapter's inspiration came right after the author completed a lab assignment at a university. The task was to approximate the cotangent value to a certain precision by expanding it into a Taylor series. And naturally, the factorial computation was indispensable.

c

Main.c

In the context of recursion, the function will continue to call itself until it meets the condition n == 0 || n == 1. Once this condition is satisfied, the results from the subsequent factorial() function calls are returned in the reverse order, similar to folding an accordion. By the time step 6 is reached, the initial call to factorial(3) will return 6.

question-icon

Imagine an ad where you see a girl holding a bottle of milk. On this bottle, there's another picture of the same girl holding a bottle of milk, and on that smaller bottle, there's yet another picture of the girl, and so on. What's this concept called?

Select the correct answer

Everything was clear?

Section 5. Chapter 6
some-alt