Default arguments in C++

By: Babbar Ankit Emailed: 1728 times Printed: 2329 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

#include<iostream>
#include<iomanip>
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;
   sum+=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;
}

IMPORTANT

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: http://www.botskool.com/programming-tutorials


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

Comments(1)


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++ )
Using cout.width() in C++
Using Comments in a C++ Program
Converting Pointer Parameters in C++ to Java
Calculating total based on the given quantity and price in C++
Demonstration of Prefix and Postfix operators in C++
virtual inheritance example in C++
C++ Recursion function explained using Fibonacci series
Multiple statements in for loops in C++
Dot (.) vs Arrow (->) to access data members in C++
assert() example program in C++
Difference between Procedural, Structured, and Object-Oriented Programming
Specifying default values to function parameters in C++
Public versus Private members in C++
Constructors and Destructors in C++
Nested Loops in C++
Most Emailed Articles (in C++)
Matrix using nested for loops in C++
What Is a Pointer in C++?
C++ Destructors Versus Java Finalization
Operator Precedence in C++
How to handle Exceptions in C++
atoi(), itoa() in C++
Using cout.fill() in C++
Multiple inheritance example in C++
Using the Built-in Arithmetic Types in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Sorting an array of Strings in C++
A Brief History of C++
Difference between Procedural, Structured, and Object-Oriented Programming
C++ and Object-Oriented Programming