C++ multiple function parameters describe the ability of a function to accept more than one argument or can say functions with multiple parameters to perform operations using several inputs, this feature makes a function to execute more complex operations by working with multiple subset of data at once.
Syntax
In C++, you can define a function with multiple parameters by listing them in the function’s declaration and definition, separated by commas. Here’s the basic syntax −
return_type function_name(param1_type param1_name, param2_type parame2_name,...);
Where,
param1_type and param1_name are parameter type (eg. int, char, bool, string) and its name respectively, similarly param2_type (eg. int, char, bool, string) and param2_name are parameter type and its name respectively and so on.
Data Types for Multiple Function Parameters
There are 2 types of passing data to multiple function parameters, as given in the following −
1. Single Data Type for Multiple Parameters
Functions where all parameters are of the same data type.
Example
Open Compiler
#include <iostream>usingnamespace std;// Function taking multiple parameters or arguments of same data type (int)voidsum(int a,int b,int c){
cout <<"Sum: "<<(a + b + c)<< endl;}intmain(){sum(1,2,3);return0;}
Output
Sum: 6
2. Multiple Data Types for Multiple Parameters
Functions where parameters can have different data types.
Example
Open Compiler
#include <iostream>usingnamespace std;// Function taking arguments of different data typesvoidgetInfo(string name,int age,double height){
cout << name <<" is "<< age <<" years old and "<< height <<" meters tall."<< endl;}intmain(){getInfo("Aman",26,1.78);getInfo("Naman",32,1.65);return0;}
Output
Aman is 26 years old and 1.78 meters tall.
Naman is 32 years old and 1.65 meters tall.
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
Multiple Parameters Passing Techniques
Multiple parameters (or, single) passing techniques in C++ refer to the methods which are used to pass arguments to functions. These techniques define how the data is transferred and manipulated within the function. Here we will discuss few primary techniques −
1. Pass by Value
In pass by value, a copy of the actual parameter’s value is passed to the function. The changes made to the parameter inside the function do not affect the original argument. It is safe and straightforward but can be inefficient for large data structures due to copying.
2. Pass by Reference
This method passes a reference to the actual parameter, allowing the function to modify the original argument. The function works with the original data rather than a copy. It is efficient as it avoids copying, but requires careful handling to avoid unintended modifications.
3. Mutable vs Immutable Types
Mutable Types | Immutable Types |
---|---|
These are types whose instances can be modified after they are created. | These are types whose instances cannot be changed after they are created. |
Lists, dictionaries, and sets are common mutable types. | Integers, strings, and tuples are common immutable types. |
Types of Multiple Function Parameters
In C++, functions are capable of accepting multiple parameters, and these parameters can be categorized in various ways. Here’s a breakdown for different types of multiple function parameters in C++ −
1. Fixed Number of Parameters
A function with a fixed number of parameters where it has a specific and unchanging number of input parameters.
Example
Open Compiler
#include <iostream>usingnamespace std;// Function with a fixed number of argumentsvoidgetDetails(int age,double height,const string& name){
cout <<"Name: "<< name <<", Age: "<< age <<", Height: "<< height << endl;}intmain(){getDetails(25,5.6,"Sam");return0;}
Output
Name: Sam, Age: 25, Height: 5.6
2. Variable Number of Parameters
A function that can accept a variable number of parameters or arguments. This is typically implemented using variadic functions or template parameter packs.
Example
Open Compiler
#include <iostream>usingnamespace std;// Variadic template functiontemplate<typename... Args>voidprintNumbers(Args... args){(cout <<...<< args)<< endl;}intmain(){printNumbers(1,2,3);// Outputs: 123printNumbers(4,5,6,7,8);// Outputs: 45678return0;}
Output
123
45678
3. Default Parameters
The default parameters are the parameters with default values that can be omitted when calling the function. The function uses the default values if no arguments are provided for those parameters.
Example
Open Compiler
#include <iostream>usingnamespace std;// Function with default parametersvoidgreet(const string& name ="Guest",int age =0){
cout <<"Hello, "<< name;if(age >0){
cout <<". You are "<< age <<" years old.";}
cout << endl;}intmain(){greet();// Outputs: Hello, Guestgreet("Alice");// Outputs: Hello, Alicegreet("Bob",30);// Outputs: Hello, Bob. You are 30 years old.return0;}
Output
Hello, Guest
Hello, Alice
Hello, Bob. You are 30 years old.
4. Named Parameters (C++ Specific)
Though C++ does not support named parameters directly, you can achieve similar functionality using a class or struct to encapsulate parameters.
Example
Open Compiler
#include <iostream>usingnamespace std;// Structure to encapsulate parametersstructPerson{
string name;int age;double height;};// Function that takes a parameter objectvoidprintPerson(const Person& p){
cout <<"Name: "<< p.name <<", Age: "<< p.age <<", Height: "<< p.height << endl;}intmain(){
Person alice ={"Alice",25,5.9};printPerson(alice);return0;}
Output
Name: Alice, Age: 25, Height: 5.9
5. Parameter Objects
A single parameter that is a complex type like a class or struct, encapsulating multiple related values.
Example
Open Compiler
#include <iostream>usingnamespace std;// Class to hold multiple parametersclassRectangle{public:int width;int height;Rectangle(int w,int h):width(w),height(h){}};// Function that takes a parameter objectvoidcalculateArea(const Rectangle& rect){
cout <<"Area: "<<(rect.width * rect.height)<< endl;}intmain(){
Rectangle rect(10,5);calculateArea(rect);// Outputs: Area: 50return0;}
Output
Area: 50
Leave a Reply