JavaScript / Functions

Making your own functions

Create a function with parameters and a return value, call it, and know where a variable can be used (scope).

Imagine splitting a restaurant bill between 3 friends every week. Instead of writing the same calculation each time, make your own function once and use it whenever you need it. Write function, then a name, then a parameter in brackets, then the code inside { }:

JavaScript
function split3(total) {
  let result = total / 3;
  return result;
}
console.log(split3(2700));
console.log(split3(450));
Console
900
150
PartIn the example
Function namesplit3
Parameter: receives the valuetotal
Returning the resultreturn result;
Calling it with the argument 2700split3(2700)

When you call split3(2700), the value 2700 goes into the parameter total, the code runs, and return sends the result back to where it was called, so console.log prints it. A function does nothing just by being defined; you have to call it.

Two parameters, or none

If a function needs more than one value, separate the parameters with commas. If it needs none, leave the brackets empty, both when defining and when calling it:

JavaScript
function sum(num1, num2) {
  let result = num1 + num2;
  return result;
}
function goodMorning() {
  return "Good morning!";
}
console.log(sum(50, 100));
console.log(goodMorning());
Console
150
Good morning!
Pass the same number of arguments as there are parameters, in the same order: in sum(50, 100), num1 gets 50 and num2 gets 100. Give the function a name that says what it does, with no dots or spaces.

Scope

Scope is where a variable can be used. A variable declared inside a function's { } does not exist outside it. So this program gives an error:

JavaScript
function split5(total) {
  let result = total / 5;
  return result;
}
console.log(result);
Console
ReferenceError: result is not defined

The fix is to use the value the function returns: console.log(split5(6250)). The same rule applies to the braces of if and for: if you need a variable after the braces, declare it before them.

JavaScript
let reward = 0;
let contest = 1;
if (contest == 1) {
  reward = 300;
}
console.log("You won " + reward + " pounds");
Console
You won 300 pounds

Practice

1Write the programCurriculum practice

Create a function divide6 with the parameter total that stores total ÷ 6 in a variable per and returns it. Then print the result of divide6(72).

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
2Fill in the blankCurriculum practice

The function works out the answer but does not send it back. Fill the blank so it returns result and prints 70.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
function calc(num) {
  let result = num * 7;
  ___
}
console.log(calc(10));
Console
Press Run to see the result.
3Write the programCurriculum practice

Above the existing line, create a function goodNight with no parameters that returns the text Good night!.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
console.log(goodNight());
Console
Press Run to see the result.
4Fill in the blankCurriculum practice

Call the function mul with the arguments 9 and 7 and print the result.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
function mul(num1, num2) {
  let answer = num1 * num2;
  return answer;
}
console.log(___);
Console
Press Run to see the result.
5Edit the programCurriculum practice

The program gives an error because result is declared inside the function. Fix the last line so it prints 6250 divided by 5 using the function.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
function split5(total) {
  let result = total / 5;
  return result;
}
console.log(result);
Console
Press Run to see the result.
6Write the programExtra practice

Create a function finalPrice with two parameters, price and discount (a percentage), that returns the price after the discount: price - price * discount / 100. Print finalPrice(400, 25), then finalPrice(250, 10).

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
7Write the programExtra practice

Create a function checkResult with the parameter score that returns Pass if the score is 50 or more, otherwise Fail. Print checkResult(73), then checkResult(41).

HintDeclare result before the if, give it a value in each branch, then return it after the if.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.

Key points

  • function name(param) { ... return value; }, then call it with name(argument).
  • A function can have one parameter, several, or none; arguments are passed in the same order.
  • A variable declared inside { } cannot be used outside them (scope); use the returned value instead.