MHB Just a question about recursive functions, no code.

  • Thread starter Thread starter carl123
  • Start date Start date
  • Tags Tags
    Code Functions
AI Thread Summary
Efficiency in recursive functions in C++ can be enhanced by preventing unnecessary calls. Tail call optimization is a key feature in some C++ compilers that allows the last recursive call in a function to execute without additional stack memory allocation, effectively transforming recursion into a loop and preventing stack overflow. Additionally, techniques like memoization and dynamic programming can be employed to store previously computed values, which is particularly useful in cases like calculating Fibonacci numbers, where repeated computations can be avoided. These strategies collectively improve the performance and reliability of recursive functions in C++.
carl123
Messages
55
Reaction score
0
What are different ways of ensuring efficiency in a recursive function in C++? i.e. Prevent calling your recursive function when not necessary.
 
Last edited:
Technology news on Phys.org
carl123 said:
Prevent calling your recursive function when not necessary.
Ha-ha. If you do any computation that is unnecessary, then you are a... strange programmer.

Some C++ compilers do tail call optimization. Then if the recursive call is the last thing a recursive function does, no memory is allocated on the stack, unlike in a normal function call, and the recursion behaves as a loop. This is important because otherwise a recursive function can easily run out of stack space.

Sometimes it makes sense to store values already computed by a recursive function to avoid re-computing them. Fibonacci numbers is one example. Memoization and dynamic programming are two techniques that store intermediate results.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
16
Views
2K
Replies
11
Views
1K
Replies
5
Views
12K
Replies
2
Views
1K
Back
Top