3

In the following recursive function, I expect the function to return "arrived" at the end but instead, it returns undefined. Isn't that when the excution goes in the if block the code should return? Appreciate your comments on this.

function myFun(i){
    if(i===0){return ('arrived');}
    i = i - 1;
    myFun(i);
}

If I change the code as follows then it'll return "arrived" but still don't know why the above doesn't return "arrived".

function myFun(i){
    if(i===0){return ('arrived');}
    i = i - 1;
    return myFun(i);
}
3
  • @anthony, i is sent in the parameter Commented May 1, 2018 at 15:31
  • If you have a function foo, which does nothing but call a function bar, which returns a value "BAR", would you expect foo to return the result of the call to bar if foo didn't have a return statement? function foo() { bar() } function bar() { return "bar" } foo(); A recursive function is no different. Commented May 1, 2018 at 15:36
  • 1
    Your example totally makes sense. Thank you! Commented May 1, 2018 at 15:59

2 Answers 2

7

The first function does not return a value because all code paths must return a value. And after the first line, there is no return statement. It only returns a value when called with 0 as parameter.

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

3 Comments

If you call the function myFun(5) then it ultimately reaches to myFun(0) and at that point it should return "arrived", right? So, I mean if I call myFun(5), how is it different from myFun(0) in which I directly call the function with argument 0?
In that case you have 6 nested (recursive) calls. The 6th call is done with i === 0, and yes it returns a string to the context of the fifth call. That value is then 'thrown away' because from where the 5th (and 4th and 3rd etc) calls happen, which is line 3, there is no return statement.
Now I see, great explanation, Thank you.
1

Recursion is a functional heritage and so writing your program in a functional style will yield the best results.

This means avoiding things like

  • imperative style statements for, if, switch, etc that do not return a value
  • mutation or variable reassignments like i = i + 1

const myFun = i =>
  i === 0
    ? "arrived"
    : myFun (i - 1)
    
console.log (myFun (10))
// "arrived"

Notice expressions, unlike statements, evaluate to a value. We made the following changes

  • function statement function myFun (i) { ... } replaced with a function expression myFun (i) => ...
  • if statement replaced with ternary expression, eg condition ? ifTrue : ifFalse
  • variable assignment statement i = i - 1; myFun(i) replaced with expression myFun(i - 1)

Note, the return statement itself is a side effect and has little use in a functional JavaScript program.

Going along with the other answer here, all code paths must return a value! An advantage to writing your program in a functional style means you can't write a function that doesn't return a value, nor can you write a conditional with only one branch.

TL;DR: use functional style and your problem automatically vanishes

Comments

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.