-
Hajipur, Bihar, 844101
In C++, functions often need additional information to perform their tasks. This information is passed to the function through parameters. Understanding how parameters work helps you write flexible and reusable programs.
When you call a function, you can provide values that the function uses while running. These values are known as arguments, and the variables that receive them inside the function are called parameters.
Function parameters are variables defined inside a function declaration or definition that receive values (arguments) from the function call.
Example:
#include <iostream>
using namespace std;
void displayMessage(string name) {
cout << "Hello, " << name << "!" << endl;
}
int main() {
displayMessage("Riya");
return 0;
}
Here, name is a parameter, and "Riya" is an argument. The parameter acts as a placeholder for any value passed when the function is called.
C++ allows three main types of parameter passing methods:
This is the most common method. When you pass parameters by value, a copy of the actual value is made and used in the function. Any changes inside the function do not affect the original variable.
Example:
void updateValue(int x) {
x = 100;
}
int main() {
int num = 50;
updateValue(num);
cout << num; // Output: 50
}
The original variable num remains unchanged because only a copy was modified.
In this method, instead of passing a copy, the reference (address) of the variable is passed to the function. Changes made inside the function affect the original variable.
Example:
void updateValue(int &x) {
x = 100;
}
int main() {
int num = 50;
updateValue(num);
cout << num; // Output: 100
}
Here, the value of num changes because both num and x refer to the same memory location.
A pointer can also be used to pass parameters. You pass the address of the variable to the function, and it accesses the value using the pointer.
Example:
void updateValue(int *x) {
*x = 200;
}
int main() {
int num = 50;
updateValue(&num);
cout << num; // Output: 200
}
Here, the *x operator dereferences the pointer to modify the original variable.
A function can take multiple parameters separated by commas.
Example:
#include <iostream>
using namespace std;
void addNumbers(int a, int b) {
cout << "Sum: " << a + b << endl;
}
int main() {
addNumbers(5, 7);
return 0;
}
Here, the function addNumbers() accepts two integer parameters and calculates their sum.
C++ allows assigning default values to parameters. If no argument is provided for that parameter, the default value is used automatically.
Example:
void greet(string name = "User") {
cout << "Hello, " << name << "!" << endl;
}
int main() {
greet(); // Output: Hello, User!
greet("Nisha"); // Output: Hello, Nisha!
}
Default parameters are useful for creating flexible functions with optional arguments.
When you have multiple functions with the same name but different parameter lists (different number or type of parameters), it’s called function overloading.
Example:
void print(int a) {
cout << "Integer: " << a << endl;
}
void print(string b) {
cout << "String: " << b << endl;
}
int main() {
print(10);
print("Hello");
}
C++ automatically determines which version to call based on the argument type.
Arrays can also be passed as parameters to functions. The array name acts as a pointer to the first element.
Example:
void display(int arr[], int size) {
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
}
int main() {
int numbers[] = {1, 2, 3, 4, 5};
display(numbers, 5);
}
The function receives the array reference and its size for processing.
To ensure a parameter cannot be modified inside a function, you can declare it as const. This helps prevent accidental changes to input values.
Example:
void showValue(const int &x) {
cout << "Value: " << x << endl;
}
Here, the function can access but not modify the value of x.
Function parameters make your code reusable and adaptable. Instead of writing the same logic multiple times, you can define one function and pass different arguments to achieve various outcomes.
They also make programs modular, which improves readability and debugging.
Parameters are variables that receive values from function arguments.
You can pass values by value, reference, or pointer.
Default parameters simplify function calls.
Constant parameters prevent unwanted modifications.
Function overloading allows multiple parameter variations.
Understanding parameters is crucial for mastering functions and writing cleaner, more efficient C++ code.
Write a function that takes two integers as parameters and returns their sum.
Create a function that swaps two numbers using pass by value.
Write a function to swap two numbers using pass by reference.
Write a function to swap two numbers using pointers.
Create a function that takes three parameters and returns the largest of them.
Write a program to demonstrate the use of default function parameters.
Create a function that takes an array and its size as parameters and prints all elements.
Write a program where a function takes a string parameter and prints it in reverse.
Write a program to demonstrate constant reference parameters in a function.
Create a program that passes multiple data types (int, float, string) as parameters to a function.