How does recursion work its way back?

后端 未结 1 1182
长情又很酷
长情又很酷 2021-01-20 17:04

I\'m working to understand recursion more in depth and I\'m struggling with WHY it works the way it does. I know that this function returns the square of the previous return

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-20 17:32

    You're thinking about the steps linearly, while the execution is actually nested (represented by indenting):

    call p1a(3)
        call p1a(2)
            call p1a(1)
                return 2
            return pow(2, 2)
        return pow(4, 2)
    

    So the final return returns the value 16.

    0 讨论(0)
提交回复
热议问题