Skip to Content
FunctionsMultiple Parameters

Multiple Parameters in C++ Functions

Functions can accept multiple parameters, allowing you to pass several values to perform more complex operations.

Basic Syntax

returnType functionName(type1 param1, type2 param2, type3 param3) { // function body return value; }

Simple Example with Two Parameters

#include <iostream> using namespace std; int add(int a, int b) { return a + b; } int main() { int result = add(10, 20); cout << "10 + 20 = " << result << endl; return 0; }

Output:

10 + 20 = 30

Example with Three Parameters

#include <iostream> using namespace std; int sum(int a, int b, int c) { return a + b + c; } int main() { int result = sum(5, 10, 15); cout << "5 + 10 + 15 = " << result << endl; return 0; }

Output:

5 + 10 + 15 = 30

Different Data Types

You can mix different data types in parameters:

#include <iostream> #include <string> using namespace std; void displayInfo(string name, int age, double height) { cout << "Name: " << name << endl; cout << "Age: " << age << " years" << endl; cout << "Height: " << height << " meters" << endl; } int main() { displayInfo("Alice", 25, 1.65); return 0; }

Output:

Name: Alice Age: 25 years Height: 1.65 meters

Rectangle Area Calculator

A practical example calculating rectangle area:

#include <iostream> using namespace std; double calculateRectangleArea(double length, double width) { return length * width; } int main() { double area = calculateRectangleArea(5.5, 3.2); cout << "Rectangle Area: " << area << " square units" << endl; return 0; }

Output:

Rectangle Area: 17.6 square units

Triangle Area Calculator

Using three parameters for triangle calculation:

#include <iostream> #include <cmath> using namespace std; // Calculate triangle area using Heron's formula double calculateTriangleArea(double a, double b, double c) { double s = (a + b + c) / 2.0; // semi-perimeter double area = sqrt(s * (s - a) * (s - b) * (s - c)); return area; } int main() { double area = calculateTriangleArea(3.0, 4.0, 5.0); cout << "Triangle Area: " << area << " square units" << endl; return 0; }

Output:

Triangle Area: 6 square units

String Formatting Function

#include <iostream> #include <string> using namespace std; string formatMessage(string greeting, string name, string punctuation) { return greeting + ", " + name + punctuation; } int main() { string message1 = formatMessage("Hello", "Alice", "!"); string message2 = formatMessage("Good morning", "Bob", "."); cout << message1 << endl; cout << message2 << endl; return 0; }

Output:

Hello, Alice! Good morning, Bob.

Grade Calculator

Calculate final grade from multiple components:

#include <iostream> using namespace std; double calculateFinalGrade(double homework, double midterm, double final) { // 30% homework, 30% midterm, 40% final return (homework * 0.30) + (midterm * 0.30) + (final * 0.40); } int main() { double finalGrade = calculateFinalGrade(85, 90, 88); cout << "Final Grade: " << finalGrade << "%" << endl; return 0; }

Output:

Final Grade: 87.9%

Parameter Order Matters

The order in which you pass parameters must match the function definition:

#include <iostream> using namespace std; void displayOrder(int first, int second, int third) { cout << "First: " << first << endl; cout << "Second: " << second << endl; cout << "Third: " << third << endl; } int main() { displayOrder(10, 20, 30); // 10 goes to first, 20 to second, 30 to third return 0; }

Output:

First: 10 Second: 20 Third: 30

Complex Example: Circle Calculator

#include <iostream> #include <cmath> using namespace std; const double PI = 3.14159265359; void circleProperties(double radius, double& area, double& circumference) { area = PI * radius * radius; circumference = 2 * PI * radius; } int main() { double r = 5.0; double area, circumference; circleProperties(r, area, circumference); cout << "Circle with radius " << r << ":" << endl; cout << "Area: " << area << endl; cout << "Circumference: " << circumference << endl; return 0; }

Output:

Circle with radius 5: Area: 78.5398 Circumference: 31.4159

Best Practices

  1. Limit the number of parameters: If you need more than 5-6 parameters, consider using a struct or class
  2. Use meaningful names: calculateArea(length, width) is clearer than calc(a, b)
  3. Group related parameters: Keep parameters that work together adjacent
  4. Consider parameter order: Put required parameters first, optional ones later
  5. Use const for input-only parameters: void process(const string& text)

Common Mistakes

Wrong Number of Arguments

int add(int a, int b) { return a + b; } // Wrong: too few arguments // add(5); // Error! // Wrong: too many arguments // add(5, 10, 15); // Error! // Correct add(5, 10); // ✓

Wrong Argument Types

void display(int number, string text) { cout << text << ": " << number << endl; } // Wrong order - types don't match // display("Score", 100); // Error! // Correct display(100, "Score"); // ✓

Practice Exercises

  1. BMI Calculator: Create a function that takes weight (kg) and height (m) as parameters and returns BMI.

  2. Temperature Converter: Write a function that takes temperature and unit (‘C’ or ‘F’) and converts it.

  3. Max of Three: Create a function that finds the maximum of three numbers.

  4. Average Calculator: Write a function that takes four test scores and returns the average.

  5. Rectangle Perimeter: Create a function that calculates rectangle perimeter from length and width.

Next Steps

  • Learn about Default Parameters to provide default values for parameters
  • Explore function overloading to create multiple versions of the same function
  • Study pass by reference vs pass by value for efficient parameter passing

Multiple parameters make your functions more versatile and powerful!

Last updated on