---
title: "Default arguments and function overloading"
subject: "Object Oriented Programming"
chapter: 3
key: "semester_3/object_oriented_programming:default-args-function-overloading"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/default-args-function-overloading
site: BCT Engineering
---

# Default arguments and function overloading

Object Oriented Programming · Chapter 3: C++ Language Constructs

- Asked in: 2074 Chaitra, 3+5 marks; 2074 Asoj, 5 marks; 2072 Chaitra, 4 marks; 2069 Chaitra, 4 marks; 2068 Baisakh, 2+2 marks (asked 5 times)
- Video: [Function overloading and default arguments in C++ (Episode 2)](https://www.youtube.com/watch?v=ElKSqvP43ts) — BBarters
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/default-args-function-overloading
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-3

## Question

Explain how default arguments are used with functions. How can a function with default argument be implemented with function overloading? Explain with example.

## Short answer

Default arguments provide values for trailing parameters if omitted, allowing one function to handle multiple argument counts. E.g., int volume(int l, int w=1, int h=1) works as three overloaded functions. Overloading permits separate implementations, while defaults share one body; mixing can cause ambiguous calls.

## Answer

#### Default arguments

A default argument is a value given in the function declaration that is used automatically when the caller does not supply that argument. Default values are given from the **right side**: once a parameter has a default, all parameters to its right must also have defaults.

```cpp
float simpleInterest(float p, float t, float r = 5.0);   // r defaults to 5

simpleInterest(1000, 2);        // r = 5.0 is used
simpleInterest(1000, 2, 8);     // r = 8, default ignored
```

Advantages: fewer arguments to write, one function covers common cases, and existing calls keep working when a new parameter is added.

#### Default arguments and function overloading

A function with default arguments behaves like a set of overloaded functions. The same effect is obtained by writing several functions with the same name and different parameter lists.

```cpp
#include <iostream>
using namespace std;

// Version 1: using default arguments
int volume1(int l, int w = 1, int h = 1) {
    return l * w * h;
}

// Version 2: using function overloading (same behaviour)
int volume2(int l)               { return l; }
int volume2(int l, int w)        { return l * w; }
int volume2(int l, int w, int h) { return l * w * h; }

int main() {
    cout << volume1(5) << " " << volume1(5, 4) << " " << volume1(5, 4, 3) << endl;
    cout << volume2(5) << " " << volume2(5, 4) << " " << volume2(5, 4, 3) << endl;
    return 0;
}
```

**Output**

```text
5 20 60
5 20 60
```

The single function `volume1` replaces the three overloaded functions of `volume2`.

**Caution (ambiguity):** if overloaded functions and default arguments are mixed carelessly, calls can become ambiguous. For example, with `void f(int a, int b = 0);` and `void f(int a);`, the call `f(5)` matches both and the compiler reports an error.

| Default arguments | Function overloading |
|---|---|
| One function body | Separate body for each version |
| Same logic with some values optional | Different logic allowed for each version |
| Defaults only at the right end | Any differing parameter types or counts |

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Where must default arguments be placed?
   A. Rightmost parameters
   B. Leftmost
   C. Middle only
   D. Anywhere
   Answer: A. Right to left. (Key point: Default rule)

2. What does volume1(l, w=1, h=1) replace?
   A. Nothing
   B. Three overloaded versions
   C. A macro
   D. A class
   Answer: B. Same effect as overloads. (Key point: Equivalence)

3. Why is f(5) an error with f(int a, int b=0) and f(int a)?
   A. No match
   B. Wrong type
   C. It matches both
   D. Default ignored
   Answer: C. Ambiguity. (Key point: Ambiguity)

4. volume1(5,4) gives?
   A. 9
   B. 5
   C. 60
   D. 20
   Answer: D. 5*4*1. (Key point: Output)

## Related in Object Oriented Programming

- [Default argument and overloading program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adefault-arg-overloading-program) — Object Oriented Programming, chapter 3
- [Display N characters using default arguments](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3An-chars-default-args-program) — Object Oriented Programming, chapter 3
- [Default arguments in class template](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-template-default-args) — Object Oriented Programming, chapter 9
- [Default arguments and their syntax](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adefault-arg-define-syntax) — Object Oriented Programming, chapter 3, asked 2×
- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3
- [Binary operator member function one argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Abinary-member-one-argument) — Object Oriented Programming, chapter 5

## Related in other subjects

- [I/O functions used with data files](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-io-functions) — Computer Programming, chapter 10, asked 2×
