---
title: "C++ Recursion Tutorial"
url: https://weworkworldwide.com/tutorials/recursion-2/
description: "C++ Recursion Tutorial: In this section we're assuming you already know what functions, stack & heap are."
date: 2026-10-30T13:00:10+00:00
source: https://weworkworldwide.com/llms.txt
---

# C++ Recursion Tutorial

“To understand recursion, one must first understand recursion” Stephen Hawking

In this section we’re assuming you already know what *functions*, *stack & heap* are.

The idea of recursion is to allow one function call itself.

How?

Simply within the body of the target function, call the function.

Example:

``` line-numbers
#include <iostream>
using namespace std;
int multiply(int);
int main() {
int res = multiply(4);
cout<<res;
return 0;
}
int multiply(int value){
if (value !=0){
return multiply(value -1) * value;
}else{
return 1;
}
}
```

Output: 24

Within the body of the `main` function, we called the `multiply()` function with the value of 4.

The first time we called this function, the assigned value was 4.

Within its body, we have an `if-statement` which is in charge of checking if the argument of the function is not zero.

For this call, the result of this condition is true (the argument is 4) and so the body of the `if-statement` will run.

Within this statement we have the `return` keyword with the expression of ` multiply(value -1) * value`.

In order to resolve the expression, we need to call the `multiply` function with the value of `value-1` so it is basically `multiply(3)` function.

As mentioned before in *stack&heap section*, when a function is called within the body of another function, the CPU will stop running the first function and will jump into running the body of the second function.

So here the first function call will stop running until the result of the `multiply(3)` function is declared and retuned.

## multiply(3):

The first line of this function was to check if the input is not 0, and as you can see the value is 3 so the condition is true and its body will run.

Within the body of the statement we have the expression of ` multiply(value -1) * value` which is equal to: `multiply(2) * 3`. So to solve the expression, the CPU will stop running the current function and will jump to running the `multiply(2)` function and gets the result of the function call.

## multiply(2):

Again the first line in the function is to check the value of the argument and because this argument is not 0, the body of the statement runs.

The expression in the statement will be: ` multiply(value -1) * value`, which is equal to: ` multiply(1) * 2`. And again in order to resolve the expression, a call to the `multiply(1)` should happen.

## multiply(1):

In this call because the value of the argument is still non-zero, the body of the `if-statement` will run and so we will have this expression within the statement: ` multiply(0) * 1`.

Again, to solve the expression, another call to the `multiply()` function with the value of 0 should happen.

## multiply(0):

This time because the value of the function (argument) is 0, the condition of the `if-statement` won’t run and so the body of the `else` statement is the one to run instead.

Within the body of this statement, we have the `return 1` which will cause the `multiply(0)` to terminate with the value of 1 as its final result.

So now one function `multiply(0)` resolved and it is off the stack.

Note: so far we start with `main()`, then went to `multiply(4)`, `multiply(3)`, `multiply(2)`, `multiply(1)`, and here `multiply(0)`. Basically we have a stack of 6 function with the `multiply(0)` is off the stack now, the remaining functions will be 5.

## Back to the multiply(1):

The function was at `return multiply(0) * 1` statement when the call to the `multiply(0)` happened. Now because the result of the call is declared, the function will be replaced with its value and the final expression will be: `1*1`.

So the value that `multiply(1)` will return is 1.

## Back to the multiply(2):

We know that the call to the `multiply(1)` happened within the body of the `multiply(2)` in the expression of: ` multiply(1) * 2`. So now that the value of the `multiply(1)` is cleared, the function will be replaced with its returned value which is 1 and the final result of this expression at this point is now clear which is 2. (`1*2`)

## Back to the multiply(3):

The call to the `multiply(2)` happened within the body of the `multiply(3)` in the expression of: ` multiply(2) * 3`. And now that the result of the call to the `multiply(2)` is cleared, this expression can be resolved as well.

So the final expression is: ` 2 * 3` which results 6 and that’s the value that will be returned to the caller function.

## Back to the multiply (4):

This function was the first call from the `main()` function and it has the expression of : ` multiply(3) * 4` that caused the call to the `multiply(3)`.

Now that the result of the `multiply(3)` is cleared. This expression can also be resolved.

So the final expression is: `6 *4` which is equal to 24. And the value 24 is the one that will be returned to the called function which is `main()` in this case.

## Back to the `main()` function:

``` line-numbers
This line of instruction was the reason to call the `multiply(4)` function: `int res = multiply(4); `
```

And now that the result of the `multipl(4)` is clear, the result will be assigned to the `res` variable. (So it will have the value of 24 stored in its memory space).

At the next line inside the `main()` function, the values stored in the `res` variable is sent to the output stream (the value is 24) and in the line after this, is the call to the `return 0;` which is equal to the termination of the `main()` function and the program as a result.

Note: the value 0 as mentioned before in the *stack&heap* section will be sent to the Operating-System.

## A couple notes:

1.  Recursion is about simplifying a process. For example if someone  
    asked to add a range of numbers together (for example 0 to 100). We can  
    simply use recursion just like in the example above and get the total  
    result.

2.  Recursion is somewhat difficult to understand at first but if you  
    keep running other types of examples it’ll soon get easy to  
    understand.

3.  If you don’t pay attention, a function can call itself  
    infinitely. Of course because the memory is limited, a function might  
    call itself multiple times and then the program simply crashes because  
    it ran out of memory which also known as `Stack overflowed`.

So make sure there’s a condition in your function that finally will stop letting a function to call itself.

Another example:

``` line-numbers
#include <iostream>
using namespace std;
void printNumber(int);
int main() {
printNumber(100);
return 0;
}
void printNumber(int value){
cout<<value<<" ,";
if (value>0){
printNumber(value-1);
}
}
```

Output:

100 ,99 ,98 ,97 ,96 ,95 ,94 ,93 ,92 ,91 ,90 ,89 ,88 ,87 ,86 ,85 ,84 ,83 ,82 ,81

,80 ,79 ,78 ,77 ,76 ,75 ,74 ,73 ,72 ,71 ,70 ,69 ,68 ,67 ,66 ,65 ,64 ,63 ,62 ,61

,60 ,59 ,58 ,57 ,56 ,55 ,54 ,53 ,52 ,51 ,50 ,49 ,48 ,47 ,46 ,45 ,44 ,43 ,42 ,41

,40 ,39 ,38 ,37 ,36 ,35 ,34 ,33 ,32 ,31 ,30 ,29 ,28 ,27 ,26 ,25 ,24 ,23 ,22 ,21

,20 ,19 ,18 ,17 ,16 ,15 ,14 ,13 ,12 ,11 ,10 ,9 ,8 ,7 ,6 ,5 ,4 ,3 ,2 ,1 ,0 ,
