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

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