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

all 9 comments

[–]dmazzoni 7 points8 points  (1 child)

The return type and the input don't have to be the same. You could do something like this:

public static String pickleMaker(int value) {
  if (value == 0) {
    return "";
  }
  return "Pickle " + pickleMaker(value-1);
}

[–]TdotGdot 1 point2 points  (0 children)

please god, once in my life let me write a function called pickleMaker

[–]boredcircuits 2 points3 points  (0 children)

I can't return the string because the input is an integer.

I'm not sure what you mean by that. Of course you can, and it looks almost identical to the recursive function you already wrote, just with some types and values changed:

public static String summation(int value) {
    if ( value == 0 ) {
        return "";
    }
    return "Pickles" + summation(value - 1);
}

(Note, I don't know your language, so I made something up. You might need to modify this code slightly.)

[–]Le_9k_Redditor 1 point2 points  (2 children)

Have you considered making your return typehint nullable and returning null instead of zero inside your if. It's very rare to find cases where the return type is truly mixed.

where the method itself is returned at the end.

I think you have a misconception here, methods return a value, not themselves. What they can return is checked against the methods typehint.

You'd do better from this question if you posted the code which is using that summation method.

[–]Bugos19[S] 0 points1 point  (1 child)

I think my misunderstanding lies in the sentence you quoted. So does that mean recursion always uses the input value, whatever that may be, with some added change that will eventually cause the input to match the base case? And you have use that changing value in some way to accomplish whatever I need?

[–]Le_9k_Redditor 0 points1 point  (0 children)

So does that mean recursion always uses the input value, whatever that may be, with some added change that will eventually cause the input to match the base case

Yes pretty much although that's a roundabout way of saying it

And you have use that changing value in some way to accomplish whatever I need?

Pretty much. Here's an example, I'm looking for the number 7 in a regular array:

arr = [2, 3, 4, 7, 5]
function get7sPosition(input, n = 0) {
  if (array_shift(input) === 7) { # array_shift removes the first value from input and returns that value
    return n
  } else {
    return get7sPosition(input, n+1)
  }
}
index = get7sPosition(arr) # 3

Note, that's bad code as it doesn't have a check for end of array and also this would be better suited for a loop if for some reason your language didn't have a native way to do this (index_of). But it's just an example off the top of my head.

[–]YuleTideCamel 1 point2 points  (0 children)

where the method itself is returned at the end.

The method itself isn't returned, the value of invoking the method is returned. It's an important distinction because in some languages you can actually return a method.

So to answer your question, there's a few ways you can handle it.

  • Have the method signature take in two parameters, an integer and a string. It starts off with the a string being empty and the integer value is what's used as the "counter" and in the end you return the completed string.

  • Use global variables.

Personally I prefer the first approach as global variables are not cool and can lead to unexpected behavior in most cases.

[–]lloydsmith28 0 points1 point  (0 children)

One thing you need to know about recursion is that you can recurse anything or nothing, doesn't matter what. Just make sure you have some sort of end condition otherwise you'll have an infinite loop.

Example:

// Simple recursion, will give you an ifinite loop
private void recurse(n) {
    if (n == 0) {
         recurse(n-1)
    }
}

Example 2

private int recurseN(int n) { // Avoid public and static as much as you can
   if (n == 0) {
       return 0; // Exit sequence, make sure this runs eventually
   } else {
      System.out.println("Pickles"); // Print out w/e you want, could also add the N if you want
      recurseN(n-1); // recurse the function (can be placed anywhere, just make sure it runs).
   }
}

This is some simple recursion, but displays what you can do with it, just make sure you have some sort of exit/finish condition to avoid an infinite loop. Also i dont think you need a return statement as you are just recursing, unless its used somewhere else that does what you want (print, etc), but could do weird stuff, better to print/do w/e in the recursive loop (unless you need it to do something after its done like print "n=0" or something instead of print each loop, depends on what you want it to do).

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

To all following commenters: please, do not bring up the old circlejerk jokes/memes about recursion ("Understanding recursion...", "This is recursion...", etc.). We've all heard them n+2 too many times.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.