
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Hello World
- C++ Omitting Namespace
- C++ Tokens
- C++ Constants/Literals
- C++ Keywords
- C++ Identifiers
- C++ Data Types
- C++ Numeric Data Types
- C++ Character Data Type
- C++ Boolean Data Type
- C++ Variable Types
- C++ Variable Scope
- C++ Multiple Variables
- C++ Basic Input/Output
- C++ Modifier Types
- C++ Storage Classes
- C++ Numbers
- C++ Enumeration
- C++ Enum Class
- C++ References
- C++ Date & Time
- C++ Operators
- C++ Arithmetic Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- C++ Assignment Operators
- C++ sizeof Operator
- C++ Conditional Operator
- C++ Comma Operator
- C++ Member Operators
- C++ Casting Operators
- C++ Pointer Operators
- C++ Operators Precedence
- C++ Unary Operators
- C++ Control Statements
- C++ Decision Making
- C++ if Statement
- C++ if else Statement
- C++ Nested if Statements
- C++ switch Statement
- C++ Nested switch Statements
- C++ Loop Types
- C++ while Loop
- C++ for Loop
- C++ do while Loop
- C++ Foreach Loop
- C++ Nested Loops
- C++ break Statement
- C++ continue Statement
- C++ goto Statement
- C++ Strings
- C++ Strings
- C++ Loop Through a String
- C++ String Length
- C++ String Concatenation
- C++ String Comparison
- C++ Functions
- C++ Functions
- C++ Multiple Function Parameters
- C++ Recursive Function
- C++ Return Values
- C++ Function Overloading
- C++ Function Overriding
- C++ Default Arguments
- C++ Arrays
- C++ Arrays
- C++ Multidimensional Arrays
- C++ Pointer to an Array
- C++ Passing Arrays to Functions
- C++ Return Array from Functions
- C++ Structure & Union
- C++ Structures
- C++ Unions
- C++ Pointers
- C++ Pointers
- C++ Dereferencing
- C++ Modify Pointers
- C++ Class and Objects
- C++ Object Oriented
- C++ Classes & Objects
- C++ Class Member Functions
- C++ Class Access Modifiers
- C++ Static Class Members
- C++ Static Data Members
- C++ Static Member Function
- C++ Inline Functions
- C++ this Pointer
- C++ Friend Functions
- C++ Pointer to Classes
- C++ Constructors
- C++ Constructor & Destructor
- C++ Default Constructors
- C++ Parameterized Constructors
- C++ Copy Constructor
- C++ Constructor Overloading
- C++ Constructor with Default Arguments
- C++ Delegating Constructors
- C++ Constructor Initialization List
- C++ Dynamic Initialization Using Constructors
- C++ Inheritance
- C++ Inheritance
- C++ Multiple Inheritance
- C++ Multilevel Inheritance
- C++ Object-oriented
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Virtual Function
- C++ Pure Virtual Functions & Abstract Classes
- C++ File Handling
- C++ Files and Streams
- C++ Reading From File
- C++ Advanced
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
- C++ Socket Programming
- C++ Concurrency
- C++ Advanced Concepts
- C++ Lambda Expression
- C++ unordered_multiset
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