Lesson 38 of 296
Functions: parameters, arguments and return
What you will learn
- What a function is and why functions matter
- How to write and call a function
- Parameters and arguments
returnand what happens without it- Default parameters
- Scope: where variables can be used (first look)
- Pure functions
On this page
What is a function?
A function is a named, reusable block of code. You write it once and call (run) it whenever you need it.
Why functions matter:
- No repetition. Write the logic once, use it everywhere. This idea is called DRY: Don't Repeat Yourself.
- Clear names.
calculateVat(price)explains itself. - Easy to test and fix. A bug fixed in the function is fixed everywhere it is used.
Writing and calling a function
function greet() {
console.log("Hello!");
}
greet();
greet();Shows Hello! twice.
function greet() { ... }defines (creates) the function. Nothing runs yet.greet()calls the function. The brackets()mean "run it now".
Writing greet without brackets does not run it. It only refers to the function itself.
Parameters and arguments
Functions become useful when you give them input.
function greet(name) {
console.log(`Hello, ${name}!`);
}
greet("Sam");
greet("Aisha");Shows Hello, Sam! then Hello, Aisha!.
| Word | What it is | In the example |
|---|---|---|
| Parameter | The name in the function definition. A placeholder. | name |
| Argument | The real value you pass when calling | "Sam", "Aisha" |
This difference is a common interview question: parameters are in the definition, arguments are in the call.
Several parameters, matched by position:
function introduce(name, city) {
console.log(`${name} lives in ${city}.`);
}
introduce("Sam", "Leeds"); Sam lives in Leeds.
introduce("Leeds", "Sam"); Leeds lives in Sam. (wrong order)Missing and extra arguments
introduce("Sam");Shows Sam lives in undefined. A missing argument becomes undefined. Extra arguments are ignored. JavaScript does not warn you. TypeScript (Part 9) does.
return: giving a value back
console.log only shows a value. return sends a value back to the code that called the function, so it can be saved and used.
function add(a, b) {
return a + b;
}
const result = add(3, 4);
console.log(result); 7
console.log(add(10, 5) * 2); 30return also ends the function straight away. Code after it does not run.
function check(age) {
if (age < 18) {
return "Too young";
}
return "OK";
}No return means undefined
function add(a, b) {
console.log(a + b);
}
const result = add(3, 4); shows 7
console.log(result); undefinedThe function showed 7 but did not return anything. A function with no return gives back undefined. This is one of the most common beginner bugs.
Rule: if a function calculates something, return it. Let the calling code decide whether to print it.
Default parameters
function greet(name = "Guest") {
return `Hello, ${name}!`;
}
greet(); "Hello, Guest!"
greet("Sam"); "Hello, Sam!"The default is used when the argument is missing or undefined. It is not used for null or "".
Scope (first look)
Variables created inside a function only exist inside that function.
function makeTotal() {
const total = 50;
}
makeTotal();
console.log(total); ReferenceError: total is not definedThe area where a variable can be used is called its scope. Part 5 covers scope fully.
A function can read variables from outside it. But it is cleaner to pass everything it needs as arguments.
Pure functions
A pure function:
- always returns the same output for the same input, and
- does not change anything outside itself (no "side effects").
Pure:
function double(n) {
return n * 2;
}
Not pure (changes an outside variable):
let count = 0;
function increase() {
count++;
}Pure functions are easy to test and understand. Use them whenever possible. They matter a lot in React.
A realistic example
function calculateTotal(pricePence, quantity, vatRate = 0.2) {
const subtotal = pricePence * quantity;
const vat = Math.round(subtotal * vatRate);
return subtotal + vat;
}
const total = calculateTotal(1000, 3);
console.log(`Total: £${(total / 100).toFixed(2)}`); Total: £36.00Naming functions
- Start with a verb:
getUser,calculateTotal,isValidEmail,sendEmail. - Functions that return true/false often start with
is,hasorcan. - One function, one job. If the name needs "and" (
validateAndSaveAndEmail), split it.
Common mistakes
- Forgetting the
()when calling. - Using
console.loginstead ofreturn. - Code after
returnthat never runs. - Arguments in the wrong order.
- Functions that do too many things.
How to say it in an interview
"A function is a reusable block of code. Parameters are the names in the definition; arguments are the actual values passed in the call. return sends a value back and exits the function; without it the function returns undefined. I use default parameters for optional values, keep functions small with one job, and prefer pure functions because they are predictable and easy to test."
Key words
| Word | Meaning |
|---|---|
| Function | A named, reusable block of code |
| Call / invoke | Run a function, using () |
| Parameter | The placeholder name in the function definition |
| Argument | The real value passed when calling |
| Return value | The value a function sends back |
| Default parameter | A value used when an argument is missing |
| Scope | Where a variable can be used |
| Pure function | Same input always gives same output, with no side effects |
| Side effect | A change outside the function, such as editing a variable or saving data |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.