Function with default Argument
Function with default argument is used when user does not provide the argument then the default argument will be used.
Syntax:
return type functionName(datatype varName1, datatype varName2=value,..)
{
Statements;
}
Example
Write a Program to show the Use of Function with default Argument.
#include <iostream>
using namespace std;
void display(int id, string name, string city="Delhi")
{
cout<<"\nEmployee's Details!!\n";
cout<<"\n Welcome "<<name<<" your Id is "<<id<<" and City is " <<city;
}
int main()
{
int i;
string n,c;
cout<<"Enter the Id: ";
cin>>i;
cout<<"Enter the name: ";
cin>>n;
cout<<"Enter the City: ";
cin>>c;
// Here # arguments passed to the function
display(i,n,c);
//It will print the default value for the City
display(i,n);
return 0;
}
Comments
Post a Comment
If you have any doubts, Please let me Know