1

why is it necessary to add return statement before ternary operator in recursive function to return function output?

// This dose not work

    function rec(n) {
      n == 1 ? n : n + rec(n - 1);
    }

// This works as return statement is added before ternary operator
    function rec(n) {
      return n == 1 ? n : n + rec(n - 1);
    }

// This works
    function rec(n) {
      if (n == 1) return 1;
      return n + rec(n - 1);
    }

2
  • 3
    Why do you expect recursive functions to work any differently from non-recursive functions in regards to return? Commented Apr 20, 2020 at 6:38
  • I mean when return is default in ternary operator, why do I have to add it extra? Commented Apr 20, 2020 at 6:44

5 Answers 5

1

// If you would like to do this in one line then correct solution would be:
    let rec = n => n == 1 ? n : n + rec(n - 1);
// Now you dont need to add the return keyword before

// This works as return statement is added before ternary operator
    function rec(n) {
      return n == 1 ? n : n + rec(n - 1);
    }

// This works
    function rec(n) {
      if (n == 1) return 1;
      return n + rec(n - 1);
    }

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

Comments

0

A recursive function is function which calls itself during the execution. The ternary operator decides if the function need to call itself. So the return statement call the same function.

In the example n == 1 ? n : n + rec(n - 1); if n=1 then the function should return the value of n if not then the function will call itself with new value that is n-1.

Comments

0

You need a return because of

n + rec(n - 1);

where the rec(n-1) call needs to return a value to be able to calculate n + rec(n - 1), and that goes for each call to rec() until n reaches 1 when it just returns 1.

Comments

0

return is never default in ternary operation. return is default in Arrow-function but it not default in normal function deceleration. to return a output from a normal function execution it is always necessary to add return statement, but it is optional in case of Arrow-function.

function x() { 5;}
console.log(x()); // Opuput: undefined

let y = () => 5;
console.log(y()); // Output: 5

Comments

0

A conditional expression (often called a ternary) is simply an expression. It yields a value, but it doesn't do anything with it. In fact, unless it has side-effects, it's totally useless unless you either:

  • return it from a function,
  • assign its result to a variable, or
  • nest it in another expression in which you do one of these things

You may be confused by the fact that arrow functions with single-expression bodies return the result of that expression. It's still being returned by the function, even though you don't explicitly use return. And because of this simplicity, conditional expressions are often used as the body of arrow function.

But it should be no more surpising that you have to have return here than that you have to have it in

function add (x, y) {
  return x + y;
}

If you took out the return there, the addition will still happen when the function is invoked, but it won't yield any value. It's the same thing in your original.

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.