r/learnprogramming • • 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

738 comments sorted by

View all comments

Show parent comments

4

u/EdenStrife Apr 05 '22

It's just calling a function from within itself.

It's sometimes easier and makes more sense than doing a loop.

9

u/KimPeek Apr 05 '22

There is high risk of introducing memory leaks if you use recursion where you should use a loop. They are not interchangeable. CS recursion, compared to mathematical recursion, requires more than a function calling itself. Recursion requires a base case, a decomposable problem (size-n problem), a defined subproblem (size-m problem), and the construction of the solution for the size-n problem from solutions to size-m problems.

3

u/EdenStrife Apr 05 '22

Sure, recursion is not a substitute for a loop in all cases. It's useful for specific problems.

I do have a bit of an issue with defining recursion the way you do. Recursive problems and their solution structure is one thing and recursion as a concept seems to me to be different things.

The idea is just that a function calls itself. That you can use that to solve a specific kind of problem seems to me at least to be an arbitrary add-on to the definition.

1

u/Aggravating_Moment78 Apr 06 '22

Nope that’s actually WHY the function would call itself, because in and of itself a function calling itself is not very useful unless you are using it to solve a problem in the way described above or a similar way...

→ More replies (1)