Functions Overview
- This lesson introduces the concept and importance of functions in JavaScript.
Introduction to Functions
In programming, we often need to perform the same task multiple times.
For example:
Calculating total marks
Validating user login
Printing a report
Reusing logic in different places
Writing the same code again and again is inefficient and error-prone.
To solve this problem, JavaScript provides functions.
What is a Function ?
A function is a block of reusable code designed to perform a specific task.
Instead of writing the same logic repeatedly, we:
Write it once inside a function
Call it whenever needed
Simple Definition
A function is a named block of code that runs only when it is called.
Why Do We Need Functions ?
Functions help us to:
Avoid code repetition
Improve readability
Make code modular
Simplify debugging
Organize large programs
Real-Life Example of a Function
Think of a remote control:
Button = function
Pressing button = calling function
Action happens only when button is pressed
Similarly, a function runs only when it is called.
Basic Syntax of a Function
function functionName() {
// code to be executed
}
Calling a Function
Defining a function does nothing by itself.
The function must be called to execute.function greet() {
console.log("Welcome to JavaScript");
}
greet(); // function call
Output
Welcome to JavaScript
Parts of a Function
A JavaScript function consists of:
Function keyword
Function name
Parentheses ( )
Function body { }
function addNumbers() {
let a = 10;
let b = 20;
console.log(a + b);
}
How Functions Work
JavaScript reads the function definition
Function is stored in memory
Function does not run automatically
Function runs only when called
Functions with and without Parameters
Runs fixed code or accepts input values to produce different outputs.
// Function without parameters
function showMessage() {
console.log("Hello User");
}
showMessage();
showMessage();
// Function with parameters
function greetUser(name) {
console.log("Hello " + name);
}
greetUser("Rahul");
greetUser("Switty");
- Parameters vs Arguments
Term Meaning Parameter Variable listed in function definition Argument Value passed during function call
Function with Return Value
Sends a result back from the function using the return keyword.
// Function returning a value
function add(a, b) {
return a + b;
}
let result = add(5, 3);
console.log(result); // 8
Why Return is Important ?
Sends result back to caller
Stops function execution
Makes functions more useful
Reusable Function
Uses the same function multiple times with different inputs.
// Reusable function
function square(num) {
return num * num;
}
console.log(square(2)); // 4
console.log(square(4)); // 16
console.log(square(6)); // 36
Same logic reused with different values.
Function Naming Rules
Must start with a letter, _, or $
Cannot start with a number
Should be meaningful
Use camelCase convention
Examples:
calculateTotal
getUserName
isValid
Common Mistakes
Forgetting to call the function
Writing logic outside function
Not using return when needed
Using unclear function names
Advantages of Functions
Code reusability
Better structure
Easier maintenance
Faster debugging
Scalable applications
Where Are Functions Used ?
Functions are used in:
Form validation
Calculations
Event handling
API calls
Game logic
Backend services
Types of Functions
JavaScript supports different types of functions:
Normal functions
Functions with parameters
Functions with return values
Arrow functions
Anonymous functions