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

C++ Default Arguments

A default argument is a backup value a function uses if you skip it, like a pizza that comes with cheese unless you ask otherwise. It makes some inputs optional.
Syntax
cpp
return_type function_name(type param1, type param2 = default_value) {
  // body
}

function_name(argument);  // param2 uses the default

Introduction to Default Arguments

A parameter can be given a default value in its declaration, like void greet(string name = "Guest"), so calling greet() with no argument at all automatically uses "Guest" instead of requiring the caller to supply it explicitly every time.

Example: Introduction to Default Arguments

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

void greet(std::string name = "Guest") {
	// Print to the console with cout
	std::cout << "Hello, " << name << std::endl;
}

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

Multiple Default Arguments

A function can have several default parameters at once, and C++ fills in the values you do provide from left to right — so if you supply only the first argument, every parameter after it falls back to its default.

This lets common cases stay short while still allowing full customization when needed.

Example: Multiple Default Arguments

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

void configure(int width = 100, int height = 50) {
	// Print to the console with cout
	std::cout << width << "x" << height << std::endl;
}

// Program execution starts in main()
int main() {
	configure();
	configure(200);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Defaults in Function Prototypes

When a function is both declared (prototyped) and defined separately, default values must be specified only in the declaration, not repeated in the definition — putting them in both places, or only in the definition, causes a compile error.

This keeps the default value's source of truth in one place.

Example: Defaults in Function Prototypes

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

void greet(std::string name = "Guest");

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

void greet(std::string name) {
	// Print to the console with cout
	std::cout << "Hello, " << name << std::endl;
}

Overloading with Default Arguments

Mixing default arguments with function overloading can create genuine ambiguity for the compiler — if void show(int a, int b = 5) and void show(int a) both exist, a call to show(10) matches both, and the compiler won't know which one you meant.

It's worth checking for this kind of overlap whenever you add default values to an already-overloaded function.

Example: Overloading with Default Arguments

cpp
#include <iostream>

void show(int a, int b = 5) { std::cout << "a,b: " << a << "," << b << std::endl; }
// void show(int a) {...} // would be ambiguous with show(a, b=5) when called as show(x)

int main() {
	show(10);
	show(10, 20);
	return 0;
}

Position Rules

Default arguments must be grouped at the right-hand end of the parameter list — once one parameter has a default value, every parameter after it must also have one, since the compiler fills defaults from right to left based on how many arguments were actually supplied.

You can't place a required, non-default parameter after a defaulted one.

Example: Position Rules

cpp
#include <iostream>

void order(int a, int b = 2, int c = 3) {
	std::cout << a << "," << b << "," << c << std::endl;
}
// void invalid(int a = 1, int b) {} // compile error: default before non-default

int main() {
	order(1);
	order(1, 5);
	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. Putting a default argument before a non-default one, like void f(int a = 1, int b), which is an error.
  2. Repeating the default value in both the declaration and definition, which is a redefinition error.
  3. Adding overloads that clash with defaults, so calls become ambiguous.

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.