Introduction to Functions in JavaScript
Functions are among the most important constructs in JavaScript. They allow you to group code into small, reusable blocks. Instead of writing the same code again and again, you can write it once inside a function and use it whenever needed.
Understanding functions will help you write cleaner, organized, and professional JavaScript code. For college students and freshers, this chapter is extremely important because functions appear in almost every program, interview question, and project.
What Is a Function?
A function is a block of code designed to perform a specific task.
Think of a function like a machine:
You give something to the machine (input).
It does some work.
It gives you something back (output).
Functions help you:
Avoid repeating code
Make your program organized
Break big problems into small parts
Reuse logic anywhere in the program
Basic Function Structure
A function has two main parts:
Function Definition ? where you write the code
Function Call ? where you use or run the code
Syntax:
function functionName() {
// code
}
Calling the function:
functionName();
Example 1: Simple Function
function greet() {
console.log("Hello, welcome to JavaScript!");
}
greet(); // calling the function
Output:
Hello, welcome to JavaScript!
Example 2: Function That Prints Your Name
function showName() {
console.log("My name is Arjun");
}
showName();
Output:
My name is Arjun
Why Functions Are Useful
Imagine you need to print the same message many times.
Without a function:
console.log("Welcome Student");
console.log("Welcome Student");
console.log("Welcome Student");
With a function:
function welcome() {
console.log("Welcome Student");
}
welcome();
welcome();
welcome();
Cleaner, shorter, and easier to maintain.
Function Parameters and Arguments (Preview)
Sometimes you want a function to work with different values.
We will learn this deeply in the next chapter, but here’s a simple idea:
Parameter ? input placeholder
Argument ? actual value you send
Example:
function greetUser(name) {
console.log("Hello " + name);
}
greetUser("Riya");
greetUser("Karan");
Output:
Hello Riya
Hello Karan
Functions become powerful when you use parameters.
Return Statement (Preview)
Some functions return a value.
Example:
function add(a, b) {
return a + b;
}
let result = add(10, 20);
console.log(result);
Output:
30
We will learn return values in detail later.
Real-Life Example: Function for Billing
function calculateBill(price, quantity) {
let total = price * quantity;
console.log("Total Bill:", total);
}
calculateBill(50, 3);
Output:
Total Bill: 150
Functions make your programs cleaner and reusable.
Example Program With Multiple Function Calls
function showDetails() {
console.log("Name: Meera");
console.log("Course: JavaScript");
console.log("College: XYZ College");
}
showDetails();
showDetails();
Output:
Name: Meera
Course: JavaScript
College: XYZ College
Name: Meera
Course: JavaScript
College: XYZ College
Function ran twice without rewriting the code.
Common Mistakes Beginners Make
Forgetting to call the function
Adding parentheses in function definition (incorrect)
Writing the function call before definition (handled by hoisting but confusing)
Confusing parameter and variable names
Writing functions too long—keep them simple
Practice Tasks (Do It Yourself)
Create a function that prints your name.
Create a function that prints your college and course.
Create a function that prints numbers 1 to 5.
Create a function that prints the square of a number.
Create a function that prints today’s date.