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
2
u/tube32 Apr 05 '22 edited Apr 05 '22
The best way to imagine recursion is like the movie inception.
Let us consider a recursive function to print from 0 to any given number (n)
Lets first understand the function line by line.
Function declaration, pretty straightforward.
Checks if n is equal to 0 (will explain what a base condition is later)
If second line is True, then print 0
Else
Call the same function, BUT with n-1 as the parameter.
print the current value of n
Return (end of function)
Now say user inputs the value of n as 3.
The function first checks if n==0, which gives false since n=3, so the code moves to the else block.
Now what does the else block say? The else block says call the same function again, but with value 2 (n-1).
The code thinks to itself, "Huh, weird but sure whatever" and proceeds to do so. So now we're back to line number 1, except this time its
function(2).Now one more time at line 2 it checks if n==0, and it gives false so we move to the else block. The else block says, okay since we're here, lets call the bad boy once again with n=1.
The same process repeats for n=1 as well and the function is called for n=0.
Now the magic happens, the if statement at line 2 evaluates to True! The compiler is overjoyed as it was waiting to enter the if block. It finally enters the if block and asks the code, what do I do now? At line 3 the code says, since you have managed to make it so far, go ahead and print(0) on the screen. The compiler happily obliges and moves forward. This time, it skips lines 4 and 5 since the if block was executed and goes directly to line 6, which is a simple return statement.
Now where does the compiler emerge? It emerges back at line 5 BUT for when n=1. Now that its back it goes to line 6 and displays the current value of n (right now 1). Then on to line 7 where the the function ends for n=1.
Where does it emerge now? Again at line 5 but for n=2. Proceeds to follow the same steps as above.
Then finally we're back to line 5 for function(3) what the user originally called, where the function terminates.
A base condition is what tells a recursive function " so hey listen, if this condition is met, just do so and so and return a constant value, DO NOT call yourself again". Without a base condition a function will keep on calling itself repeatedly (equivalent of an infinite loop)
>somehow does a magic wrap up backwards thing that does all the calculations.
>>Now where does the compiler emerge? It emerges back at line 5 BUT for when n=1.
This is the magic backwards part, after the base condition is met, functions start coming out and proceed with the rest of the code, like in this case they continued from line 6.
I am afraid I have over complicated it, recursion is best understood with a visual representation. I hope you atleast got something out of it, I am not so good with explaining stuff over text :/