 # 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

8 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.

5 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.

14 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.

9 Likes

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()`.

1 Like
``````{
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.

``````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.

This made so much more sense. I was staring at my screen for 20 minutes on this exercise before I just went on to the next one. Thank you!

1 Like

Hi- I’d like to start with referring to the example shown in “Helper Functions”. A question I have is, how did the number (15) come to be? Thank You.

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.

1 Like

What I have understood is that these functions create a connection between ‘number’ and ‘Celsius’, and I think it’s done by:

1. `return multiplyByNineFifths(celsius)`

2. `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.

I hope that helps.

5 Likes

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.

``````const foo = bar => bar.splice(-1, 1, 10)

const baz = [1,2,3,4,5]

const faz = foo(baz)

console.log(faz)    //  

console.log(baz)    //  [1, 2, 3, 4, 10]
``````

Below we do not mutate the object, but map to it so do not affect the original…

``````const foo = bar => bar.map(x => x * 2)

const baz = [1,2,3,4,5]

const faz = foo(baz)

console.log(faz)    //  [2, 4, 6, 8, 10]

console.log(baz)    //  [1, 2, 3, 4, 5]
``````

Ignore the methods above if they have not been taught yet. They will come up, in due course. For now just let the outcomes sink in.

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?

``````function fahrenheitToCelsius (fahrenheit) {
return fahrenheit * (9/5) + 32
}
console.log(fahrenheitToCelsius(15))
//prints 59
``````

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);
``````