r/learnprogramming • u/Temporary-Warthog250 • Apr 05 '22
Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them
Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.
Someone else who reads yours and does understand it, try to explain it to them to help them learn
1.6k
Upvotes
3
u/AlSweigart Author: ATBS Apr 05 '22 edited Apr 05 '22
I just got done writing a book on recursion. The reason it's mystifying to you is most likely because the tutorials/instructors who taught it didn't explicitly bring up the call stack.
First: A stack is one of the simplest data structures in computer science. It's like a list, but you can only add or remove things to and from the end of the list. We say you "push" data onto the end (or top) of the stack and "pop" data off the top of stack.
Second: Function calls aren't one-way. When you call a function, the program automatically and behind the scenes your program notes where the execution should return to when the function call returns. This data is called a frame object, and frame objects are pushed to the top of the call stack when a function is called and popped off the top of the stack when the function returns.
So if you have a function
a()that callsb()that callsc(), this is how your program knows that whenc()returns, it should go back tob(), and whenb()returns, it should go back toa(). Here's a brief Python program:When we run it, notice it goes A-B-C-C-B-A:
This is why stacks are called a LIFO or last in, first out. The last thing pushed to the stack is the first thing popped off it.
When we call
a(), a frame object (I'll call it a) is pushed to the top of the stack, then whenb()is called a frame object (I'll call it b) is pushed and becomes the new top of the stack. Thenb()callsc()which creates a c-frame object to push to the top of the stack. Whenc()returns, the c-frame object is popped off, then whenb()returns, the b-frame object is popped off, then whena()returns the a-frame object is popped off and the call stack is empty and our program is back in the global scope, outside of all functions. (Or, if you're in a program written in C which has amain()function, returning frommain()means the program terminates.)One thing to keep in mind: the execution is always in the function call at the top of the stack. This is why when
c()returns tob(), the c frame object pops off and the old b frame object under it is now at the top.It's a bit confusing because the call stack handled behind the scenes. You can't point to a place in your source code where the call stack is. It's either automatically handled by the interpreter or the code for it is added to your program by the compiler.
Now, if you have a recursive function, the function is calling itself. So the call stack has frame objects for the same function but that's okay because remember that the frame objects represent function calls and not functions. Say we have a factorial function:
When we call
fact(3), a new frame object (I'll call it fact-3) is popped to the top of the call stack and the execution enters thefact()function. This ends up runningreturn n * fact(n - 1), but before it can do that, it needs to callfact(2).A new frame object (I'll call it fact-2) is created and popped to the top of the stack. This is just like
a()callingb(), except in this case a and b are the same function: it'sfact()callingfact().Here's the other thing that trips people up: each frame object stores a separate copy of the local variables. Rather than think of the local variables like
nas local to the function, they are actually local to a specific function call. This means there are multiplenvariables: in the fact-3 frame objectnis set to3and in the fact-2 frame objectnis set to2. They have the same name, but these are two separate variables storing two separate values.This is a major point of confusion for people learning recursion. Seriously, people are always thrown off because when you look at the code, you only see one
nvariable and it's easy to overlook the fact that there's actually onenvariable for each function call. This is why recursion looks like magic: you expectfact(5)to do 5 x 4 x 3 x 2 x 1 but all you see isn * fact(n - 1)and people don't know where the full multiplication is taking place.Anyway,
fact(2)has to callfact(1)(creating a fact-1 frame object), which returns1. When it returns, the fact-1 frame object is popped off the stack. Now we're back in the previous call tofact(), but it's the fact-2 frame object wherenis set to2. Thereturn n * fact(n - 1)now looks likereturn 2 * 1becausefact(n - 1)returned1and the localnvariable is2.This makes our
fact(2)call return2 * 1, that is,2, and the program (automatically, behind the scenes) pops off the fact-2 frame object and the execution returns to thefact()function again, but in the context of the fact-1 frame object where the localnvariable is (and always has been) set to3. This time thereturn n * fact(n - 1)is reallyreturn 3 * 2. So thisfact(3)function call returns6and pops the fact-3 frame object off the call stack.There's nothing left on the call stack at this point, so the program continues running code from whatever point in the global scope called
fact(3)and thatfact(3)function call has evaluated to6.As for where the 3 x 2 x 1 is taking place, the flow control of
fact()is simple enough that we can substitute the function calls with that call'sreturnstatement: