Bifalgorithm post · Computer science one · Recursion
How can recursion be made tail-recursive in C?
How can recursion be made tail-recursive in C? (Computer science one · Recursion) — "This example was not tail-recursive as the recursive call was not the fina…
Ideas in this flowchart
- Recursive C functions: tail-recursive summation and naive Fibonacci
- Tail-recursive summation with recSumTail
- The original summation example is not tail-recursive because the sum occurs after the recursive call
- The accumulated sum is passed through each call before the recursive call
- When i equals size, the function returns sum
- Otherwise, the function calls recSumTail with i+1 and sum + arr[i]
- Naive recursive Fibonacci implementation
- For negative n, fibonacci returns 0
- For n less than or equal to 1, fibonacci returns 1
- For larger n, fibonacci returns fibonacci(n-1) + fibonacci(n-2)