LESSON 16

Functions

Learning objective: Package reusable logic into functions.

Understand

Functions are reusable blocks of code you can call by name.

Define a function once, then call it whenever you need it. Functions keep code organized, avoid repetition, and give behaviour a clear name. They are the core building block of larger programs.

Analogy: A function is like a recipe: define the steps once, then cook it any time you like.

See It in Action

Defining and calling:

function greet() {
  console.log("Hi!");
}
greet();
greet();
How it works: greet is defined once but called twice, running its code each time without repeating it.

Try It Yourself

  1. Define a function that logs a message.
  2. Call it more than once.
  3. Rename it to something descriptive.

Quick Quiz

Why use functions?

Challenge

Write a function you can call multiple times to log a greeting.

Success condition: Calling your function runs its code each time.