[Solved] Why this recursive method is considered a factorial? [closed]


Your program return the factorial when m = 1.
Take this example: (m=1, n=4)

4 != 1 -> so you call recursively prod(1, 3) 
    3 != 1 -> so you call recursively prod(1, 2)
        2 != 1 -> so you call recursively prod(1, 1)
            1 == 1 -> you return 1 to the last call
        In this call, recurse = 1 and n = 2 so you return 2 to the upper call
    Here, recurse = 2, n = 3, so you return 6 to the upper call
Here, recurse = 6, n = 4, so you return 24 to the upper call

END OF FUNCTION, result is 24 which is 4!

Every time you are calling the function prod recursively, the current function prodA is paused (saving the values of its variables in memory) to execute the new function prodB until prodB returns something to prodA. Then A continues to execute itself (loading back its values from memory)

solved Why this recursive method is considered a factorial? [closed]