A function packages a piece of work under a name, so that it can be used again whenever it is needed. Functions are the main way to organise a program.
Declaring a Function
A function is a named block of code. A function declaration starts with the keyword function, followed by the name, the parameters in brackets and the body in braces. Declared functions are available throughout the script, even before the line in which they are written.
Definition: Function. A named block of code that can be run many times and may return a value.
Parameters and Return Values
Parameters are the names given to the inputs of a function. The actual values supplied in a call are called arguments. The statement return sends a value back to the caller and ends the function. A function without a return statement gives back undefined. A parameter can be given a default value, which is used when the caller leaves it out.
Function Expressions
A function can also be stored in a variable. This is a function expression, for example const area = function (w, h) { return w * h; };. Unlike a declaration, it can be used only after the line that creates it.
Arrow Functions
An arrow function is a shorter way to write a function, using the => symbol. When the body is a single expression, the braces and the word return can be left out: const square = n => n * n;. Arrow functions are very common in event handlers and array methods, which later topics use.
Definition: Arrow function. A short way of writing a function using the => symbol.
Table 3.4: Three ways of writing a function
| Form | Example | Note |
|---|---|---|
| Declaration | function sq(n) { return n * n; } |
Usable before its line |
| Expression | const sq = function (n) { return n * n; }; |
Usable after its line |
| Arrow | const sq = n => n * n; |
Short form |
Example 3.4: Reusable maths functions
function area(w, h) { return w * h; }
const square = n => n * n;
const greet = (name = "friend") => "Hello, " + name;
console.log(area(4, 5));
console.log(square(6));
console.log(greet("Asha"));
console.log(greet());
Output:
20
36
Hello, Asha
Hello, friend
Scope and Good Habits
Variables declared with let or const inside a function exist only inside it. This is called local scope, and it keeps one function from changing the values of another. Variables declared outside every function are global, and every function can see them, so too many globals lead to mistakes. Give each function one clear job and a name that begins with a verb, such as calculateTotal. A function that takes inputs and returns a result, without changing anything else, is easy to test and reuse.
Common mistakes
- Printing a result inside a function with console.log when the caller needs a returned value.
- Calling a function expression before the line that defines it.
- Forgetting the brackets when calling, so that the function is not run.
- Writing code after a return statement, which never runs.
Key takeaways
- A function is written once and called many times.
- Parameters are the names, arguments are the values passed in a call.
- return sends a value back and ends the function.
- Arrow functions give a short form, such as n => n * n.
Practice questions
- What is the difference between a parameter and an argument?
- What does a function without return give back?
- Write an arrow function that doubles a number.
Answers to the practice questions
- A parameter is a name in the function definition, and an argument is the actual value passed in a call.
- The value undefined.
- const double = n => n * 2;
Sources
- Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit III
