What on Earth is Recursion? - Computerphile

Computerphile · 780K views · 2014 · 9:40

Open on YouTube

Algorithms & data structuresSoftware practice, open source & careersEducation & learningexplainer

Why Stack Overflow recommends it

Stack Overflow users link it when answering beginner questions about how recursive calls and the call stack work, as a visual explanation to go with code traces.

Recommended by 7 people across 6 different months, 2014 to 2019.

What it is

A Computerphile video in which Professor Brailsford explains recursion from first principles, using the University of Nottingham computer science setting and the call stack idea. It is a short explainer filmed by Sean Riley for the Computerphile channel.

What people said

Recommends AntonH · 2014-05 · Stack Overflow · on “Java Recursion method”

mystery(20) = mystery(10) + 1 mystery(20) = (mystery(5) + 1) + 1 mystery(20) = ((mystery(2) + 1) + 1) + 1 mystery(20) = (((mystery(1) + 1) + 1) + 1) + 1 mystery(20) = ((((mystery(0) + 1) + 1) + 1) + 1) + 1 and we know that mystery(0) = 0. mystery(20) = ((((0 + 1) + 1) + 1) + 1) + 1 mystery(20) = (((1 + 1) + 1) + 1) + 1 mystery(20) = ((2 + 1) + 1) + 1 mystery(20) = (3 + 1) + 1 mystery(20) = 4 + 1 mystery(20) = 5 Or, simply put, we get 1+1+1+1+1=5 Pretty good video on recursion here: [this video]

Recommends Tesseract · 2014-07 · Stack Overflow · on “How does return function return a value to If statement?”

There are a few great videos that explain how recursion and the stack works on the Computerphile channel on youtube. You should watch them all. [this video] [LINK] [LINK] [LINK] [LINK]

Recommends matrixanomaly · 2015-03 · Stack Overflow · on “Java Recursive Method how does it work?”

Showing you a link isn't enough to warrant for an answer, and are many resources on this, as recursion is a concept in computer science and programming, not just to Java. Here's a video by Computerphile I felt helped visualize and understand recursion: [this video]

Recommends castletheperson · 2016-05 · Stack Overflow · on “Eloquent JavaScript Recursion Return?”

Recursive functions can often be expressed as a loop. See this adaptation: function power(base, exponent) { var result = 1; for (var i = 0; i < exponent; i++) result *= base; return result; } As you can see, the exponent doesn't play a role at all in what is output, it just represents the number of loops to make. In the world of recursion, the exponent represents the number of times the function has yet to call itself before it can return. This YouTube video visualizes recursion nicely: Computerphile [this video]

Recommends Vishwas Atrey · 2019-01 · Stack Overflow · on “how does recursion retain value even after some cycle”

You are getting the above output because at the 8th line of your code you are calling the function agian recursively so the remaining code after the 8th line is saved in the call stack in your memory. So, when you get the output 1 2 3 4, the remaining code that is left (in call stack) is executed hence you are getting this output. below is the call stack of your program when n = 0 the stack will place 1 |___| |___| |_1_| when n = 1 the stack will place 2 |___| |_2_| |_1_| when n = 2 the stack will place 3 |_3_| |_2_| |_1_| when n = 4 the stack will not place anything as it encounters the return statement now, we have some values in our stack so the program starts popping the top most value i

Recommends iGian · 2019-03 · Stack Overflow · on “Why does adding 1 to a recursive proc call return the count of calls made?”

I just added some puts to your code, maybe it helps to follow the logic better than I can explain with my english. So, this is the tweaked code: def rcall(num) if 10 == num rec = 0 puts "rec = #{rec} ---- End of recursion" return rec end rec = rcall(num - 1) res = 1 + rec puts "rec = #{rec} \t\t res = i + rec = #{res}" res end When you call on 15 for example, you get: rcall(15) # rec = 0 ---- End of recursion # rec = 0 res = i + rec = 1 # rec = 1 res = i + rec = 2 # rec = 2 res = i + rec = 3 # rec = 3 res = i + rec = 4 # rec = 4 res = i + rec = 5 # 5 If you call on a number less than 10 , you never reach the end of the recursion, so no value is returned to build back the "call stack" and the

Recommends Adam · 2019-03 · Stack Overflow · on “Longest Collatz sequence Efficiency”

Also thanks for sharing. This is beautifully simple and probably most efficient way to calculate it. It made me learn about recursion and I spent great deal of time try to understand it until I got it. For anyone wondering I recommend below video: [this video]