📅 Date: Jan 9, 2026
🔥 Topic: Value vs Reference Diff & Default Arguments
⚔️ The Comparison
Yesterday I learned the code logic. Today I dug deeper into why and when to use which.
| Feature | Pass by Value | Pass by Reference (&) |
|---|---|---|
| Memory | Creates a copy (More memory). | Uses original memory (Efficient). |
| Safety | Original data is safe. | Original data can be modified. |
| Speed | Slower for large objects. | Faster (No copying involved). |
✨ Small Concept: Default Arguments
I also learned a cool trick called Default Arguments. We can give parameters a default value, so if we don't pass anything, it uses the default one.
Rule: Default arguments must be at the end of the parameter list.
#include <iostream>
using namespace std;
// 'money' has a default value of 100
void treat(string name, int money = 100) {
cout << name << " treats with Rs " << money << endl;
}
int main() {
treat("Sahil", 500); // Uses 500
treat("Rohan"); // Uses default 100!
return 0;
}
💠Thoughts
C++ gives you a lot of control. Default arguments are super useful for making flexible functions. Tomorrow is the big day—I start Arrays!
No comments:
Post a Comment