DEV Community

Olaniyi Philip Ojeyinka
Olaniyi Philip Ojeyinka

Posted on • Updated on

The Jude way to recursion

So before we start,let me introduce to you ,Jude ,jude has been sleeping for the last couple of hours and it somehow seems unusual of him,so we brought out one of our many imaginary tools to detect any irregularities in someone
sleep and we detect at that moment that, he is dreaming and also in the dream he slept and also find him self dreaming about
himself sleeping and and also dreaming ......
So we went on to detect what exactly our dear jude is dreaming,and realised he as been dreaming about owning a tesla
and at the lowest level of his dream he owns a higher model of the car as the level of the dream get higher he owns a lower model of the car.

Alt text of image

D1-Dream at level one
D2-Dream at level two
D3-Dream at level three
D2-Dream at level four

While he continues dreaming we hope he didn't enter an unending dream cycle and he get up soon(When the base case is met).

Thats Enough Story, I think what happened to jude can be said to be a recursive(adjective of recursion) dreaming so what is
recursion ? recursion is just the act of defining a function or an object in terms of itself that is calling a fuction or
object right inside of its definition.And according to wikipedia its said to be a function defined in terms of simpler and often
smaller versions of itself.
Well, You may be thinking "what the fuck do we need recursion for" recursion can be used to perform repetitive task just like iteration loop
,although not a perfect replacement or alternative to iterations because it could require more space since each function calls are stored in the Stack before reaching the base
case.
Stack? oh whats that?according to wikipedia, a stack is abstract data type that serves as a collection of elements,with two principal operations
Push(add an element) and Pop(remove element).The operation may be performed in two different ways which are

First in Last out
and
Last in First Out

Just see it as a storage container or even think of it just like a stack of book as in picture below

Stack of books

and clearly the example above follow the last in first out order because if we are to take from the book stack
safely without having anyone of the stack fallen ,we have to start from the top.

In this article, we will be using recursion to calculate the power of a number and implementation will be in
JAVA.

//code

...

class Test{


    public int repeat(int x, int n){

        if (n == 0) {
            return 1;
        }
        return  x*repeat(x,n-1);


    }
    public static void main(String[] args){
        Test test = new Test();
        System.out.println( test.repeat(5,3));

    }
}
Enter fullscreen mode Exit fullscreen mode

...

Explanation :

Alt text of image

Starting from the left to right,when we make a call to the powerOf() the first time its details were saved in the stack and as we move up (check the up arrow),it continue saving the simpler version of function into the stack memory until the base case is reached and then return the resulting value to the next stack (Check the down arrow) until main stack is reached where result is returned.

Find any error? Please let me know in the reply section.Anyways just finished my first article.

Top comments (0)