Go Recursion

Learn via video courses
Topics Covered

Overview

Recursion refers to an event when a function calls itself directly or indirectly. When using recursion in coding every call's scope will be reduced and the result will converge at some point with the help of the base condition which is the final executable statement in recursion and stops any further function calls.

Introduction

In Golang, Recursion is achieved using functions. We can have a function calling itself to create a recursive function.

Pseudo-code:

The above function will print the greetings infinitely as there is no base condition. A base condition is the criteria when the execution of recursion must stop.

In programming, recursion is used at the places where a large problem can be broken down into smaller subproblems, and the solutions of subproblems ultimately solve the main problem.

Classification of Recursion

Direct and Indirect Recursion

Direct Recursion:

Direct recursion is the state of function in which it calls itself and does not need any assistance from any other function. this means the function will call itself directly. Here the base condition is n==1, once n = 1 then it will start returning, and the function call ends.

Output:

Example for Direct Recursion : playground link

Indirect Recursion:

Indirect recursion is the state of function in which it calls itself and needs assistance from other functions so that it will be executed. this means the function will call indirectly using other functions.

Output:

Example of Indirect recursion: playground link

Finite and Infinite Recursion

Finite Recursion:

Finite recursion is the state of function in which it calls itself and stops its execution at a boundary or base condition.

Output:

Example for Finite recursion: playground link

Infinite Recursion:

Infinite recursion is the state of function in which it calls itself and its execution does not have a boundary condition. This makes the function run infinitely.

Output:

Example for Infinite recursion: playground link

Tail Recursion and Head Recursion

Tail Recursion:

Tail recursion is the state of a function in which it calls the function at the end of the recursive function. As the recurring call is the last thing in the function it is referred to as Tail recursion.

Output:

Example for Tail recursion: playground link

Head Recursion

Head recursion is the state of the function in which it calls the function at the start of the recursive function. As the recurring call is the first thing in the function it is referred to as Head recursion.

Output:

Example for Head recursion: playground link

Recursion Using Anonymous Functions in Golang

In Golang, We can implement recursion using anonymous functions, Anonymous functions are those functions in golang that do not have a name.

Output:

Example for Recursion using anonymous functions in Golang: playground link

Example of Recursive Go Programs

Conclusion

  • Understood what is Golang recursion.
  • Explained classification of recursions.
  • Implemented examples in Golang for recursion.