Implementing recursive algorithms


As you might have already guessed, recursion is implemented via nested function calls, as we learned earlier in this course. However, a recursive function calls itself instead of calling some other function. Now that we know about stack frames and stack memory, it is easy to visualize how these recursive function calls work under the hood.

Continuing our ATM queue example, let us look at how a recursive function can implement the recursive relation. Here n is the number of people from the front of the queue up to and including us (so calling findPosition(4) corresponds to a queue where we are the 4th person).

  1. C++

  2. Java

  3. Typescript

  4. Javascript

  5. Python

The recursive function call in the code above works in three steps to return the result.

Liking the course? Check our discounted plans to continue learning.