Function with default parameters (arguments)

Or

Function with parameters having default values

 Consider the following program.

# include

void main ( )

{

void disptime ( int = 8, int = 0, int = 0 );

disptime (,29,39);

disptime (8,30);

disptime (8);

disptime ( );

}

Void disptime (inthr, intmn, intsec )

{

cout << hr <<' : ' << sec << '\n';

}

O/P:-           8:29:39:                8:29:39

8:30:0                            8:30:0

8:0:0                     8:0:0

8:0:0                     8:0:0

→      The prototype of function disptime indicates that it accepts three integers with default values 8, 0 and 0

→      In the first call of disptime we are passing all the three arguments. This will be copied into hr, min and sec and no default value will be utilized hence the o/p is 8:29:39

→      In the 2nd call only two actual parameters are passed. This parameters will be copied into hr and min. sec will get the default value i.e 0. Hence the o/p is 8:30:0

→      In the 3rd call only once actual parameters are passed. This parameters will be copied into hr. and sec will get the default value i.e and hence o/p is 8:0:0

→      In the 4th call no actual parameters are passéd. Hence hr, min and sec will take the default initial value i.e 8, 0 and 0 respectively hence o/p is 8:0:0

 

Program :-

# include

void main ( )

{

void dispdate ( int = 1, int = 1, int = 2006);

dispdate ( );

dispdate (26);

dispdate (15,8 );

dispdate ( 31,10, 2005 );

}

Void dispdate  ( int d, int m, int y )

{

cout << d << '/' << m << '/' << y << '\n';

}

O/P:-

1/1/2006

26/1/2006

15/8/2006

31/12/2005


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

No comments