Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive functions in C/C++

Tags:

c++

c

recursion

If we consider recursive function in C/C++, are they useful in any way? Where exactly they are used mostly? Are there any advantages in terms of memory by using recursive functions?

Edit: is the recursion better or using a while loop?

like image 756
Vijay Avatar asked Feb 16 '10 10:02

Vijay


1 Answers

Recursive functions are primarily used for ease of designing algorithms. For example you need to traverse a directory tree recursively - its depth it limited, so you're quite likely to never face anything like too deep recursion and consequent stack overflow, but writing a tree traversal recursively is soo much easier, then doing the same in iterative manner.

In most cases recursive functions don't save memory compared to iterative solutions. Even worse they consume stack memory which is relatively scarse.

like image 191
sharptooth Avatar answered Oct 18 '22 02:10

sharptooth