C
C#2mo ago
splorgamus

I need help implementing recursion in a general way

Help I realise you need to understand recursion to understand the rest of DSA like graphs etc but I'm not fully confident So I know that recursion uses a base case and a recursive case but how can I come up with these on the spot like nothing because you can turn any for loop or while loop into a recursive function?
5 Replies
Keswiik
Keswiik2mo ago
I feel like you're gonna have a hard time getting good responses with the way you've phrased this question. What exactly do you need help with? Got examples of something you're having a hard time translating into a recursive function?
splorgamus
splorgamusOP2mo ago
I was thinking of for example iterating through the elements of an array maybe print maybe print every other element like you would with i += 2
Keswiik
Keswiik2mo ago
given that example, what do you think your base and recursive cases would be?
splorgamus
splorgamusOP2mo ago
So I suppose for the base you would have an index variable as a parameter and if that's >= to the array length then you return? (I'm not exactly sure why sometimes people return blank) And the recursive case you would call the function with your argument as index + 2
Keswiik
Keswiik2mo ago
yup, sounds right to me

Did you find this page helpful?