Javascript · Functions (Basics)
Parameters and Arguments
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Making Functions Flexible
This function always greets the same Codémon. To greet another one, you'd need a second function... not very DRY:
function greetAquachu() { console.log("Hello, Aquachu!"); } greetAquachu();
A parameter is a variable declared between the parentheses. Its value is chosen at each call, so one function can handle every Codémon:
function greet(name) { console.log("Hello, " + name + "!"); } greet("Aquachu"); greet("Pyrochu"); greet("Zap");
Exercise 2 — Parameter vs Argument
function attack(damage) { // damage is a parameter // ... } attack(25); // 25 is an argument
Two words for two sides of the same thing: 1. The parameter is the name written in the declaration: `damage` 2. The argument is the actual value passed in the call: `25` At each call, the parameter receives the argument, and the body runs with that value.
function attack(damage) { console.log("Pyrochu deals " + damage + " damage!"); } attack(25); attack(40);
Exercise 3 — Quiz: Name That Thing
In this code, what is `20`?
function heal(amount) { console.log("+" + amount + " HP"); } heal(20);
Options
• An argument
• A parameter
• A return value
• A function name
Hint : One word is for the declaration, the other for the call.
Exercise 4 — Shout It
Give the function a parameter named `word` and use it in the message.
function shout(____) { console.log(____ + "!!!"); } shout("Zap"); shout("Thunder");
Options: word word "word" text
Hint : The same name appears in the parentheses and inside the body, without quotes.
Exercise 5 — Several Parameters
A function can take several parameters, separated by commas. The arguments are matched in order: first with first, second with second.
function introduce(name, level) { console.log(name + " is level " + level); } introduce("Zap", 5); introduce("Metachu", 12);
Swap the arguments and JavaScript won't complain: it just produces nonsense. The order is your responsibility.
function introduce(name, level) { console.log(name + " is level " + level); } introduce(5, "Zap");
Exercise 6 — Quiz: Right Order
What does this code print?
function report(attacker, target) { console.log(attacker + " hits " + target); } report("Florachu", "Rockachu");
Options
• ```Florachu hits Rockachu```
• ```Rockachu hits Florachu```
• ```attacker hits target```
• An error
Hint : Match each argument with the parameter in the same position.
Exercise 7 — Three Parameters
Complete the declaration and the call so the message reads `Pyrochu hits Aquachu for 30`.
function dealDamage(attacker, target, ____) { console.log(attacker + " hits " + target + " for " + damage); } dealDamage("Pyrochu", ____, 30);
Options: damage "Aquachu" Aquachu 30 target
Hint : The body already uses a name that must exist as a parameter, and a name in the call needs quotes to be text.
Exercise 8 — Missing or Extra Arguments
If you pass fewer arguments than there are parameters, the missing ones are `undefined`. No error, just a strange message:
function greet(name) { console.log("Hello, " + name + "!"); } greet();
Extra arguments are simply ignored:
function greet(name) { console.log("Hello, " + name + "!"); } greet("Zap", "Metachu", 42);
Exercise 9 — Quiz: Forgotten Argument
What does this code print?
function showLevel(level) { console.log("Level: " + level); } showLevel();
Options
• ```Level: undefined```
• ```Level: ```
• ```Level: 0```
• An error
Hint : What value does a parameter have when nothing was passed for it?
Exercise 10 — Any Value Can Be an Argument
An argument doesn't have to be written by hand. A variable, a calculation, or even an array works:
function announce(level) { console.log("Reached level " + level); } let current = 4; announce(current); announce(current * 2);
Passing an array lets a function work on a whole team at once:
function showTeam(members) { for (let cdm of members) { console.log("- " + cdm); } } const team = ["Aquachu", "Pyrochu", "Zap"]; showTeam(team);
Exercise 11 — Parameters Cheat Sheet
Match each term with its description.
Items to match
• Parameter
• Argument
• Missing argument
• Extra argument
Definitions
• A name in the declaration
• A value in the call
• The parameter is undefined
• Ignored
Exercise 12 — Attack Report
Write a function `attack` with three parameters: the attacker's name, the target's name and the damage. 1. It prints `"X attacks Y!"` 2. Then it prints `"Y loses Z HP"` 3. Call it for Pyrochu hitting Florachu for 30, then for Aquachu hitting Pyrochu for 45
Starter code
// Write your code here
Back to the course