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

71

u/dkToT Apr 05 '22

Basically a function that will call itself until a certain criteria or situation is satisfied. Once it is satisfied, the result will be returned to the parent function call all the way back to the top. This will help with clean code but only if you know your functionality follows a certain pattern.

A great example of this is the fibonacci series and why it was used in alot of early interview questions. The fib(n) is equal to fib(n-1) + fib(n-2). With recursion you can use this pattern to do the calculation for fib(n-1) and fib(n-2) where fib(n-1) = fib(n-2) + fib(n-3) and fib(n-2) = fib(n-3) + fib(n-4), etc....

Super useful and clean if you now the exact pattern and expectation (traversing a tree, etc..) of what you are building but I personally don't see this used often.

15

u/TheTomato2 Apr 05 '22

To add on to this, recursion, at least for me, was confusing because sometimes it's taught before they teach you how or what the main stack in program works/is. It's really simple once you learn its just the same function calling itself and pushing another instance on to the stack and then using returns to unwind the stack. That's all recursion is. It's also makes it easy to understand why probably shouldn't use it unless the problem really begs for it.

4

u/above_all_be_kind Apr 06 '22

Yes, this was exactly it for me. Recursion, as a concept, was perfectly understandable but it wasn’t until I understood how its code-based implementation actually worked that I was able to start getting comfortable with creating it from scratch. The whole secret to understanding its code-based implementation, as it turned out, was comprehension of stacks.

7

u/Temporary-Warthog250 Apr 05 '22

Awesome!!

5

u/---cameron Apr 05 '22 edited Apr 05 '22

Recursion:

GNU means GNU is NOT UNIX

It is defined in terms of itself.

add(x,y)
  return add(x-1,y+1) + 2 

uppercase_name(name)
    uppercase_name(first_half(name)) + uppercase_name(second_half(name))

Of course, in these versions the functions / GNU are purely defined in terms of themselves, so they'd go on forever; (GNU) = (GNU is not linux) = ((GNU is not linux) is not linux) = (((GNU is not linux) is not linux) is not linux) ....

So I just want to add that while recursion implies a loop, you want to eventually have a way to end the loop, by making the definition at some point not call itself (as if it calls itself, it starts the loop again)

add(x,y)
    // During one of our 'loops', when we finally meet this condition
   // we don't loop again, we will finish
    if(x <= 0)
      return x + y;
    else
      return add(x-1,y-1) + 2  

(Note, this add function is weird, its just an arbitrary example of defining something in terms of itself).

So calling add would result in

add(3,1)

 // x isn't = 0 so 

--> add(2,0) + 2

--> (add(1,-1) + 2) + 2

--> ((add(0,-2) + 2) + 2) + 2

 // X is 0 so we just return x + y 

--> ((0 + -2) + 2) + 2) + 2

--> 4

2

u/TeknicalThrowAway Apr 05 '22

of what you are building but I personally don't see this used often.

Traversing a filesystem, or a DOM, or parsing a document :)

2

u/[deleted] Apr 06 '22

I can understand that Recursion repeats itself. but every code repeats the process it is doing, all the for/while loops that i have used in code is also repeating. so does that mean, i can apply recursion to every program ??? or is there any limitation ??

2

u/username-256 Apr 10 '22

Good question. The answer is to do with how recursion is implemented.

While recursion is in theory equivalent to iteration, since every recursive algorithm can be implemented by an iterative algorithm, in practice recursion nearly always uses more memory. The extra memory is used when the implementation adds data (such as the function return address) onto the machine call stack. The data can also include the call arguments (the values you pass to the function parameters).

In some languages and some compilers a version of recursion called tail recursion can be used (or recognised by the compiler) where extra data is NOT added to the stack.

So no, you shouldn't apply recursion to every problem. Real-world problems such as limited (virtual) memory get in the way. Meaning that after MANY recursive calls your program crashes with an "out of memory' condition.

1

u/aleptian Apr 05 '22

I’d say i’m decent at tracing recursion… but actually applying it is where i’m clueless.

1

u/MattPatrick51 Apr 06 '22

So basically behaviour trees or state machines? Or i'm dumb and should be sacrificed for heresy.

1

u/quackquackgo Apr 06 '22

To add to the explanation, to understand/write recursive algorithms I found really useful to keep two steps in mind: Base case and recursive case. Before writing any recursive program, you have to know what are your cases.

Step 1: The base condition (bottom of the recursion tree): It has a simple answer and is the condition that will stop the recursion (instead of continuing forever). In the fibonacci example, the base case would be fib(1) = 1 and fib(0) = 0.

Step 2: The recursive case: The part with the recursive call. I usually just think about what I want to do in simple words. In fibonacci, I want to sum the fibonacci of two previous numbers. Simply written: fib(n-1) + fib(n-2).

Important: base case always comes first. Making a recursive call without any condition before will guarantee your program never stops.

And that’s all. That’s the “magic” of recursion. No more thinking (ofc it’s best to follow the code in your head with an example to make sure it’ll work, but assuming your two cases are correct, that’s it). You final code will look like:

FIB(n):
  // base case
  if (n == 0) return 0
  if (n == 1) return 1

  // recursive case
  return FIB(n-1) + FIB(n-2)

Recognizing the cases has really helped me A LOT, I hope it helps you too. Following the two steps makes recursion way easier.