How are the functions connected and where does "`number`" come from?

Hello, I’m currently learning about “helper functions” in “introduction to javascript” here and I 'm a bit confused.

In the following code, the value of 15 is first passed as an argument for the function of getFahrenheit as “celsius” but what I don’t understand is how it’s then again used as an argument for multiplyByNineFifths. How is there a relationship between that function and 15? I don’t understand how they connect.

The lesson attempts to explain it, but I still don’t get it. Here is the code along with it’s explanation

Code:

function multiplyByNineFifths(number) {
return number * (9/5);
};

function getFahrenheit(celsius) {
return multiplyByNineFifths(celsius) + 32;
};

getFahrenheit(15); // Returns 59

Explained:

  • getFahrenheit() is called and 15 is passed as an argument.
  • The code block inside of getFahrenheit() calls multiplyByNineFifths() and passes 15 as an argument.
  • multiplyByNineFifths() takes the argument of 15 for the number parameter.
  • The code block inside of multiplyByNineFifths() function multiplies 15 by (9/5) , which evaluates to 27 .
  • 27 is returned back to the function call in getFahrenheit() .
  • getFahrenheit() continues to execute. It adds 32 to 27 , which evaluates to 59 .
  • Finally, 59 is returned back to the function call getFahrenheit(15) .
2 Likes

the lesson does indeed explain this:

The code block inside of getFahrenheit() calls multiplyByNineFifths() and passes 15 as an argument.

the value 15 is passed as argument to celsius parameter, then the celcius parameter (which now has a value of 15) is passed as argument to the parameter of multiplyByNineFifths function

5 Likes

Yeah i didn’t quite get it too, i think the confussion is because we don’t know what ‘number’ means at all, i mean, could i use a word aside ‘number’ and the function will take it anyway, right? Because when it says ‘number’ but in the next function there isn’t a ‘number’, then there’s the confussion.

Anyway, with the exercise i understood it, it’s simple.

2 Likes
function numSquared(number) {
    return Math.pow(number, 2);
}

In the above function, number is an arbitrarily chosen name. We could use any variable we wish, but hopefully it has some meaning congruent with the function’s purpose. n or x are just as valid. dog or banana would not make much sense, though, right?

var number = 13;
console.log(numSquared(number));

The above has arbitrarily defined a variable with name, number with a preset value. The call expression argument is not arbitrary since number must exist.

Parameters (formal parameters) define local variables so the variable doesn’t have to exist when the function is defined. Arguments on the other hand, when they are a variable the variable must be defined or an error with be thrown.

6 Likes

Thank you, that was helpful in understanding this.

1 Like

It made more sense to me to do it this way. I also got the same result. Is there a reason the exercise did it differently?

function multiplyByNineFifths(celsius) {
  return celsius * (9/5);
};

function getFahrenheit(celsius) {
  return multiplyByNineFifths(celsius) + 32;
};

let fahrenheitResult = getFahrenheit(15); 

console.log(fahrenheitResult);

To reiterate: I don’t get why the exercise didn’t just use celsius for all the parameters.

I have the same doubt. Why and how does the function getFahrenheit() calls a function that has another name entirely, meaning multiplyByNineFifths()? How does that connection occur?

In my understanding, if I call getFahrenheit(), the function that it is going to get called is getFahrenheit().

{
const f = x => x * 9 / 5;
const c = x => x * 5 / 9;
const F = x => f(x) + 32;
const C = x => c(x - 32);
console.log(F(-40), C(-40));
console.log(F(0), C(32));
console.log(F(100), C(212));
}
-40 -40
32 0
212 100

Hello all,

As I have understood so far is that this functions creates basically some equations…
I have created my own example:

function aNumber(x) {
return x * (5/2);
} ;
function anotherNumber (y) {
return aNumber(y) + 10;
};
anotherNumber(3);

…and it should look like this: (x*(5/2)) +10 …
Please correct me if I am wrong. This is what I understand from the course example…

Not wrong at all, but, there is a dependency which can lead to problems down the road. A program as a whole depends upon its functions and modules, but functions depending upon other functions is another matter. An alternative to naming another function within a function is passing by reference the function we wish that function to use.

function aNumber(x) {
    return x * 5 / 2;
}
function anotherNumber(f, x) {
    return f(x) + 10;
}

console.log(anotherNumber(aNumber, 3))    // 17.5

Of course there is more that must be woven in to prevent errors, but that is the gist of it.

Bottom line, avoid use of named functions (unless built in to ES) within functions. This will help avoid problems later.


Now, wrap your head around this…

function plusTen(f) {
    return x => f(x) + 10;
}
fiveHalvesX = plusTen(aNumber)
console.log(fiveHalvesX(3))    // 17.5
1 Like

I think parameters and arguments passed in this example are kind of a “sequential linked chain” !!

excuse me. while calling fiveHalvesX(3), at what particular moment is 3 being assigned as the value of x?

x is 3 in the aNumber function.