 Helper Functions: Order of Statements

I am having trouble understanding the order/flow in the examples for the Helper Functions, both the Celsius to Fahrenheit example and the Monitor Cost exercise. Here is the the Fahrenheit example:

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

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

getFahrenheit(15); // Returns 59

The explanation for the order/flow is explained as:

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

My question: Why wouldn’t we put the getFahrenheit statement first, above the multiplyByNineFifths statement? It seems out of order to me, according to the explanation of how the functions are called. Isn’t step 2 starting in the middle, with function getFahrenheit(celsius)… ? (Therefore it would make sense to reverse the order of the statements?)

Thank you! (Apologies if the formatting doesn’t come out right. I am a first-time poster, and I don’t know if it will let me preview/edit.)

No reason. They can be written in any order, even below where they are first called owing that they are function declarations.

The question was about, ‘flow’, so we should address that, too. Flow relates to the current statement and the statement following. Control flows in that order. That is why we call it, control flow.

Functions only occupy space in memory until such time as they are called, or invoked. Program flow is now channeled into the function such that the first line now has control. We may think of ‘control’ as loosely defined to be the statement with the pointer to the next statement. The return statement knows where the next statement is in calling scope, so it hands control over and the function dies.

Aside

This exercise calls into view the concept of helper functions. There are two kinds of helpers, global and local. We would chose the former if there is some guarantee that it will be accessed from elsewhere in the program. The latter is useful when the helpers are dedicated to the calling (enclosing) function.

For this exercise we might well choose the latter and write a parent function to handle temperature conversion. Which helper is called internally will depend upon the user input.

const tempConvert = function (f=32, c=0) {
const cf = c => ...;
const fc = f => ...;
if (f) {
return fc(f)
}
return cf(c)
}

Now dream up some calls. To convert C to F, use (false, %C) where %C is interpreted as some centigrade degree. To convert F to C just input the F degrees. We’ll leave you to fill in the formula for each helper.

const tempConvert = function (f=32, c=0) { const cf = c => 1.8 * c + 32; const fc = f => 5 * (f - 32) / 9; if (f) { return fc(f) } return cf(c) } console.log(tempConvert()) console.log(tempConvert(false)) console.log(tempConvert(32))

Thank you so much for the detailed response! The code was already filled in for me, but I can see how the helper functions function. I’ll keep practicing.

1 Like

Apart from functions which channel flow and resume from where they are called, if statements and switch statements channel flow through a conditional branch, and loops repeat conditionally. All are aspects of control flow.

1 Like