Default Argument Function In C++ Programming


A Function is a collection of statements that are grouped together to perform an operation. A function in C++ is a block of code that, When the function is invoked from any part of the program, it all executes the codes defined in the body of the function. The default argument is a value provided in a function declaration that is automatically assigned by the compiler if the caller of the function doesn't provide a value for the argument with a default value. In case any value is passed the default value is overridden.

The program demonstrates a function with default arguments in C++, where the function has parameters with default values that can be omitted in function calls.

  • The program defines a function named biodata() that takes three parameters - a string name, an integer age, and a string city with a default value of "Salem". The function prints the name, age, and city values to the console using cout.
  • In the main() function, the biodata() function is called twice with different parameter lists. The first call only passes the name and age arguments, omitting the city argument. In this case, the default value "Salem" is used for the city parameter. The second call passes all three arguments, including a non-default value "Namakkal" for the city parameter.
  • As a result, the program output shows two lines with the name, age, and city values of the two biodata() function calls:

Source Code

#include<iostream>
using namespace std;
//  Default Argument Function
 
void biodata(string name,int age,string city="Salem")
{
    cout<<name<<" is from "<<city<<" and age is "<<age<<endl;
}
int main()
{
    biodata("Ram",25);
    biodata("Sam",22,"Namakkal");
    return 0;
}
 

Output

Ram is from Salem and age is 25
Sam is from Namakkal and age is 22
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples