- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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) ---> 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).