1

Beginner in JS, learning from a book, passing functions example is not clear to me. This is the code:

function toCentigrade(degFahren) {
  var degCent = 5 / 9 * (degFahren - 32);
  document.write(degFahren + " Fahrenheit is " +
    degCent + " Celsius.<br/>");
}

function toFahrenheit(degCent) {
  var degFahren = 9 / 5 * degCent + 32;
  document.write(degCent + " Celsius is " +
    degFahren + " Fahrenheit.<br/>");
}

function convert(converter, temperature) {
  converter(temperature);
}
convert(toFahrenheit, 23);
convert(toCentigrade, 75);
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Chapter 4, Example 2</title>
</head>

<body>
</body>

</html>

I don't understand the part with function convert. This is the explanation from the book:

Admittedly, you could use these functions as is without any problem, but that wouldn’t result in a very interesting example. Instead, the third function, convert(), will be used to execute toCentigrade() and toFahrenheit():

function convert(converter, temperature) {
 return converter(temperature);
}

This function takes the first parameter, converter, and uses it as a function. The second parameter, temperature, is then passed to converter() to perform the conversion and write the results to the document. The final two lines of code use convert() and pass it the appropriate converter function and temperature value:

convert(toFahrenheit, 23);
convert(toCentigrade, 75);

Although this is certainly a more complex solution to a relatively simple problem, it demonstrates the fact that functions are values in JavaScript. We can assign them to variables and pass them to other functions.

What I don't understand is probably the essence of this example - why the parameters are (converter, temperature), how can we assign temperature parameter to converter (which is now a function inside a function) when temperature is not defined - it's just a word (for me), how after that convert function is used for converting (toFahrenheit, 23 and toCentigrade, 75): for example, the function toFahrenheit knew that it should look up to convert function for a number needed for writing conversion result to a webpage, how? I mean, how exactly we connected all three functions in this example which resulted in printing the results to a webpage? Basically, I'm lost with all of this part:

function convert(converter, temperature) {
   converter(temperature);
}
convert(toFahrenheit, 23);
convert(toCentigrade, 75);
Barmar
  • 596,455
  • 48
  • 393
  • 495
pilgrim011
  • 47
  • 7
  • The author wrote in the book: "This function takes the first parameter, converter, and uses it as a function." – pilgrim011 Jul 07 '15 at 11:25
  • The most important thing to know about JavaScript is that EVERYTHING is an object, whether that be a function, a reference to an object or a plain value such as an integer or a string. That's why you can pass functions around just like any other object. – Ted Nyberg Jul 07 '15 at 11:27
  • @TedNyberg: Primitive values are not objects. – Felix Kling Jul 07 '15 at 11:57
  • Sure they are, otherwise they wouldn't have a prototype. – Ted Nyberg Jul 07 '15 at 14:16

4 Answers4

0

converter and temperature are passed in as parameters. converter is a function which is then called with temperature as a parameter.

It isn't a stupid question.

user2182349
  • 8,572
  • 3
  • 22
  • 36
0

In JavaScript, you can hold function in a variable, for ex.

var fun=function(){
}

And you can pass this function to another function as normal variable.(This is also known as callback function)

function2(fun);

you can execute fun from function2. This will work like function pointer.(But Not exactly function pointer)

Here you are calling convert function and in that you are executing callback function with second parameter of convert function.

Laxmikant Dange
  • 6,983
  • 4
  • 36
  • 60
0
convert(toFahrenheit, 23);

passes in the toFahrenheit function as a parameter to the convert function. Therefore:

function convert(converter, temperature) {
  converter(temperature);
}

the parameter converter above is simply a reference to the toFahrenheit function. So, in this example, the code:

converter(temperature);

is the same as:

toFahrenheit(temperature);

I presume the author of your book is just trying to show you that functions can be passed as arguments if required.

Ash
  • 2,018
  • 2
  • 15
  • 22
  • Thank you. Just one more thing - how do we connect/exchange (temperature) with (23)? In other words, how do we assign numeric value to parameter in this example? – pilgrim011 Jul 07 '15 at 22:30
  • When you call the convert function you are passing through an integer value (in this case 23) as the second parameter to the function (temperature). So converter(temperature) is the same as converter(23). Hope that answers your question. – Ash Jul 08 '15 at 07:58
0

Maybe it would be more clear to you using the alternative syntax.

So, both of these codes are equivalent (to be true there is just a tiny difference, but let's ignore it, that's not the point) :

function toCentigrade(degFahren) {
    var degCent = 5 / 9 * (degFahren - 32);
    document.write(degFahren + " Fahrenheit is " + degCent + " Celsius.<br/>");
}

var toCentigrade = function(degFahren) {
    var degCent = 5 / 9 * (degFahren - 32);
    document.write(degFahren + " Fahrenheit is " + degCent + " Celsius.<br/>");
}

When you call convert(toFahrenheit, 23), you pass a function and a value to the convert function.

convert function doesn't care on how you call it or how it's arguments are named outside, inside convert you just know that converter is a function object that takes one argument and temperature is some numeric value.

Community
  • 1
  • 1
Sebastien C.
  • 4,122
  • 1
  • 17
  • 31