I am not able to understand the recursion of functions. How it works? how the values are stored and all?

后端 未结 4 1009
名媛妹妹
名媛妹妹 2021-01-28 04:19

I am not able to understand the recursion of functions. How it works? how the values are stored and all?

int tree_size(struct node* node) { 
  if (node==NULL) {         


        
4条回答
  •  遥遥无期
    2021-01-28 05:01

    When entering a function, a new stack frame is created (on the stack in memory). The stack frame keeps track of the local data within that function, such as locally defined variables and incoming arguments. (And other things such as return address and previously stored register values that must be preserved. However this is not as relevant for this question.)

    With recursion, when you call a function from within the same function , you create a new stack frame (as with any call), and that new stack frame will store the local variables for the new call.

    As C. Stoll pointed out, the order of the two calls is unspecified, due to the + operator.

提交回复
热议问题