- Related Questions & Answers
- How can we create recursive functions in Python?
- C++ Program for Recursive Bubble Sort?
- C Program for Recursive Insertion Sort
- C++ Program for the Recursive Bubble Sort?
- Binary Search (Recursive and Iterative) in C Program
- C Program for Binary Search (Recursive and Iterative)?
- Replacing space with a hyphen in C++
- C# Program to get free space in a drive
- A Space Optimized Solution of LCS in C Program?
- Python Program for Recursive Insertion Sort
- Java Program for Recursive Bubble Sort
- Java Program for Recursive Insertion Sort
- Java Program for Binary Search (Recursive)
- Program for average of an array(Iterative and Recursive) in C++
- Reverse a String (Recursive) C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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) ---> fact(1)

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).

Advertisements