What a function is
A function is a reusable block of code that carries out a specific task. It prevents code repetition and makes a program easier to read. The source lesson shows three ways to write one: a function declaration with the function keyword, a function expression assigned to a constant, and an arrow function using the fat arrow syntax. All three create a named block that can be called whenever the task is needed.
Calling a function means writing its name with parentheses. The values placed inside the parentheses are the real inputs. Keeping the definition and the call separate is useful: the same block can serve many different inputs without being copied.
Parameters, arguments and return
Parameters are the placeholders written in the definition; arguments are the actual values sent in the call. A default parameter gives a value when no argument is supplied, as in a function whose parameter is set to ten. Inside the body, the return statement produces the result and ends the function immediately. If no return statement is reached, the function yields an undefined value.
A void function is one that does work but returns no value. This does not mean it does nothing: it may print a message or change something, yet the caller receives no result. Confusing void with an empty body is a common exam mistake.
Scope and callbacks
Scope describes where a name can be seen. Global names are reachable from anywhere; local names exist only inside the function where they were created; block-scoped names declared with let or const live only inside the braces that contain them. This separation protects values from being changed accidentally by unrelated code.
A callback is a function passed as an argument to another function. Array methods such as map, filter and reduce rely on this pattern: they call the supplied function once for each element. Passing a function around this way is a standard technique for keeping code short and readable.
A small worked example
Consider a function that doubles its input. With an argument of seven, the value seven is assigned to the parameter, the body computes seven times two, and the return statement hands back fourteen. The same function would return a different result for any other argument, which is exactly why the block is worth defining once.
When values are passed by value, changes made inside the function do not affect the caller. When they are passed by reference, a change inside the function is reflected outside. Knowing which rule applies prevents surprises when a program grows beyond a few lines.
Frequently asked questions
What are the values expected as input in a function definition called?
The placeholders in the definition are parameters; the real values sent in the call are arguments. The two terms are often mixed up in tests.
What is the return type of a function that performs work but returns no value?
Void. It describes a function that performs an action without sending a result back, not a function that does nothing.
If double(x) returns x times 2, what does double(7) return?
The argument 7 is assigned to x, the body computes 7 times 2, and the return statement gives back 14.
Create a free account — save your progress
Save your notes