-4

In recursive function developed using java is below.

public class Base {
    public static void main(String[] arg) { 
        m1(4);      
    }

    public static void m1(int num) {    
        if(num>=1) {
            System.out.println("----A "+num);
            return num*m1(num-1);
        else {
            return 1;
        }
    }   
}

Here, when calling m1(num-1), how num value is assigned to num-1. Example, num =4 , next time num value will be num =3. I mean here , we wont assign as num = 3 although we call m1() function. When calling function m1(), it is stored in the stack, so it will become f(3). I can understand that. But I am confused how num value is assigned to a new value.Could you please explain?.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Vinee-the-Pooh
  • 835
  • 3
  • 10
  • 27
  • 2
    There's more than one variable called `num` on the stack, specifically one instance per stack frame/recursive call. – nanofarad Apr 20 '20 at 03:45
  • 1
    Not only function stored in the stack, after every call new function with new variable is stored in stack. – Eklavya Apr 20 '20 at 03:52
  • 1
    Did you do your **research**, e.g. a *web search* for [`How recursive works`](https://www.google.com/search?q=How+recursive+works). There are many good articles on the subject, with nice diagrams to help you understand. Why did none of those answer your question? What more did you need? Said another way, without you explaining why those were not good enough, why would you expect any answer here not to be just a rehash of the same information? – Andreas Apr 20 '20 at 04:05
  • [Recursion?](https://stackoverflow.com/q/61314797/2970947) Click [here](https://stackoverflow.com/questions/61314797/how-recursive-function-works-internally#comment108469436_61314797) to learn more. – Elliott Frisch Apr 20 '20 at 04:09
  • LOL @ElliottFrisch – Andreas Apr 20 '20 at 04:10
  • @ElliottFrisch But don't click too often, otherwise you get to Stack Overflow. – akuzminykh Apr 20 '20 at 04:11
  • @akuzminykh Not if your p̶r̶o̶g̶r̶a̶m̶m̶i̶n̶g̶ ̶l̶a̶n̶g̶u̶a̶g̶e̶ browser has [optimized tail calls!](https://en.wikipedia.org/wiki/Trampoline_(computing)) – Elliott Frisch Apr 20 '20 at 04:13
  • 1
    *"But I am confused how num value is assigned to a new value."*, I think you have a misunderstanding here. When you call a function, you don't pass the variable itself but the *value* of the variable. This value is assigned to the parameter. Each function call has its own parameters. The "names" are the same but they are different `num`s. – akuzminykh Apr 20 '20 at 04:18

2 Answers2

1

If I interpreted your question correctly, you're not really confused about the recursion part, you're more confused about how the value num gets assigned to num - 1 part. The answer to this is that num is never being reassigned in the first place, at least not in its own lexical scope.

What that basically means is that there is a different value of "num" in each function call of m1() on the call stack (they will be, in this case, 4, 3, 2, 1, and 0 before the recursion terminates). The variable num is not reassigned in the function call because "num - 1" itself is just an expression, not an assignment. The value of num on each function call on the call stack is actually a different "num" variable all together, and this has to do with the fact that java is "pass by value".

In general, if you're looking to brush up your understanding of recursion, highly suggest checking out the book "The Little Schemer".

0

When you call a function/method, the values of the passed arguments are copied and put into the corresponding parameters. When you have a function f(int x) and call it like f(argumentInteger) then the value of argumentInteger is copied and assigned to x.

A recursive function call is not different from a normal function call. You pass an integer, a value, and this is what is assigned to the corresponding parameter.

A function call gets its own stack frame that is a part of the memory for the functions parameters, variables and other things.

The variable num is not "put" into the stack frame of the recursively called function. It is copied, or rather the value is taken, and the copy goes into the other stack frame. So per function call you have a different num. Don't misunderstand that just because the name is always the same.

akuzminykh
  • 4,522
  • 4
  • 15
  • 36