Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C++ Default Function Arguments: How They Work & Best Practices

C++ Default Function Arguments

Explore how to assign default values to function parameters in C++, with step‑by‑step examples and practical guidelines.

In C++, function parameters can be given default values. When a call omits an argument, the compiler substitutes the default. If an argument is supplied, the default is ignored.


How Default Arguments Work

C++ Default Function Arguments: How They Work & Best Practices

Consider the following illustration:

  1. temp() uses both defaults.
  2. temp(6) overrides the first parameter; the second remains default.
  3. temp(6, -2.3) provides values for both, overriding both defaults.
  4. Calling temp(3.4) mistakenly passes a double to an int parameter. The compiler truncates the value to 3, demonstrating that argument types must match the declaration order.

Example: Using Default Arguments

#include <iostream>
using namespace std;

// Prototype with default arguments
void display(char = '*', int = 3);

int main() {
    int count = 5;

    cout << "No argument passed: ";
    display();   // uses '*', 3

    cout << "First argument passed: ";
    display('#');   // uses '#', 3

    cout << "Both arguments passed: ";
    display('$', count); // uses '$', 5

    return 0;
}

void display(char c, int count) {
    for(int i = 1; i <= count; ++i) {
        cout << c;
    }
    cout << endl;
}

Output

No argument passed: ***
First argument passed: ###
Both arguments passed: $$$$$

The program behaves as follows:

  1. display() uses the defaults c='*' and count=3.
  2. display('#') overrides only the first argument; the second remains default.
  3. When both arguments are supplied, defaults are ignored.

Defining defaults directly in the function definition is also valid and often clearer:

#include <iostream>
using namespace std;

void display(char c = '*', int count = 3) {
    for(int i = 1; i <= count; ++i) {
        cout << c;
    }
    cout << endl;
}

int main() {
    int count = 5;

    cout << "No argument passed: ";
    display();

    cout << "First argument passed: ";
    display('#');

    cout << "Both arguments passed: ";
    display('$', count);

    return 0;
}

Key Points to Remember

  1. After a parameter receives a default value, every following parameter must also have a default. For example:
    // Invalid
    void add(int a, int b = 3, int c, int d);
    
    // Valid
    void add(int a, int c, int b = 3, int d = 4);
    
  2. When defaults are specified in the definition rather than the prototype, the definition must precede any call. Otherwise the compiler will issue an error.

C Language

  1. Mastering C++ Operators: A Complete Guide with Practical Examples
  2. C++ Function Overloading: A Practical Guide
  3. How to Pass Arrays to Functions in C++: A Practical Guide
  4. Four Proven Patterns for User‑Defined Functions in C
  5. C++ Programming Basics: What Is C++ and Why It Matters
  6. Top 15 C++ Online Courses to Master the Language (2024 Update)
  7. Function Pointers in C: Practical Examples and Best Practices
  8. Mastering Variable Arguments in C: A Practical Guide
  9. Master C++ Web Programming with CGI
  10. Fanuc Decimal Point Programming: Overview & Parameter Settings