⚡0 XP🔥0

Functions, References and Overloading

Lesson 2 of 12 ⏱ 5 min read ✎ 2 exercises ❓ 4-question quiz ▶ Runs C++

References and pass-by-reference, const references, function overloading, default arguments, inline functions and lambdas.

References#

A reference is an alias - a second name for an existing variable. It is declared with & after the type and must be initialised:

▶ Try it YourselfC++
Ctrl + Enter to run
Output
marks = 85, m = 85
Same address? yes
हिंदी में समझें

Reference किसी variable का दूसरा नाम है (जैसे किसी का घर का नाम)। int &m = marks; के बाद m और marks एक ही memory को दर्शाते हैं - m बदला तो marks भी बदल गया। Reference बनाते समय ही उसे किसी variable से जोड़ना ज़रूरी है और बाद में उसे बदला नहीं जा सकता।

Pointer Reference
Syntax int *p = &x; then *p int &r = x; then just r
Can be null yes no
Can point elsewhere later yes no (bound forever)
Typical use dynamic memory, optional values function parameters, aliases

Pass by value vs pass by reference#

▶ Try it YourselfC++
Ctrl + Enter to run
Output
After copy version: 60
After reference version: 65

The reference version changes the caller's variable - like C pointers, but cleaner: no & at the call, no * inside.

const references - fast and safe#

Copying a big object (a long string, a vector of 1 lakh items) is slow. Pass it by const reference: no copy, and the compiler stops you from changing it.

▶ Try it YourselfC++
Ctrl + Enter to run
Output
Vowels: 10
Tip

Rule of thumb: pass small types (int, double, char) by value; pass strings, vectors and objects by const & if you only read them, and by & if you need to modify them.

Function overloading#

In C every function needs a unique name. In C++ several functions can share a name if their parameter lists differ (number or types). The compiler picks the right one from the arguments:

▶ Try it YourselfC++
Ctrl + Enter to run
Output
5
5.6
6
Vindhyaedu
Common Mistake

Two functions that differ only in return type (int f(int) and double f(int)) are an error - the compiler cannot tell which one you mean from the call.

Default arguments#

Parameters can have default values, used when the caller leaves them out. Defaults must be at the end of the parameter list:

▶ Try it YourselfC++
Ctrl + Enter to run
Output
1180
1062
945

inline functions#

inline suggests that the compiler paste the function body at the call site instead of making a real call - useful for tiny, frequently called functions. Modern compilers decide this themselves, so inline today mainly allows a function to be defined in a header file.

inline int square(int x) { return x * x; }

Lambdas - small unnamed functions#

A lambda is a function you write right where you need it - very common with STL algorithms:

▶ Try it YourselfC++
Ctrl + Enter to run
Output
81
75

[ ] is the capture list (which outside variables the lambda can use), ( ) the parameters, { } the body.

Summary#

  • A reference (int &r = x;) is an alias; pass by & to modify, by const & to read big objects without copying.
  • Overloaded functions share a name but differ in parameters.
  • Default arguments fill in trailing parameters.
  • Lambdas are inline, unnamed functions: [capture](params) { body }.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Swap by reference

Write void swapValues(int &a, int &b) (no pointers) so that main prints x = 8, y = 3.

Expected output:
x = 8, y = 3
2

Overloaded area()

Write three overloads of area: area(double r) for a circle (use 3.14159), area(double l, double b) for a rectangle, and area(int side) for a square. Print with fixed << setprecision(2):

Circle: 78.54
Rectangle: 12.50
Square: 49.00
Expected output:
Circle: 78.54
Rectangle: 12.50
Square: 49.00

Quick Quiz

Q1.

After int x = 5; int &r = x; r = 9; what is x?

Q2.

Which pair of functions is a valid overload?

Q3.

For void print(int a, int b = 10); which call is valid?

Q4.

Why pass a large object as const string &s?