Functions

Functions are some of the fundamental building blocks when developing an application, as you often need to perform the same action in many places. For example, you may want to show a message whenever an error occurs. To avoid repeating the same code all over places, you can use a function to wrap that code and reuse it.

A javascript function is callable object that executes a block of code when “something” invokes, i.e. calls it. To use a function, you must define it somewhere in the scope from which you wish to call it.

In the next exercises, we’ll be invoking a built in function and method.

Alert

A+ presents the exercise submission form here.

Console log

console is an object that has a method called log. Methods are functions stored as object properties. This particular method is very useful to know, as it is a common tool used for debugging, allowing JavaScript to log data to the JavaScript console.

A+ presents the exercise submission form here.

JavaScript provides many built-in functions, such as alert(), and methods, such as console.log(). Since functions are objects, it is possible to assign them to variables, as shown in the example below:

alert("Works!") //"Works!"
const myOwnAlert = alert;
myOwnAlert("Works!")//"Works!"

Next you will learn how to develop your own custom functions.

JavaScript Function syntax

A standard function definition (also called a function declaration, or function statement) consists of the function keyword, followed by:

  • The name of the function. Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).

  • A list of parameters to the function, enclosed in parentheses and separated by commas (parameter1, parameter2, …).

  • The JavaScript statements that define the function, aka the code to be executed, enclosed in curly brackets {}

function name(parameter1, parameter2, parameter3) {
// code to be executed
}

The () Operator Invokes the Function

In the following example, name function is invoked (called) and its return value is assigned to the constant variable gandalfActor.

function name(parameter1, parameter2, parameter3) {
  return parameter1 + " " + parameter2 + " " + parameter3;
}

const gandalfActor = name("Sir", "Ian", "McKellen") //The name-function gets invoked with three arguments
console.log(gandalfActor); //"Sir Ian McKellen"

Accessing a function without () will return the function object instead of the function result.

In the following exercises, you will be going through the following functions: Array-sum, Double number, Half number. Knowledge from previous sections is required (data-types, variables). You can use console.log to debug. You do not need to invoke (i.e. call) the function in these exercises- invoking (calling) is handled by the exercise when attempting to grade.

Array-sum function

A+ presents the exercise submission form here.

Double number function

A+ presents the exercise submission form here.

Half number function

A+ presents the exercise submission form here.