Skip to Content
Learn
Functions
Helper Functions

We can also use the return value of a function inside another function. These functions being called within another function are often referred to as helper functions. Since each function is carrying out a specific task, it makes our code easier to read and debug if necessary.

If we wanted to define a function that converts the temperature from Celsius to Fahrenheit, we could write two functions like:

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

In the example above:

  • 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).

We can use functions to section off small bits of logic or tasks, then use them when we need to. Writing helper functions can help take large and difficult tasks and break them into smaller and more manageable tasks.

Instructions

1.

In the previous exercise, we created a function to find the number of monitors to order for an office. Now let’s write another function that uses the monitorCount function to figure out the price.

Below monitorCount Create a function declaration named costOfMonitors that has two parameters, the first parameter is rows and the second parameter is columns. Leave the function body empty for now.

2.

Time to add some code to the function body of costOfMonitors to calculate the total cost.

Add a return statement that returns the value of calling monitorCount(rows, columns) multiplied by 200.

3.

We should save the cost to a variable.

Declare a variable named totalCost using the const keyword. Assign to totalCost the value of calling costOfMonitors() with the arguments 5 and 4 respectively.

4.

To check that the function worked properly, log totalCost to the console.

Folder Icon

Take this course for free

Already have an account?