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 { }:
function split3(total) {
let result = total / 3;
return result;
}
console.log(split3(2700));
console.log(split3(450));900 150
| Part | In the example |
|---|---|
| Function name | split3 |
| Parameter: receives the value | total |
| Returning the result | return result; |
| Calling it with the argument 2700 | split3(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:
function sum(num1, num2) {
let result = num1 + num2;
return result;
}
function goodMorning() {
return "Good morning!";
}
console.log(sum(50, 100));
console.log(goodMorning());150 Good morning!
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:
function split5(total) {
let result = total / 5;
return result;
}
console.log(result);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.
let reward = 0;
let contest = 1;
if (contest == 1) {
reward = 300;
}
console.log("You won " + reward + " pounds");You won 300 pounds
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).
Press Run to see the result. The function works out the answer but does not send it back. Fill the blank so it returns result and prints 70.
function calc(num) {
let result = num * 7;
___
}
console.log(calc(10));Press Run to see the result. Above the existing line, create a function goodNight with no parameters that returns the text Good night!.
console.log(goodNight());
Press Run to see the result. Call the function mul with the arguments 9 and 7 and print the result.
function mul(num1, num2) {
let answer = num1 * num2;
return answer;
}
console.log(___);Press Run to see the result. 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.
function split5(total) {
let result = total / 5;
return result;
}
console.log(result);Press Run to see the result. 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).
Press Run to see the result. 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).
Hint
Declare result before the if, give it a value in each branch, then return it after the if.Press Run to see the result. Key points
function name(param) { ... return value; }, then call it withname(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.