🔵 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.
Default arguments allow a function parameter to have a value when the caller does not provide it.
Level
🔵 Core C++ Features
🔵 Core C++ Features
Example File
default-arguments.cppMain Focus
Concept + syntax + practical C++ program
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
| Term | Meaning / Use |
|---|---|
| default value | Default value used in Default Arguments programming. |
| parameter | Parameter used in Default Arguments programming. |
| function call | Function call used in Default Arguments programming. |
| optional argument | Optional 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
- Create function bill(price, tax=18).
- 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.
Default arguments allow a function parameter to have a value when the caller does not provide it.
Level
🔵 Core C++ Features
🔵 Core C++ Features
Example File
default-arguments.cppMain Focus
Concept + syntax + practical C++ program
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
| Term | Meaning / Use |
|---|---|
| default value | Default value used in Default Arguments programming. |
| parameter | Parameter used in Default Arguments programming. |
| function call | Function call used in Default Arguments programming. |
| optional argument | Optional 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
- Create function bill(price, tax=18).
- 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.