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

or where the body of your function is getting so large, you need to split it into different smaller functions. Having separate functions makes its easier to test the different functions/components.

In this lesson, the helper function doesn’t add much value, but explains the concept nicely

1 Like

That would be F degrees, with C degrees being 15.

The idea I get from helper functions is that we can abstract the maths away from the general function.

const cToF = c => 9 * c / 5 + 32

const fToC = f => 5 * (f - 32) / 9
function fahrenheitToCelsius (fahrenheit) {
  return fToC(fahrenheit)
}
function celsiusTofahrenheit (celsius) {
  return cToF(celsius)
}

Not the best examples, but one can get the idea.

1 Like

I see! There is defiantly an underlining, but not quite outright said/shown, lesson of code stability and how you build the code up for reliability and readability. For @stetim94 , I understand now that debugging a huge Function code block could be a nightmare when combing too many…I wanna use the term expressions or operations. Combo that with what @mtf pointed out and it sounds like way better practice to

“(keep)…abstract…maths away from the general function.”

Thus creating a more coherent function for people trying to read it and figure out what it’s doing. As well as (apologies if I’m repeating) probably making it easier to find errors.

If my interpritation is off let me know but this seems to have explained things very nicely. As always, thank you! At the risk of sounding cheesy; You all make this community wonderful!

I also couldn’t get where the connexion between the two functions was. So I run the code as it was presented and it didn’t give any outcome. I suspect there must be a mistake there?

@carchaz34798479806 Because the code doesn’t output anything to the console. :slight_smile: Try this:

function multiplyByNineFifths(number) {
    return number * (9/5);
};
  
function getFahrenheit(celsius) {
    return multiplyByNineFifths(celsius) + 32;
};

console.log(getFahrenheit(15)); // Returns 59 and logs to console

Recently a friend happened to explain this concept to me, which was very helpful and so now I’d wish to share it with you :slight_smile:

The way he explained it to me is that it actually doesn’t matter much what parameter names you use. Descriptive parameter names are just easier to read for us humans. Do keep in mind that parameters behave like variables within functions. So it’s important to use the same parameter names within your function to refer to the correct values. Whatever those values might be…

And that brings us to arguments. You could store an argument in a variable and pass the variable into the function. The name of the variable doesn’t however matter to the computer. Because in the end you’re passing in an integer (a number) or a string for that matter. You could just as easily pass in an integer directly as an integer (so pass in a number as a number).

Below you could easily replace argument2 for num1 and argument1 for num2 and all would make sense for us humans again :smile:

let argument2 = 2;
let argument1 = 1;

function sum(parameter1, parameter2) {
    return parameter1 + parameter2;            // return 2 + 1
}

sum(argument2, argument1);                     // Returns 3

Or you could pass in the numbers directly.

function sum(parameter1, parameter2) {
    return parameter1 + parameter2;            // return 2 + 1
}

sum(2, 1);                                     // Returns 3

THE EXAMPLE FROM THE EXCERSIZE
Because celsius behaves like a variable within getFahrenheit you can pass it to multiplyByNineFifths as an argument when calling the function. Why use celsius? Because celsius is also used as parameter for getFahrenheit. That’s how multiplyByNineFifths knows to use 15 as an argument when being called.

Why not use celsius as a parameter in multiplyByNineFifths you say? Because that function could just as well be used to calculate nine-fifth of a distance walked in kilometers. :walking_man:

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

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

getFahrenheit(15); // Returns 59

Below I altered the example from above to illustrate all we talked about. In general keep in mind what your function expects to receive and try to keep your code readable. So multiplyByNineFifths is fine as is. However, below I’m looping through an array. There I’d change item to celsius for readability.

Sometimes however, you need to store a value in another variable before passing it to a function. I do this with zeroCelsius, 'cause I use celsius again in the code below it. Things would break otherwise. :boom:

let celsius = 15;
let celsiusArray = [15, 45, 90];

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

function getKelvin(celsius) {
    return celsius + 273.15;
};

function getFahrenheit(celsius) {
    let fahrenheit = multiplyByNineFifths(celsius) + 32;
    let message = "It's " + fahrenheit + ' deg. Fahrenheit.'

    let zeroCelsius = celsius - celsius; // Changing zeroCelsius to celsius
    let kelvin = getKelvin(zeroCelsius); // would break the code.
    let fact1 = 'Fact: ' + zeroCelsius + ' deg. Celsius is ' + kelvin + ' deg. Kelvin.';
    
    kelvin = getKelvin(celsius); // Could use: let kelvin2 = getKelvin(celsius)
    let fact2 = 'Fact: ' + celsius + ' deg. Celsius is ' + kelvin + ' deg. Kelvin.';
    return message + ' \n' + fact1 + ' \n' + fact2;
};
console.log(getKelvin(celsius)); // 288.15

console.log(getFahrenheit(celsius));
// It's 59 deg. Fahrenheit. 
// Fact: 0 deg. Celsius is 273.15 deg. Kelvin. 
// Fact: 15 deg. Celsius is 288.15 deg. Kelvin.

for (const item of celsiusArray) {    // Personally I would use celsius, 
    console.log(getFahrenheit(item)); // but you could use item like so.
};
// It's 59 deg. Fahrenheit. 
// Fact: 0 deg. Celsius is 273.15 deg. Kelvin. 
// Fact: 15 deg. Celsius is 288.15 deg. Kelvin.

// It's 113 deg. Fahrenheit. 
// Fact: 0 deg. Celsius is 273.15 deg. Kelvin. 
// Fact: 45 deg. Celsius is 318.15 deg. Kelvin.

// It's 194 deg. Fahrenheit. 
// Fact: 0 deg. Celsius is 273.15 deg. Kelvin. 
// Fact: 90 deg. Celsius is 363.15 deg. Kelvin.

Alright so I hope all this helps you guys as much as it helped me. Happy coding! :nerd_face:

You can’t call the result by using ‘getFahrenheit(15)’ in this case right? Why the lesson shows it ‘Returns 59’ as a result meanwhile it shows nothing in the console panel? But in fact, you must use ‘console.log()’ to call it out. How come they used the wrong code as an example?

@java5162140166 I get the confusion. It is however the correct code. The lesson states it Returns 59, which it actually does. It returns the result from the function to the call. Nothing is then being done with that result. Indeed you could then wrap the function call into a console.log(); to have the result printed to the console. Hope this helps :slight_smile: