What is the relationship between recursion functions and memory stack?

Posted by Eslam on Stack Overflow See other posts from Stack Overflow or by Eslam
Published on 2012-12-12T22:24:52Z Indexed on 2012/12/12 23:03 UTC
Read the original article Hit count: 406

Filed under:
|

is there's a direct relationship between recursive functions and the memory stack, for more explanation consider that code:

public static int triangle(int n) {
    System.out.println(“Entering: n = ” + n);
    if (n == 1) {
        System.out.println(“Returning 1”);
        return 1;
    } else {
        int temp = n + triangle(n - 1);
        System.out.println(“Returning“ + temp);
        return temp;
    }
}?

in this example where will the values 2,3,4,5 be stored until the function returns ? note that they will be returned in LIFO(LastInFirstOut) is these a special case of recursion that deals with the memory stack or they always goes together?

© Stack Overflow or respective owner

Related posts about recursion

Related posts about stack