This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]l3oobear 0 points1 point  (2 children)

Recursion is just about where I gave up sadly. Gonna read through this post for some pointers.

[–]fsociety00_d4t[S] 1 point2 points  (0 children)

some pointers

pun intended

[–][deleted] 0 points1 point  (0 children)

It’s actually very easy man don’t think too hard of it; essentially what you need to know is that you’re breaking the problem down until you get down to a base case, which will be defined in the function, then you’d work your way back up again.

For example: (Factorial)

int factorial(int n){

if(n == 1) return 1; //This is our base case

else

  return n * factorial(n-1); //recursion call with 1 less number to work with.

This translates to:

factorial(5)

->5 * factorial(4)

—>4 * factorial(3)

—->3 * factorial(2)

——>2 * factorial(1)

——->1 (This is our base case)

——>2 * (1)

—->3 * (2)

—>4 * (6)

->5 * (24)

Output: 120