There are two primary ways to define a function, and they behave differently during the Memory Creation Phase (Module 1).
These are hoisted completely. You can call them before they appear in the code.
JavaScript
greet(); // "Hello!" (Works!)
function greet() {
console.log("Hello!");
}
The function is stored in a variable. These are not hoisted.
JavaScript
// sayHi(); // Error: sayHi is not a function
var sayHi = function() {
console.log("Hi!");
};
Arrow functions provide a shorter syntax and have a unique behavior regarding the this keyword (which we will cover in Module 7).
(param) => { code }return and the curly braces.JavaScript
// Traditional
const add = function(a, b) { return a + b; };
// Arrow
const addArrow = (a, b) => a + b;
a, b).5, 10).function welcome(name = "Guest") { ... }...args): Collects multiple arguments into a single array.JavaScriptfunction sumAll(...numbers) { return numbers.reduce((acc, curr) => acc + curr, 0); }JavaScript
(function() {
console.log("I run immediately!");
})();
A function that either takes a function as an argument or returns a function. This is the foundation of functional programming in JS.
JavaScript
function operate(a, b, operation) {
return operation(a, b);
}
const multiply = (x, y) => x * y;
console.log(operate(5, 4, multiply)); // 20
When a function is called, the JS engine creates a new Function Execution Context and pushes it onto the Call Stack.
How it works:
This example shows how functions can return other functions (the basis for “Factories”).
JavaScript
function createMultiplier(multiplier) {
// This is a Higher-Order Function returning an anonymous function
return function(number) {
return number * multiplier;
};
}
const double = createMultiplier(2);
const triple = createMultiplier(3);
console.log(double(10)); // 20
console.log(triple(10)); // 30
A function is a reusable block of code designed to perform a specific task.
Example (conceptual):
calculateSalary()
validateUser()
printReport()
A function generally consists of:
Example structure:
returnType functionName(parameters) {
statements
return value
}
int add(int, int)
int add(int a, int b) {
return a + b
}
Variables defined in function definition.
Actual values passed during function call.
void → returns nothing| Variable Type | Scope |
|---|---|
| Local | Inside function |
| Global | Entire program |
| Static | Retains value across calls |
A function calling itself to solve a problem.
Example:
factorial(n) = n * factorial(n-1)
A stack data structure that stores:
Also called:
Each function call creates a stack frame containing:
Example:
main()
└── funcA()
└── funcB()
Execution order:
Occurs when:
Result:
| Feature | Function | Method |
|---|---|---|
| Belongs to | Standalone | Class/Object |
| OOP concept | Optional | Mandatory |
| Access data | Limited | Object data |
Provided by language.
Example:
print()len()sqrt()Created by programmers.
Multiple functions with same name but different parameters.
Example:
add(int, int)
add(float, float)
(Language-dependent)
Compiler replaces function call with function body.
Advantages:
Disadvantages:
Functions without names.
Used for:
| Feature | Stack | Heap |
|---|---|---|
| Stores | Function calls | Objects |
| Speed | Fast | Slower |
| Size | Limited | Large |
| Management | Automatic | Manual / GC |