Recursion in c++ Factorial Program

后端 未结 5 1205
渐次进展
渐次进展 2020-12-05 16:48

hello i have this piece of code that i coded based on some other recursion and factorial programs but my problem is that i am really confused as to how it stored the value a

5条回答
  •  离开以前
    2020-12-05 17:28

    A recursive function breaks a big problem down into smaller cases.

    Going over your program:

    call factorialfinder with 5, result is stored as 5 * factorialfinder(4)
    
    call factorialfinder with 4, result is stored as 5 * 4 * factorialfinder(3)
    
    call factorialfinder with 3, result is stored as 5 * 4 * 3 * factorialfinder(2)
    
    call factorialfinder with 2, result is stored as 5 * 4 * 3 * 2 * factorialfinder(1)
    
    call factorialfinder with 1, result is stored as 5 * 4 * 3 * 2 * 1
    

    in essence it combines the result of a stack of calls to factorialfinder until you hit your base case, in this case x = 1.

提交回复
热议问题