Skip to main content

Default Arguments in C++ Function

1 min read Updated June 30, 2026
Share:
On this page (7sections)

Definition

The default argument is an assigned value in the function declaration. That value will be assigned by the compiler when function call doesn’t have that argument.

Syntax

return_type function_name( param_1,param_2 = value )
for example,
int fn_multipy(int x, int y = 1);

Default Arguments in Function Example Program

//Simple Default Argument Function Example Program in C++
//Function Example

#include<iostream>
#include<conio.h>

using namespace std;

int fn_multipy(int x, int y = 1);

int main() {
   int a = 200, b = 100;
   cout << "Simple Default Argument Function Example Program\n";

   cout << "\nWorks for a and b    :" << fn_multipy(a, b);
   cout << "\nWorks for a          :" << fn_multipy(a);

   getch();
}

// Default Argument Function

int fn_multipy(int x, int y) {
   return (x * y);
}

Sample Output

Simple Default Argument Function Example Program

Works for a and b    :20000
Works for a          :200

How It Works

This C++ program demonstrates Default Arguments in Function. It first prepares the data it needs, then performs the core operation step by step, and finally prints the output shown in the Sample Output above.

  1. Declare the variables that hold the program’s data.
  2. Print the final result to the console so you can compare it with the sample output.

Try changing the input values and re-running the program to see how the output changes — this is the fastest way to understand how the logic behaves.

Continue learning with these related tutorials and programs:

Frequently Asked Questions

What does this C++ program do?
It is a C++ example program that demonstrates Default Arguments in Function, including the complete source code and the expected sample output.
How do I compile and run this C++ program?
Save the code in a `.cpp` file, compile it with `g++ filename.cpp -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses user-defined functions, illustrating a common pattern in C++ programming.

Related Tutorials

Search tutorials