Default value functions, C/C++ Programming

Default Value Functions, When declaring a function we can specify a default value for each parameter. This value will be used if that parameter is left blank when calling to the function. This is done by assigning values to the arguments in the function prototypes. If a value for that parameter is not passed when the function is called, the default value is used, but if a value is specified this default value is stepped on and the passed value is used. 

Default arguments must always be the rightmost in a function list i.e. when calling a function with two or more default arguments and an omitted argument is not the rightmost argument in the argument list then all arguments to the right of that argument must be omitted.

Example:

// default values in functions

#include 

using namespace std;

int divide  (int a, int b=2);
int divide  (int a, int b)
{

int r;
r=a/b;

return  (r);

}

int main  ()
{

cout  << divide  (12);
cout  << endl;

cout  << divide  (20,4);
cout  << endl;

return  0;

}

 

Output

6

 

Posted Date: 8/21/2012 11:51:23 AM | Location : United States







Related Discussions:- Default value functions, Assignment Help, Ask Question on Default value functions, Get Answer, Expert's Help, Default value functions Discussions

Write discussion on Default value functions
Your posts are moderated
Related Questions
Luminous Jewels - The Polishing Game Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various lum

Operation on array: void Array::add( Object& toAdd ) {     lastElementIndex++;     while( ptrAt( lastElementIndex ) != ZERO &&            lastElementIndex


C++ Multiplayer Game Programming Project Project Description: We are seeking an experienced C++ Game Programmer to enhance clientside related aspects of MCS: Mars City Securi

Help for tomorrow...?

write an algorithm to swap values without using third variable

What is the difference between an external iterator and an internal iterator? Ans) An internal iterator is executed with member functions of the class that has items to step th

how to search, display all data and delete data

A: Mostly can be overloaded. The only C operators which can't be are. and?: (and sizeof, that is technically an operator). C++ adds a few of its own operators, mostly which can be

Program is to find the area of room: Program is to find the area of room with default values using classes & object class room   {   private:     int len;     i