C++ Multiple Parameters

πŸ”§ C++ Multiple Parameters

In C++, a function can accept multiple parameters, allowing it to work with more than one input value at the same time.
Parameters are separated by commas.


πŸ”Ή 1. Basic Syntax

return_type function_name(type1 param1, type2 param2, type3 param3) {
// function body
}

πŸ”Ή 2. Simple Example with Multiple Parameters

int add(int a, int b) {
return a + b;
}

cout << add(10, 20); // Output: 30


πŸ”Ή 3. Three Parameters Example

int multiply(int a, int b, int c) {
return a * b * c;
}

cout << multiply(2, 3, 4); // Output: 24


πŸ”Ή 4. Different Data Types as Parameters

void display(string name, int age, float marks) {
cout << name << " " << age << " " << marks;
}

display("Sanjit", 21, 88.5);


πŸ”Ή 5. Multiple Parameters with Return Value

double average(int a, int b, int c) {
return (a + b + c) / 3.0;
}

cout << average(60, 70, 80); // Output: 70


πŸ”Ή 6. Call by Value with Multiple Parameters

void update(int a, int b) {
a = 10;
b = 20;
}

int x = 1, y = 2;
update(x, y);

cout << x << " " << y; // 1 2 (unchanged)


πŸ”Ή 7. Call by Reference with Multiple Parameters

void update(int &a, int &b) {
a = 10;
b = 20;
}

int x = 1, y = 2;
update(x, y);

cout << x << " " << y; // 10 20


πŸ”Ή 8. Multiple Parameters with Default Values

int calculate(int a, int b = 5, int c = 2) {
return a + b + c;
}

cout << calculate(10); // 17
cout << calculate(10, 20); // 32
cout << calculate(10, 20, 30); // 60

⚠️ Default parameters must be at the end.


πŸ”Ή 9. Passing Arrays with Additional Parameters

void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
}

int a[] = {1, 2, 3};
printArray(a, 3);


πŸ”Ή 10. Passing Structure with Multiple Parameters

struct Student {
int id;
string name;
};

void show(Student s, int year) {
cout << s.name << " " << year;
}


❌ Common Mistakes

add(10); // ❌ missing parameter
add(10, 20, 30); // ❌ too many parameters

➑ Number and order of arguments must match parameters.


πŸ“Œ Summary

  • Functions can take multiple parameters

  • Parameters are separated by commas

  • Can mix different data types

  • Can use value, reference, pointer, and default parameters

  • Argument order and count must match function definition

You may also like...