The Tech Demystify: Recursion

Ismaeel Shuaib
Facebook Developer Circle: Accra
2 min readAug 3, 2020

In this episode of The Tech Demystify, we have Solomon Appier Sign (a backend developer @ Payswitch & a program manager @ TTD) using examples to explain the concept of recursion.

What is Recursion?

“Recursion or recursive function is a function that calls itself either directly or indirectly until the base condition is met.”

Solomon likened recursion concept to loops (while loops or for loops). He explained further that the recursive functions have conditions that have to be met so the loop breaks.

Components of a Recursive function

  1. Base condition
  2. Recursive case (i.e. the function calling itself)

The base function is similar to the condition in a loop. It has to be true so that the recursive call ends. If the base function is never met, it ends up in a stack overflow and keeps running.

The snapshots below will help you understand the recursive function;

  1. Without recursion
Output is 3

2. With Recursion

Output is 3

3. An alternative way to use the recursive function

Output is 40

In the loop, the initial value is set in the function whiles in the recursive function, the initial value is set as part of the parameters passed to the function.

Solomon ended this session by answering some pressing questions on recursion and recursive functions.

Useful resources are available on:

Video

#TheTechDemystify is in every Sunday at 7 pm on Facebook Group, make sure to tune in and enjoy every reverting conversation as well as tutorial sessions to help you grow in tech.

--

--