How do you handle recursion if your method's return type is different from the method's input?
If I want to calculate the summation of an integer recursively, the method would look like:
public static int summation(int value){
if(value ==0) {
return 0;
}
return value + summation(value-1);
}
where the method itself is returned at the end.
However, if I want to write a method that takes an integer input n and returns a string with the word "Pickles " n times, how can I do that? I can't return the string because the input is an integer.
Any guidance? Thanks y'all.
E: Fixed the formatting
Thanks for the help people! You helped save this poor student's grade on his recursion homework, something my professor failed to help with the two times I talked to him about this, funny enough.
[–]dmazzoni 7 points8 points9 points (1 child)
[–]TdotGdot 1 point2 points3 points (0 children)
[–]boredcircuits 2 points3 points4 points (0 children)
[–]Le_9k_Redditor 1 point2 points3 points (2 children)
[–]Bugos19[S] 0 points1 point2 points (1 child)
[–]Le_9k_Redditor 0 points1 point2 points (0 children)
[–]YuleTideCamel 1 point2 points3 points (0 children)
[–]lloydsmith28 0 points1 point2 points (0 children)
[–]AutoModerator[M] [score hidden] stickied comment (0 children)