Functions
Functions
"A function is a named block of code you write once and reuse anywhere — the foundation of every non-trivial program."
Part 1: Defining and Calling Functions
A function packages a set of instructions under a name. You define it once, then call it by that name whenever you need it — avoiding repeated code.
Basic function declaration
function greet() {
console.log("Hello, World!");
}
greet(); // Output: Hello, World!
greet(); // Can call it again!
greet();Parameters — passing data in
Parameters let you send information into the function so it can work with different inputs.
function greetUser(name) {
console.log("Hello, " + name + "!");
}
greetUser("Vivek"); // Hello, Vivek!
greetUser("Priya"); // Hello, Priya!
// Multiple parameters
function add(a, b) {
console.log(a + b);
}
add(3, 7); // 10return — sending data back out
A function without return just does something (prints, modifies). A function with return gives you a value back that you can use.
function multiply(a, b) {
return a * b;
}
let result = multiply(4, 5);
console.log(result); // 20
// You can use the returned value directly
console.log(multiply(10, 3) + 1); // 31
// return stops the function immediately
function checkPositive(n) {
if (n < 0) return "negative";
return "positive";
}Arrow functions — the modern shorthand
// Regular function
function square(n) {
return n * n;
}
// Arrow function — same thing
const square2 = (n) => n * n;
console.log(square(5)); // 25
console.log(square2(5)); // 25
// Multi-line arrow function
const greet = (name) => {
let msg = "Hi, " + name;
return msg;
};Part 2: Scope
Scope determines where a variable is accessible. A variable created inside a function only exists inside that function.
Local vs global scope
let globalVar = "I'm global";
function myFunc() {
let localVar = "I'm local";
console.log(globalVar); // ✅ Can access global
console.log(localVar); // ✅ Fine inside function
}
myFunc();
console.log(globalVar); // ✅ Still accessible
console.log(localVar); // ❌ ReferenceError: localVar is not definedEach function call has its own scope
function counter() {
let count = 0;
count++;
console.log(count);
}
counter(); // 1
counter(); // 1 (not 2! each call starts fresh)
counter(); // 1Part 3: Pass by Value vs Pass by Reference
This is a critical concept for DSA — understanding it prevents subtle bugs.
Primitives — Pass by Value
Numbers, strings, booleans: a copy is passed. The original is never changed.
function doubleIt(n) {
n = n * 2;
console.log("inside:", n);
}
let x = 5;
doubleIt(x);
console.log("outside:", x);
// inside: 10
// outside: 5 (unchanged)Objects/Arrays — Pass by Reference
Arrays and objects: the reference is passed. Modifying inside changes the original!
function addItem(arr) {
arr.push(99);
}
let nums = [1, 2, 3];
addItem(nums);
console.log(nums);
// [1, 2, 3, 99] — original changed!Default parameters
You can set a fallback value for a parameter if none is provided.
function greet(name = "Guest") {
console.log("Hello, " + name + "!");
}
greet("Vivek"); // Hello, Vivek!
greet(); // Hello, Guest!