Multiple Function Parameters in C++



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 functions 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

#include <iostream>
using namespace std;

// Function taking multiple parameters or arguments of same data type (int)
void sum(int a, int b, int c) {
   cout << "Sum: " << (a + b + c) << endl;
}

int main() {
   sum(1, 2, 3); 
   return 0;
}
Output
Sum: 6

2. Multiple Data Types for Multiple Parameters

Functions where parameters can have different data types.

Example

#include <iostream>
using namespace std;

// Function taking arguments of different data types
void getInfo(string name, int age, double height) {
   cout << name << " is " << age << " years old and " << height << " meters tall." << endl;
}

int main() {
   getInfo("Aman", 26, 1.78);
   getInfo("Naman", 32, 1.65);
   return 0;
}
Output
Aman is 26 years old and 1.78 meters tall.
Naman is 32 years old and 1.65 meters tall.

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 parameters 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. Heres 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

#include <iostream>
using namespace std;

// Function with a fixed number of arguments
void getDetails(int age, double height, const string& name) {
   cout << "Name: " << name << ", Age: " << age << ", Height: " << height << endl;
}

int main() {
   getDetails(25, 5.6, "Sam");
   return 0;
}
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

#include <iostream>
using namespace std;

// Variadic template function
template<typename... Args>
void printNumbers(Args... args) {
   (cout << ... << args) << endl;
}

int main() {
   printNumbers(1, 2, 3); 
   // Outputs: 123
   printNumbers(4, 5, 6, 7, 8); 
   // Outputs: 45678
   return 0;
}
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

#include <iostream>
using namespace std;

// Function with default parameters
void greet(const string& name = "Guest", int age = 0) {
   cout << "Hello, " << name;
   if (age > 0) {
      cout << ". You are " << age << " years old.";
   }
   cout << endl;
}

int main() {
   greet();                // Outputs: Hello, Guest
   greet("Alice");         // Outputs: Hello, Alice
   greet("Bob", 30);       // Outputs: Hello, Bob. You are 30 years old.
   return 0;
}
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

#include <iostream>
using namespace std;

// Structure to encapsulate parameters
struct Person {
   string name;
   int age;
   double height;
};

// Function that takes a parameter object
void printPerson(const Person& p) {
   cout << "Name: " << p.name << ", Age: " << p.age << ", Height: " << p.height << endl;
}

int main() {
   Person alice = {"Alice", 25, 5.9};
   printPerson(alice);
   return 0;
}
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

#include <iostream>
using namespace std;

// Class to hold multiple parameters
class Rectangle {
   public:
      int width;
      int height;
      Rectangle(int w, int h) : width(w), height(h) {}
};

// Function that takes a parameter object
void calculateArea(const Rectangle& rect) {
   cout << "Area: " << (rect.width * rect.height) << endl;
}

int main() {
   Rectangle rect(10, 5);
   calculateArea(rect);  // Outputs: Area: 50
   return 0;
}
Output
Area: 50
Advertisements