DEV Community

Cover image for Recursion in Programming
Denismacharia
Denismacharia

Posted on • Edited on

Recursion in Programming

As a programmer, you must have or will come across tasks that lead to the need for new similar lines of code. The new tasks may appear smaller or may need the repetition of smaller similar commands to handle them. They may require adding other lines of code to terminate them, which leads to increase in the size of the codebase. You can eliminate all this by implementing recursion in your programs. Let's dive into recursion and see what it's all about.

What is Recursion

Recursion is the process where a function calls itself directly or indirectly. Recursive functions solves problems by calling itself and breaking down original problems into smaller subproblems. When using recursion, you need to define an exit condition to prevent the functions from being called infinitely. The condition that when met terminates the function is known as the base condition.
Recursion is a concept rooted in a number of programming languages as we'll see.

Why choose recursion?

One of the main advantages of using recursion is that it reduces the complexity of different tasks and reduces the time spent writing and debugging. It leads to code that is easy on the eye, looks elegant and allows faster completion of tasks. Code that uses recursive functions is significantly less bulky than code that doesn't use it. However, it uses more memory since there is an extensive overhead since the function constantly updates and maintains the stack.

Implementing recursion in different programming languages.

Recursion basically works in a similar manner in different languages. However, since each language is unique, there are differences in how recursion is applied. Let's look at how each language implements it using a similar example.

Recursion in JavaScript

In JavaScript, the syntax for a recursive function is as shown below;

function recurse() {
if(condition) {
// stop calling itself
} else {recurse();
}
}
Enter fullscreen mode Exit fullscreen mode

Let's see it in a practical example. The program is designed to count down numbers to 1.

function countDown(number) {
console.log(number);
// displays the number

const newNumber = number - 1;
// subtracts one from the number

if (newNumber > 0) {
countDown(newNumber);
}
}
// the base case

CountDown (4);
// output is
// 4
// 3
// 2
// 1
Enter fullscreen mode Exit fullscreen mode

In the above JavaScript program, you pass the number as an argument when calling the function. The value of the number reduces by 1. The function countdown() is called until the base function newNumber > 0 reaches 0 and terminates the function.

Recursion in Python

Python also uses recursion to write efficient and mathematically elegant code. The syntax for Python recursive functions is as shown;

def func():
 if condition: 
# stop calling itself

else: 
# recurse

func= 
Enter fullscreen mode Exit fullscreen mode

Below is a Python program for the example we are using.

def tri_recursion (k):
if (k>0): 
result = k+tri_recursion(k-1)
print(result)
else:
result=0
return result

print("\n\nRecursion Example Results")
tri_recursion(4)

# Output will be
# 4
# 3
# 2
# 1
Enter fullscreen mode Exit fullscreen mode

The function runs itself until the number gets to zero and terminates the program.

Recursion in C++

Similar to other programming languages, recursion in C++ works by calling itself over and over again. Its syntax is as shown below;

void recurse()
{ if (condition) {
# base case

return;
}
else {
}}
# recursive case

Enter fullscreen mode Exit fullscreen mode

Now let's see it in a practical example;

#include <iostream>

void display(int n) {
  if (n == 0) {
    return;
  }
  std::cout << n << std::endl;
  display(n - 1);
}

int main() {
  display(5);
  return 0;
}

# The output is
# 5
# 4
# 3
# 2
# 1
Enter fullscreen mode Exit fullscreen mode

Conclusion

Recursion is an essential skill that assists in reducing the bulk of the codebase while increasing the ease of writing and reading code. However, you need to be careful when using recursion since there is a risk of infinite loops and programs that use too much memory and processing power.

I hope you have learnt something from the article. Find me on twitter @devdenis_. Thanks for reading.

Top comments (0)