Skip to main content

Function with Default Argument

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;
}

Output


Comments

Popular posts from this blog

Programming Basics

Logic Building Data_Types Operators Algorithms Pseudo_Code Hello Guys, Let's Play with Logic.........but wait, before that you must know that how to build Logic in Programming as you all know that programming is all about Logic..... So Let's start the game... If you are new Here that means you are absolutely right Place, Or If you already know, than you will learn new things here! If we Talk about the Programming we should be familiar with  these terms..... 1. Variable 2. Data Types 3.Keywords Variable: Variable is something which holds the value.                  Like:      a=10;                 Here,                 a---------------------->  Variable                  10 ---------------------> Value * The Value of Variable can be changed , *  ...

FAQs

Basic Questions on Programming Languages 1. What is the meaning of Programming? Ans: Programming is the process of Scheduling the task to perform step by step to Execute a specific Task. 2. Types of Programming Languages? Ans: Types:             Procedural Programming Language            Object Oriented Programming Language            Object Based Language            Scripting Language Procedural Programming Language   C Programming  Object Oriented Programming Language     C++     Java     C#     VB.Net Object Based Language     JavaScript Scripting Language     Python     VBScript     PHP     Perl     Ruby     R 3. Types Of Operators in Programming Languages. Ans:      Operators Symbols Type Example Assignment = U...