Auxiliary Space with Recursive Functions in C Program?

Here we will see how the auxiliary space is required for recursive function call. And how it is differing from the normal function call?

Suppose we have one function like below −

long fact(int n){
   if(n == 0 || n == 1)
      return 1;
   return n * fact(n-1);

This function is recursive function. When we call it like fact(5), then it will store addresses inside the stack like below −

fact(5) --->
fact(4) --->
fact(3) --->
fact(2) --->

As the recursive functions are calling itself again and again, addresses are added into stack. So if the function is called n times recursively, it will take O(n) auxiliary space. But that does not mean that if one normal function is called n times, the space complexity will be O(n). For normal function when it is called, the address is pushed into the stack. After that when it is completed, it will pop address from stack and come into the invoker function. Then call again. So it will be O(1).

Updated on: 20-Aug-2019


Kickstart Your Career

Get certified by completing the course

Get Started