Parameters, Arguments, and Default Values
In the previous chapter, you learned the basics of functions—how to define and call them. But functions become much more powerful when you can send values into them. This is where parameters, arguments, and default values come in.
Understanding these concepts is essential for college students and recent graduates because almost every real-world program uses functions that accept inputs.
Let’s make this chapter very simple and clear.
What Are Parameters?
Parameters are placeholders inside a function.
They behave as empty containers that receive values when the function is called.
Example:
function greet(name) {
console.log("Hello " + name);
}
Here, name is a parameter.
What Are Arguments?
Arguments are the actual values you pass to the function when calling it.
Example:
greet("Riya");
Here, "Riya" is the argument.
So:
Parameter ? inside function
Argument ? when calling function
Example 1: Function With One Parameter
function sayHello(name) {
console.log("Hello " + name);
}
sayHello("Aman");
sayHello("Sita");
Output:
Hello Aman
Hello Sita
Example 2: Function With Two Parameters
function add(a, b) {
console.log(a + b);
}
add(10, 20);
add(5, 7);
Output:
30
12
You can pass different arguments each time.
Example 3: Using Return With Parameters
function multiply(x, y) {
return x * y;
}
let result = multiply(5, 4);
console.log(result);
Output:
20
The function returns a value that you can store or print.
Default Parameter Values
Sometimes you want a function to work even if no argument is provided.
In that case, you can set default values.
Syntax:
function functionName(parameter = defaultValue) {
// code
}
Example:
function welcome(name = "Student") {
console.log("Welcome " + name);
}
welcome("Rohan");
welcome(); // no argument
Output:
Welcome Rohan
Welcome Student
If no argument is passed, name becomes "Student" automatically.
Example: Function With Multiple Default Values
function bill(amount = 0, tax = 5) {
console.log("Total:", amount + tax);
}
bill(100, 10);
bill(50);
bill();
Output:
Total: 110
Total: 55
Total: 5
This makes your function flexible and safe.
Real-Life Examples
1. Calculate the area of a rectangle
function area(length, width) {
return length * width;
}
console.log(area(10, 5));
Output:
50
2. Print student details
function studentDetails(name, course) {
console.log("Name:", name);
console.log("Course:", course);
}
studentDetails("Meera", "BCA");
Output:
Name: Meera
Course: BCA
3. Greeting with default value
function greetUser(name = "Guest") {
console.log("Hello " + name);
}
greetUser();
greetUser("Karan");
Output:
Hello Guest
Hello Karan
Common Mistakes Beginners Make
Writing more arguments than parameters
Forgetting to pass arguments while calling
Mixing up argument order
Confusing return with console.log
Forgetting default values
Practice Tasks (Do It Yourself)
Create a function that takes two numbers and prints their sum.
Create a function that prints your name and city.
Create a function that multiplies two numbers using return.
Create a function with default values for name and age.
Create a function to calculate the simple interest (P, R, T).