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

C++ Default Arguments

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 <iostream>
#include <string>

void greet(std::string name = "Guest") {
	std::cout << "Hello, " << name << std::endl;
}

int main() {
	greet();
	greet("Alex");
	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 <iostream>

void configure(int width = 100, int height = 50) {
	std::cout << width << "x" << height << std::endl;
}

int main() {
	configure();
	configure(200);
	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 <iostream>
#include <string>

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

int main() {
	greet();
	return 0;
}

void greet(std::string name) {
	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;
}

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.