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) .
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
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.
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.
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
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
That is just a random number, you could enter another number (for example 20), then the function will tell you that 20 degree Celsius is 68 Fahrenheit.
What I have understood is that these functions create a connection between ‘number’ and ‘Celsius’, and I think it’s done by:
return multiplyByNineFifths(celsius)
function multiplyByNineFifths(number)
Thanks to these two uses of multiplyByNifeFifths(), the arguments written inside of its parentheses (‘number’ and ‘Celsius’) are seen as equivalent by the program.
Can we use same parameter names for different functions or is there a danger of causing a conflict of some kind when doing so? Let’s say I have 10 different functions and all of them have a parameter called “number”, but the values for this number will all be different for each function.
Parameters are locally scoped and will shadow any by the same name in outer scope. It won’t create any conflicts with other functions if they use the same parameters.
Recall that parameters get their values from the call argument. We need to be mindful when those arguments are data structures, though. The bindings remain with the object in its scope, regardless what name we give the parameter. If the function mutates the object, it will take affect at the caller.
The exercise shows the idea of a helper function well, however, it seems to me to have turned something simple into something complex for the sake of explaining helper functions.
Could we not just create one function for this scenario?
While the above code works I’m worried I’m missing something by cutting the helper function.
Edit: The above is in relation to converting from one temp to another. I do realize that in some situations (though I can’t imagine one at the moment) where perhaps a helper function can be used in multiple other functions. I just can’t think of any other use that would call for keeping the initial:
function multiplyByNineFifths(number) {
return number * (9/5);