Function that returns function in Javascript

6

I started to study Javascript a short time ago and I came across a situation in a code (exercise) that I can not understand.

function hi(a,b) {
   return a*b;
}

function hello(a,b) {
   return hi(a,b+1);
}

hello(3,3);

What I understand from the above code is:

  • Step 1: The hello function is called with parameters 3.3.
  • Step 2: The hello function returns the hi function with the 3.3 parameters, but the hi function returns a b *, logo 3 3 = 9 right?

When requesting the return within the hello function what I imagine happening is:

return hi(9+1);

The result would be 10 , but when I run the code it shows me the value 12 because?

    
asked by anonymous 15.03.2015 / 15:33

3 answers

7

This code actually runs in this order:

  • Calls hello(3,3)
  • Within% with% executes hello resulting in 4
  • Calls b+1 , values of hi(3, 4) and a
  • Within b+1 multiplies and returns hi and a (result 12)
  • Back to b return of hello , which is 12, is returned

What you did not understand was that the addition is made before passing as an argument. Arguments are expressions that must be resolved before they are sent.

In theory the value of hi would also be calculated before sending as an argument, however it is a simple expression that does not need to do calculations, so it is used as is.

It's interesting to understand this expression question to know what you can use where and what you'll be doing when you use an expression. By not understanding that a program location accepts expressions in general, beginner programmers often abuse variable creation. How many times do I see programs that make an expression, save it to a variable, and then use the variable in only one place in the program. This is done because the programmer has learned that a place accepts variables and it does not realize that instead of the variable it could put the direct expression. When you use the expression it is performed before it is used. We could understand your code this way:

function hi(a, b) {
   return a * b;
}

function hello(a, b) {
   temp = b + 1;
   return hi(a, temp);
}

hello(3, 3);

Variables are storages, they are only needed when you need to store a value. And just need to save a value when you will use it more than once in the program. In some cases this can be useful when you need intermediate results.

Of course you can eventually create a variable that will be used only once for ease of readability. But this needs to be done consciously, not because you do not know that an expression might be being used.

Interestingly when the programmer learns that a place accepts expressions, it thinks that it can not use variables. Example:

if (x == 0 && y == 1)

could very well be written like this:

condicao = x == 0 && y == 1;
if (condicao)

Not everyone realizes this. I'm not saying that generally writing this way is interesting but it can be useful at some point.

As additional information the code is not returning a function, it returns the return of a function . This distinction is important because in fact a function can return another function, in which case it would be returning an algorithm rather than a result already computed. But it's not what you're doing.

This does something else and does not present the result you wanted:

function hello(a, b) {
   return function(a, b) { return a * b };
}

hello(3, 3);
    
15.03.2015 / 15:45
4

Rafael is when the hello function calls the hi function it passes as its a and b parameter received, but a, b + 1.

Type

hello(3,3) {
 //hi(3, 3+1)
 return hi(3,4);
}

blz

    
15.03.2015 / 15:42
3
  

The function hello returns function hi with parameters 3,3 but the function hi returns a b , 3 3 9 right?

No. What is passed to the function hi is 3 and 3 + 1 .

  

When requesting the return within the hello function what I imagine happens is: return hi(9+1) . The result would be 10 , but when I run the code it shows me the value 12 because?

The result is 12 because the function hello returns the result of the multiplication of a and b , being a = 3 and b 1 , the function hi multiplies 3 by the result of the sum between 3 + 1 , then 3 4 = 12 .

To give the result you want, the addition has to be done after multiplication.

function hi(a,b) {
   return a * b;
}
function hello(a,b) {
   return hi(a, b) + 1;
}

alert(hello(3,3)); // 10
    
15.03.2015 / 15:42