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

# Default arguments and their syntax

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

- Asked in: 2073 Shrawan, 2+2 marks; 2072 Kartik, 4 marks (asked 2 times)
- Video: [Default Arguments | Introduction to Programming with C++ | Part 42](https://www.youtube.com/watch?v=5RXsDgCbavQ) — Coding Cleverly
- 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-arg-define-syntax
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-3

## Question

What do you understand by default arguments? Write the syntax of default arguments.

## Short answer

Default arguments provide predetermined values for function parameters so callers may omit trailing arguments. Syntax: return_type func(type p1, type p2=val, ...); defaults must be right-to-left. Only constants, global variables, or static expressions can be used.

## Answer

A **default argument** is a value given to a function parameter in the function declaration. If the caller leaves that argument out, the compiler automatically uses the default value. If the caller supplies a value, the default is ignored.

#### Syntax

```cpp
return_type function_name(type p1, type p2 = default_value, type p3 = default_value);
```

- Default values are given from **right to left**: once a parameter has a default, all parameters to its right must also have defaults.
- The default is written once, in the declaration (prototype) or in the definition if there is no prototype, not in both.
- Default values may be constants, global variables or expressions, but not local variables.

#### Example

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

// w and h take default values
float volume(float l, float w = 1, float h = 1)
{
    return l * w * h;
}

int main()
{
    cout << volume(5) << endl;         // 5*1*1 = 5
    cout << volume(5, 4) << endl;      // 5*4*1 = 20
    cout << volume(5, 4, 3) << endl;   // 5*4*3 = 60
    return 0;
}
```

Valid: `void f(int a, int b = 2, int c = 3);`
Invalid: `void f(int a = 1, int b, int c = 3);` because `b` has no default but lies to the right of a defaulted parameter.

**Advantage:** one function can be called in several ways, which reduces the number of overloaded functions we must write.

## Check yourself

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

1. What is a default argument?
   A. A value in the declaration used when caller omits the argument
   B. A value always used
   C. A global
   D. A return type
   Answer: A. Used if omitted. (Key point: Definition)

2. Which is invalid?
   A. void f(int a, int b = 2, int c = 3);
   B. void f(int a = 1, int b, int c = 3);
   C. void f(int a, int b = 2);
   D. void f(int a = 1);
   Answer: B. b lacks default to the right of a defaulted one. (Key point: Invalid)

3. Where is the default written?
   A. Both
   B. Only call
   C. Once, in declaration (or definition if none)
   D. Only main
   Answer: C. Not twice. (Key point: Where specified)

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

## Related in Object Oriented Programming

- [Default arguments and function overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adefault-args-function-overloading) — Object Oriented Programming, chapter 3, asked 5×
- [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
- [Class and function template with syntax](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-function-template-syntax) — Object Oriented Programming, chapter 9, asked 4×
