C++ Program to Demonstrate Default Arguments in Functions

Default arguments in C++ allow you to call a function without providing all the arguments, letting the compiler use default values for missing arguments. This feature simplifies function calls and provides flexibility in how functions are used.

Prerequisites

Before diving into the implementation, ensure you have a basic understanding of the following concepts:

  1. Basic C++ Syntax: Familiarity with C++ syntax, including variables, functions, and control structures (if-else, loops).
  2. Function Overloading: Basic understanding of function overloading and how it relates to default arguments.

1. Introduction to Default Arguments

1.1 Definition

Default arguments are values that are provided in a function declaration and are used if no corresponding arguments are passed to the function during a call. Default arguments are specified in the function prototype or declaration and not in the function definition.

1.2 Syntax

The syntax for specifying default arguments is as follows:

C++
void functionName(int arg1 = default_value1, int arg2 = default_value2);

2. Implementation of Default Arguments in C++

2.1 Example 1: Basic Default Arguments

Let’s start with a basic example to demonstrate the use of default arguments in a function.

C++
#include <iostream>
using namespace std;

void greet(string name = "Guest") {
    cout << "Hello, " << name << "!" << endl;
}

int main() {
    greet("Alice");  // Calls greet with "Alice"
    greet();         // Calls greet with the default argument "Guest"

    return 0;
}

Output:

C++
Hello, Alice!
Hello, Guest!

Explanation:

  • The function greet is defined with a default argument name set to "Guest".
  • When calling greet("Alice"), the provided argument "Alice" is used.
  • When calling greet() without arguments, the default value "Guest" is used.

2.2 Example 2: Multiple Default Arguments

This example demonstrates a function with multiple default arguments.

C++
#include <iostream>
using namespace std;

void displayInfo(string name = "John Doe", int age = 30, string country = "USA") {
    cout << "Name: " << name << ", Age: " << age << ", Country: " << country << endl;
}

int main() {
    displayInfo("Alice", 25, "Canada");  // Calls with all arguments provided
    displayInfo("Bob", 40);              // Calls with two arguments, default for the third
    displayInfo("Charlie");              // Calls with one argument, default for the second and third
    displayInfo();                       // Calls with no arguments, all defaults used

    return 0;
}

Output:

C++
Name: Alice, Age: 25, Country: Canada
Name: Bob, Age: 40, Country: USA
Name: Charlie, Age: 30, Country: USA
Name: John Doe, Age: 30, Country: USA

Explanation:

  • The function displayInfo is defined with three default arguments: name, age, and country.
  • Different combinations of provided and default arguments are used when calling displayInfo.

2.3 Example 3: Default Arguments with Function Overloading

This example shows how default arguments can be used with function overloading.

C++
#include <iostream>
using namespace std;

void printMessage(string msg, int times = 1) {
    for (int i = 0; i < times; ++i) {
        cout << msg << endl;
    }
}

void printMessage(string msg) {
    cout << msg << endl;
}

int main() {
    printMessage("Hello, World!", 3);  // Calls the first overload with two arguments
    printMessage("Goodbye!");          // Calls the second overload

    return 0;
}

Output:

C++
Hello, World!
Hello, World!
Hello, World!
Goodbye!

Explanation:

  • Two overloaded versions of printMessage are defined: one with a default argument times, and one without.
  • When calling printMessage("Hello, World!", 3), the first overload is used.
  • When calling printMessage("Goodbye!"), the second overload is used.

2.4 Example 4: Default Arguments in Member Functions

This example demonstrates the use of default arguments in member functions of a class.

C++
#include <iostream>
using namespace std;

class Rectangle {
private:
    int width, height;
public:
    Rectangle(int w = 10, int h = 5) : width(w), height(h) {}

    void displayArea() {
        cout << "Area: " << width * height << endl;
    }
};

int main() {
    Rectangle rect1(4, 5);  // Calls constructor with two arguments
    Rectangle rect2(7);     // Calls constructor with one argument, default for the second
    Rectangle rect3;        // Calls constructor with default arguments

    rect1.displayArea();    // Output: Area: 20
    rect2.displayArea();    // Output: Area: 35
    rect3.displayArea();    // Output: Area: 50

    return 0;
}

Output:

C++
Area: 20
Area: 35
Area: 50

Explanation:

  • The class Rectangle has a constructor with default arguments for width and height.
  • Different combinations of provided and default arguments are used when creating objects rect1, rect2, and rect3.
  • The displayArea member function calculates and displays the area of the rectangle.

Conclusion

Default arguments in C++ provide flexibility and simplify function calls by allowing you to omit some arguments while calling a function. This article provided a comprehensive guide to using default arguments in C++, demonstrating their application through basic examples, multiple default arguments, function overloading, and member functions in a class.