Recursive function in C

advertisements
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void recursie(int);

int main(int argc, char **argv) {
  recursie(3);
}

void recursie(int a){
  if(a==0){return;}
    recursie(a-1);
    printf("%d",a);
    recursie(a-1);
}

The output is : 1213121. Can someone explain me how i get to this output ?


recursie(3)
  -calls recursie(2)
   - calls recursie(1)
     -calls recursie(0) -> void
     -prints 1
     -calls recursie(0) -> void
   -prints 2
   -calls recursie 1
      -calls recursie 0 -> void
      -prints 1
      -calls recursie 0 -> void
  -prints 3
  -calls recursie(2)
   - calls recursie(1)
     -calls recursie(0) -> void
     -prints 1
     -calls recursie(0) -> void
   -prints 2
   -callse recurse 1
      -calls recusie 0 -> void
      -prints 1
      -calls recursie 0 -> void
end