Default arguments in C++

By: Babbar Ankit Emailed: 1768 times Printed: 2516 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

C++ provides the option of  providing default values to the arguments being passed to a function.

Consider the example

using namespace std;

long int sum(int n,int diff=1,int first_term=1 )
long  sum=0;;
for(int i=0;i<n;i++)
cout<<setw(5)<<first_term+ diff*i;
return sum;

int main()
cout<<endl<<\"Sum=\"<<setw(7)< <sum(10)<<endl;
//first term=1; diff=1,n=10
//sums the series 1,2,3,4,5………10

cout<<endl<<\"Sum=\"<<setw(7)< <sum(6,3,2)<<endl;
//first term=1; diff=2,n=10
//sums the series 2,5,8,11,14,17

cout<<endl<<\"Sum=\"<<setw(7)< <sum(10,2)<<endl;
//first term=1; diff=2,n=10
//sums the series 1,3,5………..19

return 1;


all the parameters with default values should lie to the right in the signature list i.e. the default arguments should be the trailing arguments—those at the end of the list.

when a function with default arguments is called, the first argument  in the call statement is assigned to the first argument in the definition, the 2nd to 2nd and so on.

This becomes more clear from the last call to sum() in the above example where value 10 is assigned to n and 2 is assigned to diff and not first_term.

 the default argument values appear in the prototype as well as definition.

You still may omit variable names in the prototypes.

The syntax then being

int xyz(int =2,char=5);

Authors Url:

C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

dear sir, plz answer my following questions:

Q1- Why should all normal arguments precede all the default arguments in c++?

Q2- How can the compiler determine which argument corresponds to which one?

Q3- How can a compiler know for sure that a particular default argument is passed explicitly or not?

View Tutorial          By: hawa at 2012-01-29 08:29:39

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Babbar Ankit
Call by reference in C++ Functions
Default arguments in C++
Function overloading in C++

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
The Stack and the Free Store in C++
Operator Precedence in C++
Getting User Input Using cin in C++
Function overloading in C++
Vector variables in C++
Multi-Line Literals in C++
Calculating total based on the given quantity and price in C++
Using Comments in a C++ Program
do...while Loops in C++
for Loops in C++
Using switch Statements in C++
What Is a Pointer in C++?
The indirection operator (*) - dereference operator.
Dot (.) vs Arrow (->) to access data members in C++
this Pointer in C++
Most Emailed Articles (in C++)
Demonstrating global and local variables in C++
C++ and Object-Oriented Programming
Using cout in C++
Use of Conditional (Ternary) Operator in C++
const Member Functions in C++
Classes with Other Classes as Member Data in C++
What Is a Pointer in C++?
Manipulating Data by Using Pointers
Stray or Dangling Pointers in C++
C++ Destructors Versus Java Finalization
atoi(), itoa() in C++
strcat() and strncat() sample program in C++
Using #define, The Preprocessor and the Compiler in C++
Getting User Input Using cin in C++
Pass by Reference in C++ functions