Data Structures Using C++: Illustration of Recursive Function Calls (Call Stack)
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about how recursion works in memory and why output appears in an unwinding order.
Recommended by 6 people across 6 different months, 2013 to 2020.
What it is
A short screencast from ReelLearning, part of a C++ data structures series, that illustrates what happens in memory on each recursive function call. It draws the individual stack frames on the call stack as calls are pushed and then popped when the base case is reached.
What people said
Recommends user2884789 · 2013-10 · Stack Overflow · on “Recursively Visiting a node in a binary tree”
I found this really good YouTube video on recursive calling and the call stack. This clarified my question above. Here is the link: [this video]
Recommends brokenfoot · 2014-04 · Stack Overflow · on “Memory Allocation for Recursive Functions”
It is the same as calling any other function. The variables (not reference-passed ones) get allocated on the stack. When the functions start to return (i.e. base case is reached), the stack gets popped out in the order it was pushed to the stack over each nested recursive call. This youtube video explains the call stack for recursive functions really well: [this video]
Recommends dguan · 2014-08 · Stack Overflow · on “fibonacci recursion ruby explanation”
If you know stack frame, you can better understand recursion. Let's take a simpler x = Fib(3) to show the stack frame change. (1) When calling Fib(3), Fib function's stack is like this with 3 as parameter: | | | 3 | (2) when Fib(3) went to the line Fib(n-1) + Fib(n-2), the stack is like this: | | | 2 | | 3 | (3) then this Fib(2) is evaluated into Fib(1)+Fib(0), the stack is like this: | 1 | | 2 | | 3 | (4) fib(1) returns the value 1, now it's fib(0)'s turn to be evaluated: | 0 | | 2 | | 3 | (5) Fib(0) returns 0, now Fib(2)'s value is 1 and is returned to Fib(3), | 1 | now Fib(3) need another part, Fib(1), the stack is like this: | 3 | (6) Fib(1) returns 1, now Fib(3) is evaluated as 2, and g
Recommends Edmund Lee · 2014-12 · Stack Overflow · on “Ruby recursion's behavior”
OK. So I found my answer. Thanks everyone for helping. I found the answer from this short video that explains how recursive function works in C++, which I believe is a similar process. This is for future reference for anyone looking for this question (turned out searching for "recursive function" instead of being specific to ruby will lead me to lots of answer). [this video] Because of how the recursive call stack works, it will start unwinding after the call stack reaches the base case. That's why in my code, p "---------" is called twice, and why t = 2 first, then t = 1 in the output. Correct me if I am not understanding this correctly.
Recommends user8652048 · 2018-04 · Stack Overflow · on “Using SymPy, recursive function returning "None" value”
Thanks @PaulRooney! My mistake was was here: else: newtons_method(approx) When you're calling a recursive function, you need to have it return itself every call; otherwise, it will return None . It should've been: else: return newtons_method(approx) I found this video explaining the "call stack" Paul mentions in our discussion. I watched it and it gave me some insight as to what my function was doing logically: [this video]
Recommends Ziv · 2020-08 · Stack Overflow · on “Why does this recursive sum function not work correctly?”
The error you are seeing is from this line: sum(n-1)+n; sum is a function that returns void , but you are trying to add it with an integer n. I am not quite sure what that get_int("f") does, but I assume it's prompting to the user for an int input, and you are trying to sum from 0 to that number. Here is the solution: int sum(int n) // Here is the critical change to your code, now it returns an int { if (n == 1) // Root case that will stop the recursion, otherwise, it's endless return 1; // 1 = 1 + 0; return sum(n-1) + n; // Recursion } Think about what we are trying to achieve here. We want to add from 0 to n, or to say from n to 0 downwards. If n is 3, it's going to be 3+2+1+0, and you'll
Cites Lakshmi Balan · 2016-12 · Stack Overflow · on “Python Factorial Recursion function”
You are using Recursion function for calculating factorial value. So when you reach this else statement, else: res = n * factorial(n-1) the control goes to calling the function "factorial(n-1)" again instead of executing next statements. So, when its called again the statement, print("factorial has been called with n = " + str(n)) will get printed. Since ,"Stack" data structure is used behind this recursion function. Thus whenever the control goes for function calling statement, the previous state of the program will be pushed into the stack, and popped one by one in a "LIFO" manner. That's why the reason for the output. See these 2 links. You will understand it better. [this video] [LINK]