Let's talk recursion 😀

This is a continued discussion from another post.

The question is, Is this recursion?

function addTogether() {
  var len = arguments.length;
  var a = arguments[0];
  var b = arguments[1];
  var isNum = function(arg) { return Number.isFinite(arg); };
  if (len === 1 && isNum(a)) {
    return function(x) {
      if (isNum(x)) {
        return a + x;
      }
    };
  }
  else if (len === 2 && isNum(a) && isNum(b)) {
    return a + b;
  }
}
function addTogether() {
  var len = arguments.length;
  var a = arguments[0];
  var b = arguments[1];
  var isNum = function(arg) { return Number.isFinite(arg); };
  if (len === 1 && isNum(a)) {
    return function addTogether(x) {
      if (isNum(x)) {
        return a + x;
      }
    };
  }
  else if (len === 2 && isNum(a) && isNum(b)) {
    return a + b;
  }
}

It is a recursive function becuse the function that gets returned gets called again. This solution is syntactic thing and looking at it like this might help to visualize

No, it is not recursion. Recursion does not work here. There is no recursion at play.

Recursion is when a function repeatedly calls itself. In this case, if the addTogether function is called with a single argument and that argument is a number, then a new function is returned.

1 Like

recursion is a repatition and the fact it repeats itself once says it all right there

No. This is not recursive. I am sorry but your understanding is wrong.

Recursion is when a function calls itself. The addTogether function does not call itself.

Instead, the addTogether function creates a brand new function which references the variable a and returns this brand new function.

The syntax is perhaps confusing because this new function has not been given a name.

the function that gets returned is calling itself which is why the example works is all you need to understand. you can use console.log() statements in different places if you want to understand where this is happening in the chain

particularily console.log(arguments) which are paramaters that are not required

and the returned funtion calls itself

No. It does not. The returned function never calls itself. The returned function only calls the helper function.

The new function addThis references the value of first used in the function call to addTogether, but that does not involve recursion. That value is stored with the function addThis when it is created.

isNum is a helper function that is also stored, but it never calls itself. At no point is any function calling itself.

1 Like

you can store functions in variables and in this instance the function is returned to nothing and executes itself

None of the functions call themselves.

If you believe one does, please show me the exact line where a function calls itself.

why does it call itself then

it would never return anything if it didn’t get executed and it would otherwiser be orphaned as a non executed funtion

The control flow is as follows:

addTogether is called

One of three branches occurs

  1. One argument was passed in and is a number. A new function addThis is created and returned.

  2. Two arguments are passed in and both are numbers. Their sum is returned.

  3. In all other cases the return value is undefined

Nowhere in that control flow did addTogether call addTogether.

the returning function calls itself

Nope. Nowhere in the definition of addThis is there a function call back to addThis.

the compiler is executing the orphaned function so you could think of it as calling itself

No. It is not. The function is just returned.

The new returned function addThis may be stored to a variable for later use or immediately invoked, but neither of those involve recursion.

Recursion means that a function calls itself. addTogether never calls itself. addThis never calls itself.

the console shows stored functions differently so you arent explaining yourself in how it is getting executed to recieve a final awnser

This function has three possible final answers.

  1. the function may return a number

  2. the function may return a new function for later use

  3. the function may return undefined

These are the final results. A function is a first class object in JavaScript and can be returned and stored like any other object.

There is zero recursion.