🎯 Functions in C++ — Parameters, Return Types and Overloading
Master C++ functions — declaration, definition, parameters, return types, pass-by-value vs reference, default args, overloading and inline functions with examples.
A function packages reusable code under a name. It optionally takes inputs (parameters) and returns a value.
📜 Anatomy
// declaration (prototype) — tells the compiler the signature
int add(int a, int b);
// definition — the actual code
int add(int a, int b) {
return a + b;
}
// usage
int sum = add(2, 3);📥 Pass-by-value vs pass-by-reference
- By value (default):
void f(int x)— x is a copy. Changes don't affect the caller. - By reference:
void f(int& x)— x is an alias. Changes ARE visible to the caller. - By const reference:
void f(const std::string& s)— no copy, no modification. Standard for large read-only inputs.
🪄 Default arguments
void greet(std::string name = "friend") {
std::cout << "Hi " << name;
}
greet(); // "Hi friend"
greet("Sam"); // "Hi Sam"🎭 Overloading — same name, different signatures
int area(int side);
double area(double radius);
int area(int width, int height);💻 Code Examples
Pass-by-value vs reference
void doubleVal(int x) { x *= 2; }
void doubleRef(int& x) { x *= 2; }
int a = 5;
doubleVal(a); std::cout << a << ' '; // 5 (unchanged)
doubleRef(a); std::cout << a; // 10 (modified)Output:
5 10
Overloaded area function
int area(int side) { return side * side; }
double area(double radius) { return 3.14159 * radius * radius; }
int area(int width, int height) { return width * height; }
std::cout << area(5) << ' ' << area(2.0) << ' ' << area(3, 4);Output:
25 12.5664 12
⚠️ Common Mistakes
- Passing a huge object by value when const& would avoid the copy.
- Forgetting the function declaration before main when the definition is below — compile error.
- Returning a reference to a local variable — dangling reference, undefined behavior.
- Default arguments specified in BOTH declaration and definition — pick one (usually the declaration).
🎯 Interview Questions
Real questions asked at top product and service-based companies.
Q1.What's the difference between pass-by-value and pass-by-reference?Beginner
By value copies the argument — changes inside the function don't affect the caller. By reference passes an alias — changes ARE visible to the caller. References (`&`) also avoid copy overhead for large types.
Q2.What's a function prototype?Beginner
A declaration without a body: `int add(int, int);`. It tells the compiler the function exists, allowing calls before the definition. Required when the definition is in another file or below main.
Q3.What is function overloading?Intermediate
Defining multiple functions with the SAME NAME but DIFFERENT PARAMETER LISTS (count or types). The compiler picks the matching one based on the call. Return type alone is not enough to overload.
Q4.What does `const` after a function declaration mean?Intermediate
Only valid for class member functions. `void print() const;` means 'this function does not modify the object'. Compiler enforces it — required to call from const objects.
Q5.When should you use `inline`?Advanced
Modern compilers usually inline small functions automatically. Use `inline` mainly when defining a non-template function in a HEADER file — it suppresses multiple-definition linker errors.
🧠 Quick Summary
- Function = named reusable code with optional input/output.
- Pass by value (copy), by reference (&), or by const& (read-only).
- Default arguments simplify calls.
- Overloading = same name, different parameters.
- Always declare or define a function before using it.