C
C#17h 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
Keswiik16h 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
splorgamusOP16h 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
Keswiik16h ago
given that example, what do you think your base and recursive cases would be?
splorgamus
splorgamusOP16h 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
Keswiik14h ago
yup, sounds right to me
Want results from more Discord servers?
Add your server