C++ Default Parameters

Welcome to The Coding College! In this tutorial, we’ll dive into default parameters in C++, an essential feature that allows you to make your functions more flexible and user-friendly.

What Are Default Parameters?

Default parameters in C++ are values provided in the function declaration. These values are automatically assigned to parameters if no arguments are passed during the function call.

Key Benefits:

  1. Simplifies Function Calls: Reduces the need to specify arguments explicitly.
  2. Improves Code Readability: Clarifies the purpose of optional parameters.
  3. Increases Flexibility: Allows a single function to handle multiple scenarios.

Syntax

return_type function_name(parameter_type parameter_name = default_value, ... ) {
    // Function body
}
  • parameter_name: The name of the parameter.
  • default_value: The value assigned if no argument is passed.

Example: Simple Default Parameter

#include <iostream>
using namespace std;

// Function with default parameter
void greet(string name = "Guest") {
    cout << "Hello, " << name << "! Welcome to The Coding College!" << endl;
}

int main() {
    greet();         // No argument passed; uses default value
    greet("Alice");  // Overrides the default value
    return 0;
}

Output:

Hello, Guest! Welcome to The Coding College!  
Hello, Alice! Welcome to The Coding College!  

Rules for Default Parameters

  • Default parameters must be specified from right to left in the parameter list.
void display(int x, int y = 10); // Valid
void display(int x = 10, int y); // Invalid
  • Default values must be defined in either the function declaration or definition, but not both.

Example: Multiple Default Parameters

#include <iostream>
using namespace std;

void printInfo(string name = "Guest", int age = 18) {
    cout << "Name: " << name << ", Age: " << age << endl;
}

int main() {
    printInfo();               // Uses both default values
    printInfo("Alice");        // Overrides only the first parameter
    printInfo("Bob", 25);      // Overrides both parameters
    return 0;
}

Output:

Name: Guest, Age: 18  
Name: Alice, Age: 18  
Name: Bob, Age: 25  

Using Default Parameters in Function Prototypes

Default values are typically declared in the function prototype (declaration).

Example

#include <iostream>
using namespace std;

// Declaration with default parameters
void showMessage(string message = "Welcome", int count = 1);

int main() {
    showMessage();          // Uses default values
    showMessage("Hello!");  // Overrides the first parameter
    showMessage("Hi!", 3);  // Overrides both parameters
    return 0;
}

// Definition
void showMessage(string message, int count) {
    for (int i = 0; i < count; i++) {
        cout << message << endl;
    }
}

Output:

Welcome  
Hello!  
Hi!  
Hi!  
Hi!  

Combining Default Parameters with Overloading

Default parameters and function overloading can be used together, but avoid ambiguity.

Example

#include <iostream>
using namespace std;

void display(int x, int y = 10) {
    cout << "Two parameters: x = " << x << ", y = " << y << endl;
}

void display(int x) {
    cout << "One parameter: x = " << x << endl;
}

int main() {
    display(5);         // Calls the overloaded function with one parameter
    display(5, 15);     // Calls the function with two parameters
    return 0;
}

Output:

One parameter: x = 5  
Two parameters: x = 5, y = 15  

Practical Applications of Default Parameters

1. Greeting Function

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

2. Price Calculation

double calculatePrice(double basePrice, double taxRate = 0.05) {
    return basePrice + (basePrice * taxRate);
}

3. Logging Messages

void logMessage(string message, string severity = "INFO") {
    cout << "[" << severity << "] " << message << endl;
}

Best Practices for Default Parameters

  1. Keep Defaults Logical: Use values that make sense as defaults.
  2. Don’t Overuse: Only use default parameters where necessary.
  3. Combine with Overloading: For complex use cases, combine default parameters with function overloading.
  4. Maintain Consistency: Document default values clearly to avoid confusion.

Explore More on The Coding College

Default parameters can make your functions more powerful and versatile. Learn more about advanced function techniques like inline functions, templates, and recursion at The Coding College.

Leave a Comment