←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Notes

Default Arguments & Constant Arguments in C++

Study material and explanations for Unit 2

Default Arguments & Constant Arguments in C++

1. What are Default Arguments? (In Simple Words)

In C language, if a function takes 3 inputs, you must pass all 3 inputs every single time you call it. If you pass only 2, you get an error.

C++ introduces Default Arguments.

If you do not pass a value for a parameter during the function call, the compiler automatically assigns a pre-set default value to it!

Real-Life Analogy: Think of opening a Fixed Deposit in a bank. By default, the bank sets the time period to 1 year and the interest rate to 7.0%. But if you want, you can change them to 3 years and 8.5%. If you don’t mention anything, the bank’s default values are used!


2. The Strict “Right-to-Left” Rule (Golden Exam Rule)

When adding default values to parameters, you must start from the rightmost parameter and move left.

You cannot give a default value to a left parameter while leaving a parameter to its right without a default!

Default Arguments Rule Validation
VALID Declarations
• void func(int a, int b, int c = 50); (Rightmost only)
• void func(int a, int b = 20, int c = 50); (Right two)
• void func(int a = 10, int b = 20, int c = 50); (All parameters)
INVALID Declarations (Compiler Error)
• void func(int a = 10, int b, int c = 50); (Middle missing!)
• void func(int a = 10, int b, int c); (Rightmost missing!)
Reason: The compiler gets confused about which value belongs to which variable!

3. Practical Code Example: Simple Interest Calculator

#include <iostream>
using namespace std;

// PROTOTYPE: Default years = 1, default rate = 7.0%
float computeInterest(float principal, int years = 1, float rate = 7.0f);

int main() {
    // 1. Pass only 1 argument: years defaults to 1, rate defaults to 7.0
    cout << "Case 1: Rs. " << computeInterest(10000.0f) << endl;

    // 2. Pass 2 arguments: years becomes 3, rate defaults to 7.0
    cout << "Case 2: Rs. " << computeInterest(10000.0f, 3) << endl;

    // 3. Pass all 3 arguments: overrides both defaults!
    cout << "Case 3: Rs. " << computeInterest(10000.0f, 3, 9.5f) << endl;

    return 0;
}

// DEFINITION: Note that we DO NOT repeat the '= 1' or '= 7.0f' here!
float computeInterest(float principal, int years, float rate) {
    return (principal * years * rate) / 100.0f;
}

Crucial Rule: Define default values in the Function Prototype (declaration) only. Do NOT repeat them in the function definition header.


4. Constant Arguments (const Parameters)

Sometimes you want to make sure that a function cannot change an argument by mistake. You can mark that parameter with the const keyword.

A Constant Argument makes the parameter read-only. If someone tries to modify it inside the function, the compiler throws an error immediately.

void printBill(const float total) {
    // total = total - 100; // ERROR! Compiler stops you from changing a const parameter!
    cout << "Final Bill: Rs. " << total << endl;
}

Why Use const & (Constant Reference) in C++?

When passing large objects (like a Student record with 50 fields):

  1. Pass by Value (Student s): Safe, but creates a duplicate copy in RAM, wasting time and memory.
  2. Pass by Reference (Student &s): Super fast (no copying), but the function can accidentally change your original data!
  3. Pass by Constant Reference (const Student &s): The best of both worlds!
    • Zero copying (super fast because it passes a reference).
    • 100% safe (because const prevents any modification).

DAVV Exam Preparation Tips

Frequently Asked 5-Mark Questions:

  1. Explain the rules for using default arguments in C++ with an example.
    Model Answer:
    • Define default arguments as fallback values used when caller omits them.
    • Explain the Right-to-Left rule: defaults must be placed starting at the end of the parameter list.
    • Show valid vs invalid declarations.
    • Provide a quick 5-line interest calculation snippet.
  2. What are constant arguments in C++? Why are they useful?
    Model Answer:
    • Parameters declared with const cannot be modified inside the function (read-only).
    • Useful to prevent accidental changes to original data, especially when passing large objects by reference (const Student &s).

Frequently Asked 10-Mark Questions:

  1. Explain Default Arguments and Function Overloading. Can default arguments replace function overloading? What is ambiguity?
    Model Answer:
    • Explain how a single function with default arguments (e.g., add(int a, int b, int c = 0)) can handle calls with both 2 and 3 arguments, replacing 2 overloaded functions.
    • Explain the Ambiguity problem: If you write void print(int a); and void print(int a, int b = 5);, calling print(10); causes a compiler error because the compiler cannot tell which version you intended to run!