Functions in C++ allow data to be passed in through parameters and received as arguments. Understanding how parameters and arguments work is critical for writing flexible and reusable code
What are Function Parameters?
Parameters are placeholders defined in the function signature. They represent the inputs the function expects when it is called.
- Where they Appear: Inside the function definition or declaration (function prototype).
- Key Idea: Parameters define what type of input the function requires.
return_type function_name(parameter_type parameter_name) {
// Function body
// Code to perform a task
return result; // Optional return statement
}
- return_type: Specifies the data type of the value that the function will return (if any).
- function_name: The name of the function.
- parameter_type: The data type of the parameter.
- parameter_name: The name of the parameter.
Example:
int add(int a, int b) {
return a + b;
}
a
andb
are parametersint
is their typeThe function returns the sum of the two inputs
Using Function Parameters
Parameters allow functions to work with different inputs and make their behavior dynamic. When you call a function with parameters, you provide values (called arguments) that match the parameter types and order.
Example:
int add(int a, int b) {
int sum = a + b;
return sum;
}
In this example, the add
function takes two int
parameters, a
and b
, and returns their sum. To use the function, you call it with specific values for a
and b
. a
and b
are the parameters of the function add
.
What are Function Arguments?
Function arguments are the actual
values or expressions that you provide when calling a function. These values are passed to the function's parameters, allowing the function to operate on them.
- Where They Appear: In the function call.
- Key Idea: Arguments are the real data given to the function to work with.
Example:
int result = add(5, 10); // Arguments: 5 and 10
5
→ sent to parametera
10
→ sent to parameterb
Parameters vs. Arguments
Aspect | Parameters | Arguments |
---|---|---|
Definition | Variables in the function definition or prototype. | Actual values passed during a function call. |
Scope | Exist within the function. | Exist during the function call. |
Purpose | Represent inputs the function expects. | Provide inputs to the function. |
Where They Appear | Function signature or body. | Function call. |
Multiple Parameters and Arguments:
A function can have multiple parameters, and when you call the function, you must provide matching arguments for each parameter.
void displayInfo(string name, int age) {
cout << name << " is " << age << " years old.";
}
displayInfo("Alice", 30);
Default Arguments:
C++ allow you to provide default values for parameters, making them optional when calling the function. Parameters with default values are typically specified in the function declaration, not the definition.
Syntax:
void greet(string name = "Guest", int age = 18);
Example:
#include <iostream>
using namespace std;
void greet(string name = "Guest", int age = 18) {
cout << "Hello, " << name << "! You are " << age << " years old.\n";
}
int main() {
greet(); // Both defaults used
greet("Alice"); // age = default
greet("Bob", 25); // No defaults used
}
✅ Output:
Hello, Guest! You are 18 years old.
Hello, Alice! You are 18 years old.
Hello, Bob! You are 25 years old.
Rules for Default Arguments
- Specified from Right to Left:
- Default arguments must be specified starting from the rightmost parameter.
This ensures there is no ambiguity in matching arguments to parameters.
void func(int a, int b = 10, int c = 20); // ✅ Valid void func(int a = 10, int b, int c = 20); // ❌ Invalid
- Overridden by Explicit Arguments:
- If an argument is provided, it overrides the default value.
- Declared in Declarations (Not Both):
- If you declare the function in a header file, you must specify the default arguments there, not in the definition.
Placement of Default Arguments
Best practice is to place them in function declarations, especially if defined in a header file.
Default values should not be repeated in both declaration and definition.
// header file
void showMessage(string message = "Welcome");
// source file
void showMessage(string message) {
cout << message << endl;
}
Tips for Using Parameters and Arguments
Use meaningful names for parameters to improve readability.
Combine default arguments with function overloading carefully to avoid ambiguity.
Prefer passing by const reference (
const T&
) for large objects to avoid unnecessary copies.In C++, use function prototypes with full parameter types to enable type checking.