Defining & Calling Functions
Write reusable blocks of code, pass values into them, and use what they return.
By the end, you’ll be able to…
- Distinguish function declarations from function expressions and call both.
- Pass arguments into parameters and use the returned value.
- Explain undefined returns, side effects, and how declarations are hoisted.
01A named block you can reuse
A function is a named block of instructions that performs one job. Instead of repeating the same three lines every time you greet a learner, you define the instructions once and call the function whenever you need them. The declaration below uses the function keyword, a name, a parameter list, and a body. The name greet is how you refer to the work later.
Calling a function means writing its name followed by parentheses. The call runs the body from top to bottom, then control returns to the line after the call. Defining a function does not execute its body; only a call does. This separation is what makes functions reusable and keeps a long program easier to read.
02Parameters are placeholders; arguments are values
The names inside the parentheses of a definition are parameters. They are local placeholders that receive values when the function runs. The values you supply in a call are arguments. In greet('Maya'), the parameter name receives the argument 'Maya'. A function can have several parameters, separated by commas.
A return statement sends a value back to the caller and ends the function immediately. The returned value can be stored, printed, or passed into another call. Without a return statement, a function still finishes, but it produces the special value undefined. Do not confuse printing a value with returning it: output is a side effect, while a return value is data the caller can use.
03Pure work and visible side effects
A pure function returns a value that depends only on its arguments and changes nothing outside itself. area is pure: the same width and height always produce the same result, and no outside variable is touched. Pure functions are easy to test because you can predict their output without preparing a whole application.
A side effect is any observable change beyond returning a value, such as printing, writing to a file, or changing an object owned by another part of the program. Side effects are not automatically bad; a program must eventually show or save something. Keep calculation and display separate when you can, so the calculation stays reusable and the side effect stays easy to locate. Name functions with a verb and a clear object, such as calculateTotal or formatDuration, so a call reads like an instruction.
04Declarations, expressions, and hoisting
A function declaration begins with function and a name, as shown earlier. It is hoisted: the declaration is registered before the surrounding code runs, so you may call it on an earlier line. That convenience is why helper declarations often sit below the code that uses them, although putting them first is easier for beginners to read.
A function expression creates a function as a value and assigns it to a variable, for example const double = function (n) { ... }. The variable follows normal declaration rules, so calling it before the assignment line throws a ReferenceError. Both forms are useful; choose a declaration for a named helper and an expression when you want to pass the function around or keep it inside a smaller scope.
The ideas worth keeping.
- Defining a function prepares work; calling it performs the work.
- Parameters receive arguments, and return sends a value back to the caller.
- Pure functions are predictable, while side effects belong at the edges of a program.
Make it yours.
Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.
Exercise 1: Format a duration
Write a function formatDuration(totalMinutes) that returns '1h 35m', '2h', or '45m' for the matching inputs. Return a string from every path instead of printing inside the function. Print the result for 95 and 120 minutes.
Explore a worked solution
function formatDuration(totalMinutes) { const hours = Math.floor(totalMinutes / 60); const minutes = totalMinutes % 60; if (hours === 0) return minutes + 'm'; if (minutes === 0) return hours + 'h'; return hours + 'h ' + minutes + 'm';}console.log(formatDuration(95));console.log(formatDuration(120));Math.floor gives the whole hours and the remainder operator gives the leftover minutes. The early returns cover the two special cases, and the final return handles a mixed duration. Because the function returns rather than prints, the same result can be reused in a label, a log, or a test.
Exercise 2: Separate calculation from display
Write orderTotal(price, quantity) that returns the cost, and printReceipt(price, quantity) that prints a line containing the total. Call printReceipt(12.5, 4). Confirm that orderTotal itself produces no output.
Explore a worked solution
function orderTotal(price, quantity) { return price * quantity;}function printReceipt(price, quantity) { const total = orderTotal(price, quantity); console.log('Total: ' + total.toFixed(2));}printReceipt(12.5, 4);console.log(orderTotal(2, 3));orderTotal is pure: it only reads its parameters and returns a number. printReceipt owns the side effect, so the calculation remains usable in a checkout summary, a test, or another screen. The output is Total: 50.00 followed by 6.
What stayed with you?
Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.
Exercise 1: Format a duration
Write a function formatDuration(totalMinutes) that returns '1h 35m', '2h', or '45m' for the matching inputs. Return a string from every path instead of printing inside the function. Print the result for 95 and 120 minutes.
1h 35m 2h
Keep exploring.
The official references behind this lesson, when you want a second angle or the full details.
One lesson closer.
Reading is a start. Trying it yourself makes the connection.