C++ allows functions to have default parameters. This is useful when a parameter should have a specified value when the caller does not supply any value.
class DefParam{
public:
//Declare the default parameter(s) in function decalaration
void printVal(int x=10);
};
//Don't repeat the default parameter in function definition
void DefParam::printVal(int val){
printf("Val = %d\n",val);
}
int main(){
DefParam obj;
obj.printVal(); //Passes 10 to printVal()
obj.printVal(25); //Passes 25 to printVal()
return 0;
}$ ./a.exe
Val = 10
Val = 25
Note: Java does not provide default parameter values, but you can use overloading to achieve the same effects.
No comments :
Post a Comment