Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Function Default Arguments, Placeholder Parameters, and Overloading in C++

Tech 1

Default Parameters

When defining function parameters with default values, keep two rules in mind:

  1. If a parameter has a default value, all parameters to its right must also have default values.
  2. Default values can only be specified in either the declaration or the definition, not both.
#include <iostream>
using namespace std;

int calculate(int x = 5, int y = 20, int z = 15) {
    return x + y + z;
}

// Declaration with defaults
int compute(int p = 8, int q = 12);
// Definition without defaults for the same parameters
int compute(int p, int q) {
    return p * q;
}

int main() {
    cout << calculate() << endl;  // Output: 40
    cout << compute() << endl;    // Output: 96
    return 0;
}

Placeholder Parameters

Placeholder parameters occupy a position in the parameter list but don't create a usable variable name. They must either receive an argument during the call or have a default value.

#include <iostream>
using namespace std;

void display(int value, int = 100) {
    cout << "Value: " << value << endl;
}

int main() {
    display(50);  // Second argument uses default value 100
    display(50, 200);  // Second argument is explicitly 200
    return 0;
}

Function Overloading

Function overloading allows multiple functions with the same name to coexist, as long as their parameter lists differ. The compiler distinguishes them by the function signature.

Requirements for overloading:

  • All functions must be in the same scope
  • Function names must be identical
  • Paraemter types, count, or order must differ
void process(int num) { }           // Different type
void process(double num) { }        // Different type
void process(int a, double b) { }   // Different count
void process(double a, int b) { }   // Different order

// Return type alone does NOT create an overload
int process(double num);  // Error: only differs by return type

Important Considerations:

  1. Reference parameters affect overload resolution. Creating a non-const reference from a literal is illegal, so the compiler selects the const overload when passing constants.

  2. Avoid ambiguity when overloading functions that have default arguments. If one overload has default parameters and another doesn't, calls with fewer arguments become ambiguous.

#include <iostream>
using namespace std;

void output(int& ref) {
    cout << "Non-const reference" << endl;
}

void output(const int& ref) {
    cout << "Const reference" << endl;
}

void execute(int x, int y = 50) {
    cout << "Two parameters with default" << endl;
}

void execute(int x) {
    cout << "Single parameter" << endl;
}

int main() {
    int val = 42;
    output(val);   // Calls non-const version
    output(100);    // Calls const version (int& ref = 100 is illegal)
    
    execute(30);    // Ambiguous - compiler cannot decide between two overloads
    execute(30, 70); // Calls two-parameter version
    
    return 0;
}

Key takeaways:

  • int& ref = 10; is illegal because references must be bound to lvalues
  • Use const int& ref = 10; when literals need to bind to references
  • Default parameters combined with overloads create ambiguous calls that the compiler cannot resolve
Tags: C++Functions

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.