0

I've been doing Project Euler 'cause reasons. At one point, I found myself wanting to compare a value with the result of a function. I've since selected a different solution, but it's left me curious. How would this work? If I were to do something like:

//javascript
if x == mathyFunction(10){
  //do this
}

function mathyFunction(y){
  if(y>0){
    mathyFunction(y-1);
    return y;
  } else {
    return y;
  }
}

I'm aware that this isn't tail recursive or anything fancy, I'm mostly just curious how the logic works behind this.

How would the computer interpret it? Would it return true if any one of the values == x or would it only return true if all the values == x. Experimentation has left me guessing, though I'll keep digging.

9
  • From your code, I see the mathyFunction winding the 10 value down to zero and always returning 0. Then comparing it to x, and if x isn't 0 then it would be false. Commented Oct 2, 2016 at 4:05
  • @nocturns2 It's not returning 0, it's returning y. Commented Oct 2, 2016 at 4:07
  • You're right. I was referring to the value of y, which would be what you would be comparing to the value of x. Commented Oct 2, 2016 at 4:08
  • @nocturns2 x is compared to 10 as that's the value that's passed in for y. Commented Oct 2, 2016 at 4:10
  • Honestly, I'm not sure if your code will even work. it should be mathyFunction(y){ return y>0? mathyFunction(--y) : y } Commented Oct 2, 2016 at 4:10

1 Answer 1

1

You didn't specify which language you're asking about, but assuming the usual semantics of the return statement, a function can't return more than once. So if by "return all values individually", you meant something like this:

function f() {
  return 1;
  return 2;
  ...
}

Then everything after the first return is just dead code and it's the same as if the return 1 was the only return statement in there. A function call has exactly one value and that value is what you're comparing against.

If you meant that mathyFunction returns a list or other collection object, which contains the values between x and y, then x will be compared to that list. Again the semantics will depend on the language, but in most languages comparing a number to a list will either be a type error or just false.

If by returning the values individually you were referring to a yield statement (like C#'s yield return or Python's yield), then the result of the function is going to be some kind of iterator object and the same thing as in the previous paragraph applies.

Sign up to request clarification or add additional context in comments.

3 Comments

Apologies, I should've been more specific. It's javascript. The code behind mathyFunction is recursive, so it recurves and then returns a value. I've modified the example slightly to reflect this, and I've changed mathyFunction(x,y) to mathyFunction(x) for simplicity.
@Ucenna The function in your edited question is the identity function. In both cases of the if all you do is return y. In the then-case you also call the function recursively, but since you don't do anything with the recursive call's result, that doesn't do anything (except make the code take longer unless the JavaScript engine manages to optimize that call away). Again: a function call can't return more than once - recursion does not change that.
Okey dokey then! I was working my way to that conclusion. What you've said aligns perfectly with my results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.