Function parameters are variables declared in a function definition that allow a function to receive data from the code that calls it. Parameters make functions flexible and reusable because the same function can work with different values.
A parameter is a variable that receives a value when a function is called.
void greet(std::string name) {
std::cout << "Hello " << name;
}
Here, name is a parameter of the function.
Parameter: Variable declared in the function definition.
void greet(std::string name) {
std::cout << name;
}
Here, name is the parameter.
Argument: Actual value passed to the function.
greet("Rahul");
Here, "Rahul" is the argument.
#include <iostream>
void greet(std::string name) {
std::cout << "Hello "
<< name;
}
int main() {
greet("Amit");
return 0;
}
Output:
Hello Amit
Every parameter must have a data type.
void display(int number) {
std::cout << number;
}
Here, number is an integer parameter.
Other common parameter types include double,
char, bool, and std::string.
void display(int age, double height) {
std::cout << "Age = "
<< age
<< std::endl;
std::cout << "Height = "
<< height;
}
A function can receive multiple parameters.
void addNumbers(int a, int b) {
std::cout << a + b;
}
int main() {
addNumbers(10, 20);
return 0;
}
The values 10 and 20 are passed to
a and b.
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(15, 25);
std::cout << result;
return 0;
}
The function receives two parameters and returns their sum.
int square(int number) {
int result = number * number;
return result;
}
The parameter number is available inside the function and
can be used to calculate another local variable.
int square(int number) {
return number * number;
}
int main() {
std::cout << square(2) << std::endl;
std::cout << square(5) << std::endl;
std::cout << square(10);
return 0;
}
The same function can be used with different arguments.
Output:
4
25
100
#include <iostream>
#include <string>
void welcome(std::string name) {
std::cout << "Welcome "
<< name;
}
int main() {
welcome("Soopro");
return 0;
}
A string parameter can receive text data.
void showGrade(char grade) {
std::cout << "Grade = "
<< grade;
}
int main() {
showGrade('A');
return 0;
}
The function receives a single character.
void checkStatus(bool active) {
if (active) {
std::cout << "Active";
}
else {
std::cout << "Inactive";
}
}
int main() {
checkStatus(true);
return 0;
}
A bool parameter can receive true or
false.
int multiply(int a, int b, int c) {
return a * b * c;
}
int main() {
std::cout << multiply(2, 3, 4);
return 0;
}
A function can have several parameters of the same data type.
void student(std::string name, int age, double marks) {
std::cout << "Name: "
<< name
<< std::endl;
std::cout << "Age: "
<< age
<< std::endl;
std::cout << "Marks: "
<< marks;
}
int main() {
student("Rahul", 20, 85.5);
return 0;
}
int add(int a, int b) {
return a + b;
}
int main() {
int x = 10;
int y = 20;
int result = add(x, y);
std::cout << result;
return 0;
}
Variables can be passed as arguments instead of directly writing values.
int multiply(int a, int b) {
return a * b;
}
int main() {
std::cout << multiply(5 + 2, 3 + 1);
return 0;
}
Expressions can also be used as function arguments when their resulting values are compatible with the parameter types.
#include <iostream>
int add(int a, int b);
int main() {
std::cout << add(10, 20);
return 0;
}
int add(int a, int b) {
return a + b;
}
The function declaration specifies the parameter types before the function is used.
Parameter names are optional in a function declaration when only the types are needed.
int add(int, int);
The definition can use parameter names:
int add(int a, int b) {
return a + b;
}
The parameter types must match the intended function signature.
A parameter can have a default value. If an argument is not supplied for that parameter, the default value is used.
void greet(std::string name = "Guest") {
std::cout << "Hello "
<< name;
}
int main() {
greet();
std::cout << std::endl;
greet("Amit");
return 0;
}
The first call uses Guest, while the second call uses
Amit.
int calculate(int a, int b = 10) {
return a + b;
}
int main() {
std::cout << calculate(5)
<< std::endl;
std::cout << calculate(5, 20);
return 0;
}
Output:
15
25
In the first call, the default value 10 is used for b.
Default arguments are generally specified from right to left in a function parameter list.
Valid:
int calculate(int a, int b = 10, int c = 20);
Invalid example:
int calculate(int a = 10, int b, int c);
A required parameter should not follow a parameter that has a default argument in the same parameter list.
By default, function parameters are passed by value. The function receives a copy of the argument.
void change(int number) {
number = 100;
}
int main() {
int x = 10;
change(x);
std::cout << x;
return 0;
}
Output:
10
The original variable remains unchanged because the function modifies its copy.
A reference parameter can allow a function to modify the original variable.
void change(int &number) {
number = 100;
}
int main() {
int x = 10;
change(x);
std::cout << x;
return 0;
}
Output:
100
The reference parameter refers to the original variable.
void swapNumbers(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 10;
int y = 20;
swapNumbers(x, y);
std::cout << "x = "
<< x
<< std::endl;
std::cout << "y = "
<< y;
return 0;
}
The reference parameters allow the function to change the original variables.
A const reference can receive a reference to an object
without allowing the function to modify it through that reference.
void display(const std::string &name) {
std::cout << name;
}
int main() {
std::string name = "Rahul";
display(name);
return 0;
}
This is commonly useful for passing larger objects such as strings without making an unnecessary copy.
const when a reference should not modify the object.int add(int a, int b) {
return a + b;
}
int result = add(10, 20);
#include <iostream>
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
int main() {
int x, y;
std::cout << "Enter first number: ";
std::cin >> x;
std::cout << "Enter second number: ";
std::cin >> y;
std::cout << "Addition = "
<< add(x, y)
<< std::endl;
std::cout << "Subtraction = "
<< subtract(x, y)
<< std::endl;
std::cout << "Multiplication = "
<< multiply(x, y);
return 0;
}
The same input values are passed to three different functions.
#include <iostream>
#include <string>
void showStudent(
std::string name,
int age,
double marks
) {
std::cout << "Name: "
<< name
<< std::endl;
std::cout << "Age: "
<< age
<< std::endl;
std::cout << "Marks: "
<< marks
<< std::endl;
}
int main() {
showStudent("Amit", 19, 86.5);
return 0;
}
Multiple parameters allow one function to receive different pieces of related information.
const references when the function should not modify the object.double calculateTotal(double price, int quantity) {
return price * quantity;
}
| Concept | Meaning |
|---|---|
| Parameter | Variable declared in the function definition. |
| Argument | Actual value passed to the function. |
| Multiple Parameters | A function can receive several values. |
| Default Parameter | Provides a value when an argument is not supplied. |
| Pass by Value | Function receives a copy of the argument. |
| Pass by Reference | Function can work with the original variable. |
| const Reference | Allows efficient reference passing without modification through the reference. |
returnType functionName(type parameter1, type parameter2) {
// statements
}
const references can be used when modification is not required.Question: What is a parameter in a C++ function?