E-Learning Platform
0 of 139 builtAlways freeSign in
← Web Foundation I
3.03

Functions

On this stop
  • Functions
  • Parameters & return values

Every variable so far holds one fixed value, sitting there. A function is different — it's a whole circuit you wire once, then trigger — call — as many times as you like, from anywhere in your script.

Wiring the circuit

Up closeJavaScript
function greet() {
  return "Hello!";
}

function declares the circuit. greet is its name. The parentheses () are where inputs will go — empty for now. Everything between the curly braces { } is what happens when the circuit fires. return hands a value back out, the moment the function finishes running.

Declaring a function doesn't run it — it just wires it up and waits. Nothing happens until you flip the switch:

Up closeJavaScript
greet(); // "Hello!"

That's a call: the name, followed by parentheses, which is what actually triggers the circuit.

Feeding it inputs: parameters

An empty-parentheses function always does the same thing. Give it a slot for input instead, and it adapts every time you call it:

Up closeJavaScript
function greet(name) {
  return "Hello, " + name + "!";
}

greet("Nova"); // "Hello, Nova!"
greet("Milo"); // "Hello, Milo!"

name here is a parameter — a placeholder wire inside the function, filled in fresh each time you call it. "Nova" and "Milo" are the arguments — the actual values you hand over at call time. Same circuit, different input, different result.

What comes back out: return

return is what makes a function's result usable elsewhere — you can store it in a variable, just like any other value:

Up closeJavaScript
let message = greet("Nova");

message now holds "Hello, Nova!", ready to use. A function with no return still runs fine, it just hands back undefined — nothing to catch.

Common mistake

greet and greet() are not the same thing. greet just points at the function itself, sitting there, unfired. greet() — with the parentheses — is the actual call, the one that flips the switch and runs it. Forgetting the parentheses is the single most common way a function "does nothing" the first time you use one.

You've now got reusable logic with adjustable inputs and a result you can use. Next: giving that logic a way to decide — different behavior for different situations.

Three of them are waiting in the lab.