Javascript · Functions (Basics)
Calling Functions
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Call It as Often as You Like
Once declared, a function can be called any number of times. Each call runs the whole body again, from the top:
function cheer() { console.log("Go Aquachu, go!"); } cheer(); cheer(); cheer();
And since a call is just a line of code, it can live inside a loop:
function cheer() { console.log("Go Aquachu, go!"); } for (let i = 0; i < 5; i++) { cheer(); }
Exercise 2 — Calling Before Declaring
Surprising but true: a function declared with `function` can be called before its declaration. JavaScript reads all declarations first, then runs the code.
startBattle(); function startBattle() { console.log("A wild Rockachu appears!"); }
Most people still declare functions at the top and call them below: it's easier to read. But it's good to know the order isn't an error.
Exercise 3 — Quiz: How Many Times?
How many lines does this code print?
function train() { console.log("Training..."); } for (let day = 1; day <= 3; day++) { train(); } train();
Options
• ```4```
• ```3```
• ```1```
• ```5```
Hint : Count the calls inside the loop, then don't forget what comes after it.
Exercise 4 — Three Sessions
Make the training run exactly three times.
function train() { console.log("Rockachu lifts a boulder"); } for (let i = 0; i < ____; i++) { train____; }
Options: 3 () 2 4 [] ;
Hint : The loop decides how many times, and the call needs its usual ending.
Exercise 5 — Functions Calling Functions
A function body can contain calls to other functions. This lets you build big actions out of small ones:
function showIntro() { console.log("A wild Florachu appears!"); } function chooseCodemon() { console.log("Go, Pyrochu!"); } function startBattle() { showIntro(); chooseCodemon(); console.log("Battle start!"); } startBattle();
Calling `startBattle` runs `showIntro`, then `chooseCodemon`, then the last line. Each call finishes completely before the next line runs.
Exercise 6 — Quiz: In What Order?
What does this code print?
function second() { console.log("B"); } function first() { console.log("A"); second(); console.log("C"); } first();
Options
• ```A```, ```B```, ```C```
• ```A```, ```C```, ```B```
• ```B```, ```A```, ```C```
• ```A``` only
Hint : Follow the calls line by line, entering each function when it's called.
Exercise 7 — The Forgotten Parentheses
Writing the name without parentheses doesn't call the function. JavaScript just looks at it and moves on, without any error:
function attack() { console.log("Zap uses Spark!"); } attack; // nothing happens attack(); // this runs it
Only one message, from the second line. If a function "doesn't work", check the parentheses first.
Exercise 8 — Battle Rounds
Run round one, then round one again, then round two.
function roundOne() { console.log("Round 1: Pyrochu attacks"); } function roundTwo() { console.log("Round 2: Aquachu counters"); } ____(); ____(); ____();
Options: roundOne roundTwo roundOne roundTwo roundThree round
Hint : Follow the requested order exactly, one call per line.
Exercise 9 — Call or Not?
Match each line with what it does.
Items to match
• train();
• train;
• train(); inside a loop of 5 turns
• A call written above the declaration
Definitions
• Runs the function once
• Does nothing
• Runs the function 5 times
• Works anyway
Exercise 10 — Battle Script
Write the script of a battle out of small functions. 1. Declare `intro` that prints `"A wild Metachu appears!"` 2. Declare `attack` that prints `"Pyrochu uses Flame!"` 3. Declare `outro` that prints `"Metachu is KO!"` 4. Declare `battle` that runs the intro, then three attacks, then the outro 5. Call `battle` once
Starter code
// Write your code here
Back to the course