Wednesday, 28 March 2018

Default Arguments

C++ allows us to call a function without specifying all its arguments.

Default values are specified when function is declared.

Compiler looks at the prototype to see how many arguments, a function uses and alerts the program for possible default values.

Declaration of function with default values
   float amt(float prin_amt, int period, float rate = 0.12)

A function call
 float value = amt(500,7);
Passes value 500 to prin_amt and 7 to perod and then function uses 0.12 for rate.

A function call
float value = amt(500,7,0.15);
Passes explicit value of 0.15 to rate.

Only the trailing argument can have default values i.e. We can add default values from right to left. We can not provide default values in the middle of argument list.

#include<conio.h>
#include<iostream.h>
int volume(int len = 1, int wdt = 1, int hgt = 1);
void main( )
{
clrscr( )
{
cout<<"\n\n\tDefault box volume is--->"<<volume( );
cout<<"\n\n\tThe Volume with length 10 is--->"<<volume(10);
cout<<"\n\n\tThe Volume of the length 10 and width 10 is--->"<<volume(10,10);
cout<<"\n\n\tThe Volume with length 10, width 10 and height 10 is--->"<<volume(10,10,10);
getch( );
}
int volume(int len, int wdt, int hgt)
{
return(len*wdt*hgt);
}

Output:-
Default box volume is--->1
 The Volume with length 10 is--->10
 The Volume of the length 10 and width 10 is--->100
The Volume with length 10, width 10 and height 10 is--->1000

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (53) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (932) Python Coding Challenge (358) Python Quiz (23) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses