Parameters and Arguments
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 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 argumentTwo 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);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.
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");Florachu hits RockachuRockachu hits Florachuattacker hits targetHint : 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`.
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();Level: undefinedLevel: Level: 0Hint : 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.
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
// Write your code here