void display (int i ) { cout<<"integer "<<i<<endl; } void display (double i ) { cout<<"double " <<i<<endl; } int main( ) { display(7); display(3.5); return 0; }
integer 7 double 3.5
void init (inta ,intb = 0) ; // 2'nd argument = 0 by default init (3); init (2,4);
void incorrect(int a =3,int b,int c= 0) ; // error , b has no value
If we write the default arguments in the prototype of a function, we can not rewrite this arguments with their values in the definition.
void init (int a = 0) ; // declaration of init void init (int a = 0) {/* body */ } //definition of init ERROR
void init (int a = 0) ;// declaration of init void init (int a ){ / * body * / } //OK
#include <iostream> void init(int,int=3); void init(int a,int b) { cout<<a <<","<<b; } void init(int=7,int); // overdefinition int main() { init(2,1); // displays 2,1 init(4); // displays 4,3 init(); // displays 7,3 return 0; }