Void, Recursion | Functions
C Basics

Course Content

C Basics

## C Basics

1. Introduction
2. Data
3. Operators
4. Control Statements
5. Functions
6. Pointers

# Void, 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.

Main

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.

Main

### 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.

Main

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`.

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?