11.4. Receiving Function Arguments

The previous section illustrates how a function can be passed to another function as an argument. This section takes the opposite perspective to write functions that can take other functions as arguments.

11.4.1. Example: A Generic Input Validator

Our first example will be a generic input validator. It asks the user for some input, using the prompt parameter for the text of the question. A second parameter receives a function that does the actual work of validating the input.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
const input = require('readline-sync');

function getValidInput(prompt, isValid) {

   // Prompt the user, using the prompt string that was passed
   let userInput = input.question(prompt);

   // Call the boolean function isValid to check the input
   while (!isValid(userInput)) {
      console.log("Invalid input. Try again.");
      userInput = input.question(prompt);
   }

   return userInput;
}

// A boolean function for validating input
let isEven = function(n) {
   return Number(n) % 2 === 0;
};

console.log(getValidInput('Enter an even number:', isEven));

Sample Output

Enter an even number: 3
Invalid input. Try again.
Enter an even number: 5
Invalid input. Try again.
Enter an even number: 4
4

When we call getValidInput on line 22, we pass it the string 'Enter an even number:', which gets assigned to the prompt parameter.

Notice that we also pass in the function isEven (with no arguments). This gets assigned to the isValid parameter.

The function getValidInput handles the work of interacting with the user, while allowing the validation logic to be customized. This separates the different concerns of validation and user interaction, sticking to the idea that a function should do only one thing. It also enables more reusable code. If we need to get different input from the user, we can simply call getValidInput with different arguments.

Example

This example uses the same getValidInput function defined above with a different prompt and validator function. In this case, we check that a potential password has at least 8 characters.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const input = require('readline-sync');

function getValidInput(prompt, isValid) {

   let userInput = input.question(prompt);

   while (!isValid(userInput)) {
      console.log("Invalid input. Try again.");
      userInput = input.question(prompt);
   }

   return userInput;
}

let isValidPassword = function(password) {

   // Passwords should have at least 8 characters
   if (password.length < 8) {
      return false;
   }

   return true;
};

console.log(getValidInput('Create a password:', isValidPassword));

Sample Output

Create a password: launch
Invalid input. Try again.
Create a password: code
Invalid input. Try again.
Create a password: launchcode
launchcode

Try It!

  1. Use our getValidInput function to ensure user input starts with "a".

  2. Create another validator that ensures user input is a vowel.

Try it at repl.it

11.4.2. Example: A Logger

Another common example of a function using another function to customize its behavior is that of logging. Real-world applications are capable of logging messages such as errors, warnings, and statuses. Such applications allow for log messages to be sent to one or more destinations. For example, the application may log messages to both the console and to a file.

We can write a logging function that relies on a function parameter to determine the logging destination.

11.4.2.1. A Simple Logger

Example

The logError function outputs a standardized error message to a location determined by the parameter logger.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
let fileLogger = function(msg) {

   // Put the message in a file

}

function logError(msg, logger) {
   let errorMsg = 'ERROR: ' + msg;
   logger(errorMsg);
}

logError('Something broke!', fileLogger);

Let's examine this example in more detail.

There are three main program components:

  1. Lines 1-5 define fileLogger, which takes a string argument, msg. We have not discussed writing to a file, but Node.js is capable of doing so.

  2. Lines 7-10 define logError. The first parameter is the message to be logged. The second parameter is the logging function that will do the work of sending the message somewhere. logError doesn't know the details of how the message will be logged. It simply formats the message, and calls logger.

  3. Line 12 logs an error using the fileLogger.

This is the flow of execution:

  1. logError is called, with a message and the logging function fileLogger passed as arguments.

  2. logError runs, passing the constructed message to logger, which refers to fileLogger.

  3. fileLogger executes, sending the message to a file.

11.4.2.2. A More Complex Logger

This example can be made even more powerful by enabling multiple loggers.

Example

The call to logError will log the message to both the console and a file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
let fileLogger = function(msg) {

   // Put the message in a file

}

let consoleLogger = function(msg) {

   console.log(msg);

}

function logError(msg, loggers) {

   let errorMsg = 'ERROR: ' + msg;

   for (let i = 0; i < loggers.length; i++) {
      loggers[i](errorMsg);
   }

}

logError('Something broke!', [fileLogger, consoleLogger]);

The main change to the program is that logError now accepts an array of functions. It loops through the array, calling each logger with the message string.

As with the validation example, these programs separate behaviors in a way that makes the code more flexible. To add or remove a logging destination, we can simply change the way that we call logError. The code inside logError doesn't know how each logging function does its job. It is concerned only with creating the message string and passing it to the logger(s).

11.4.3. A Word of Caution

What happens if a function expects an argument to be a function, but it isn't?

Try It!

1
2
3
4
5
function callMe(func) {
   func();
}

callMe("Al");

Question

What type of error occurs when attempting to use a value that is NOT a function as if it were one?