← Back to C++ Course | Chapter 5: Functions | Lesson 3 of 13

C++ Multiple Function Parameters

A function can accept several parameters separated by commas, each with its own type, and arguments are matched to parameters by position when the function is called.
Syntax
cpp
return_type function_name(type param1, type param2) {
  // body
}

function_name(argument1, argument2);

Declaring Multiple Parameters

Multiple parameters are declared inside a function's parentheses, separated by commas, each with its own type and name, letting a single function accept several pieces of input at once.

Example: Declaring Multiple Parameters

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

void showInfo(std::string name, int age) {
	// Print to the console with cout
	std::cout << name << " is " << age << std::endl;
}

// Program execution starts in main()
int main() {
	showInfo("Alex", 30);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Matching Arguments by Position

When a function is called, each argument is matched to the corresponding parameter by its position in the list, not by name, so the order arguments are passed in matters.

Example: Matching Arguments by Position

cpp
// Include std::cout and std::cin
#include <iostream>

void divide(int a, int b) {
	// Print to the console with cout
	std::cout << a << " / " << b << " = " << a / b << std::endl;
}

// Program execution starts in main()
int main() {
	divide(10, 2);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Parameters of Different Types

A function's parameters don't all need to be the same type; mixing int, double, string, and other types within one parameter list is common and fully supported.

Example: Parameters of Different Types

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

void printOrder(std::string item, double price, int quantity) {
	// Print to the console with cout
	std::cout << quantity << "x " << item << " at $" << price << std::endl;
}

// Program execution starts in main()
int main() {
	printOrder("Book", 12.5, 3);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Using Multiple Parameters Together

Multiple parameters are often combined inside the function body to compute a result, such as multiplying a quantity by a price or comparing two values against each other.

Example: Using Multiple Parameters Together

cpp
// Include std::cout and std::cin
#include <iostream>

double totalCost(double price, int quantity) {
	// Send price * quantity back to the caller
	return price * quantity;
}

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << totalCost(9.99, 3) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Too Many or Too Few Arguments

Calling a function with the wrong number of arguments -- too few or too many -- is a compile-time error in C++, since the compiler checks that every call matches the function's declared parameter list.

Example: Too Many or Too Few Arguments

cpp
#include <iostream>

void add(int a, int b) {
	std::cout << a + b << std::endl;
}

int main() {
	add(2, 3);
	// add(2);       // compile-time error: too few arguments
	// add(2, 3, 4); // compile-time error: too many arguments
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Passing arguments in the wrong order, which compiles but gives wrong results when the types match.
  2. Calling with too few or too many arguments, which is a compile error.
  3. Writing int add(int a, b) and leaving the type off the second parameter.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.