🎯 Learning Objectives
- Understand what functions are and why they're useful
- Create functions with parameters
- Use return statements
- Write reusable code
- Build practical function-based programs
📦 What is a Function?
Function is a reusable block of code that performs a specific task. Instead of writing the same code multiple times, you write it once in a function and call it whenever needed.
✍️ Function Declaration
// Basic function
function greet() {
console.log("Hello, World!");
}
// Call the function
greet(); // Output: Hello, World!
greet(); // Output: Hello, World!
📥 Functions with Parameters
Parameters allow you to pass data into functions:
// Function with one parameter
function greetUser(name) {
console.log("Hello, " + name + "!");
}
greetUser("Ahmad"); // Hello, Ahmad!
greetUser("Fatima"); // Hello, Fatima!
// Function with multiple parameters
function addNumbers(a, b) {
let sum = a + b;
console.log(sum);
}
addNumbers(5, 3); // 8
addNumbers(10, 20); // 30
↩️ Return Statement
Functions can return values that you can use elsewhere:
// Function that returns a value
function multiply(x, y) {
return x * y;
}
let result = multiply(5, 4);
console.log(result); // 20
// Use returned value in calculations
let total = multiply(3, 7) + 10;
console.log(total); // 31
🧮 Practical Function Examples
1. Calculator Functions
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function multiply(a, b) {
return a * b;
}
function divide(a, b) {
return a / b;
}
console.log(add(10, 5)); // 15
console.log(subtract(10, 5)); // 5
console.log(multiply(10, 5)); // 50
console.log(divide(10, 5)); // 2
2. String Functions
function createFullName(firstName, lastName) {
return firstName + " " + lastName;
}
function shout(message) {
return message.toUpperCase();
}
let name = createFullName("Ahmad", "Garba");
console.log(name); // Ahmad Garba
let loud = shout("hello");
console.log(loud); // HELLO
3. Validation Functions
function isAdult(age) {
if (age >= 18) {
return true;
} else {
return false;
}
}
console.log(isAdult(20)); // true
console.log(isAdult(15)); // false
// Short version
function canVote(age) {
return age >= 18;
}
console.log(canVote(21)); // true
🔄 Function Expressions
// Store function in a variable
const square = function(num) {
return num * num;
};
console.log(square(5)); // 25
console.log(square(8)); // 64
🚀 Arrow Functions (Modern)
// Arrow function syntax
const greet = (name) => {
return "Hello, " + name;
};
// Short version (one line)
const double = (num) => num * 2;
console.log(greet("Sarah")); // Hello, Sarah
console.log(double(5)); // 10
💻 Complete Practice Example
Function Practice
✏️ Practice Exercises
- Create a function that calculates the area of a circle (π × r²)
- Write a function that converts Celsius to Fahrenheit
- Create a function that finds the largest of three numbers
- Write a function that reverses a string
- Create a discount calculator function (price, discount%)
🎯 Mini Project: BMI Calculator
BMI Calculator
Weight (kg):
Height (m):
✅ Today's Deliverable
- ✓ functions-practice.html with multiple functions
- ✓ At least 5 different functions created
- ✓ Functions with parameters and return values
- ✓ Calculator functions (add, subtract, multiply, divide)
- ✓ Validation function (age checker, even/odd)
- ✓ BMI Calculator mini-project completed
- ✓ All functions tested with console output