Chapter 2 · Decide, repeat, reuse

Functions

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

A function names a piece of work

By the end of this lesson you write functions of your own and know what each call does in memory.

function greet(name) {
  console.log(`Hello, ${name}!`);
}

greet("Ada");
  • function makes a function named greet. Its body, the block in braces, runs only when it is called.
  • name is a parameter: a box for a value each call gives.
  • The call greet("Ada"); runs the body with name set to "Ada", the argument.
function greet(name) {
  console.log(`Hello, ${name}!`);
}
greet("Ada");
greet("Alan");
function double(x) {
  x * 2;
}
function triple(x) {
  return
    x * 3;
}
console.log(double(4), triple(4));
function area(w, h) {
  console.log(w * h);
}

console.log("Area:", area(3, 4));
function grade(score) {
  // return "A", "B", "C" or "F"
}

console.log(95, grade(95));
console.log(72, grade(72));
console.log(55, grade(55));
console.log(40, grade(40));
function addOne(n) {
  n = n + 1;
}

let x = 5;
addOne(x);
console.log(x);
// toFahrenheit(c) returns c * 9 / 5 + 32
// log 0, 25, 100 and -40 as: 0 C is 32 F
function area(w, h) {
  return w * h;
}
console.log(area(3, 4));
console.log(area(3));
greet();

function greet() {
  console.log("Hello from greet");
}

const hello = () => {
  console.log("Hello from hello");
};
hello();

const double = (x) => x * 2;
const add = (a, b) => {
  return a + b;
};

function applyTwice(f, x) {
  return f(f(x));
}

console.log(add(2, 3), applyTwice(double, 3));
// 1. price(age, day = "weekday")
// 2. an arrow function label(name, cost)
// 3. log three tickets
function isPrime(n) {
  // false if a number from 2 to n - 1 divides n
}

for (let n = 2; n < 20; n++) {
  if (isPrime(n)) {
    console.log(n);
  }
}
Terminal
Step 1 of 10One function, two calls

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)