🔵 Core C++  ·  Lesson 31

Default Arguments

Default Arguments

What is Default Arguments?

Default Arguments
Default arguments allow a function parameter to have a value when the caller does not provide it.
Level
🔵 Core C++ Features
Example File
default-arguments.cpp
Main Focus
Concept + syntax + practical C++ program

Why should you learn this?

  • It helps you write correct and readable C++ programs.
  • It is used repeatedly in school practicals, projects and competitive programming.
  • It builds the base for advanced topics such as OOP, STL and data structures.

Important Terms

TermMeaning / Use
default valueDefault value used in Default Arguments programming.
parameterParameter used in Default Arguments programming.
function callFunction call used in Default Arguments programming.
optional argumentOptional argument used in Default Arguments programming.

Syntax / Pattern

void show(int x = 10);

Example Program

#include <iostream>
using namespace std;
void greet(string name = "Student"){
    cout << "Hello " << name;
}
int main(){
    greet();
    return 0;
}

Expected Output

Hello Student

Program Explanation

  • No argument is passed, so default value Student is used.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • optional settings
  • report formatting
  • menu defaults

Common Mistakes

  • Putting default argument before non-default argument.
  • Repeating defaults in both declaration and definition incorrectly.

Practice Tasks

  1. Create function bill(price, tax=18).
  2. Call same function with and without argument.

Summary

Default Arguments is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.

What is Default Arguments?

Default Arguments
Default arguments allow a function parameter to have a value when the caller does not provide it.
Level
🔵 Core C++ Features
Example File
default-arguments.cpp
Main Focus
Concept + syntax + practical C++ program

Why should you learn this?

  • It helps you write correct and readable C++ programs.
  • It is used repeatedly in school practicals, projects and competitive programming.
  • It builds the base for advanced topics such as OOP, STL and data structures.

Important Terms

TermMeaning / Use
default valueDefault value used in Default Arguments programming.
parameterParameter used in Default Arguments programming.
function callFunction call used in Default Arguments programming.
optional argumentOptional argument used in Default Arguments programming.

Syntax / Pattern

void show(int x = 10);

Example Program

#include <iostream>
using namespace std;
void greet(string name = "Student"){
    cout << "Hello " << name;
}
int main(){
    greet();
    return 0;
}

Expected Output

Hello Student

Program Explanation

  • No argument is passed, so default value Student is used.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • optional settings
  • report formatting
  • menu defaults

Common Mistakes

  • Putting default argument before non-default argument.
  • Repeating defaults in both declaration and definition incorrectly.

Practice Tasks

  1. Create function bill(price, tax=18).
  2. Call same function with and without argument.

Summary

Default Arguments is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.

← Back to C++ Tutorial