Interview Q&A · C++ · 9 questions in this topic

Functions, Overloading & Argument Passing — C++ Interview Q&A

All C++ topics

How C++ functions really work — overloading, default arguments, inline, pass-by-value vs reference, and function pointers. Part of Functions & Lambdas · 213 questions in the C++ bank.

What is function overloading and how does the compiler choose an overload?

Answer

Function overloading lets multiple functions share a name with different parameter lists (arity or types). The compiler picks the best viable candidate through overload resolution: exact matches beat promotions, which beat standard conversions, which beat user-defined conversions.

Return type alone never distinguishes overloads, and overloads that differ only in top-level const on by-value parameters are redeclarations, not overloads.

Key points

  1. 1Overloads must differ in parameter count, types or (for member functions) ref/const qualifiers.
  2. 2Resolution order — exact match, promotion (float to double), standard conversion, user conversion, ellipsis.
  3. 3Ambiguous calls are compile errors, not runtime guesses.

Example

Example.cppcpp
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
add(2, 3);   // int version
add(2.0, 3); // double version (promotion of 3)

💡 Mention that return type is not part of the signature for overload purposes — it is the most frequently probed edge.

What are default arguments? What rules apply to them?

Answer

Default arguments supply a value when the caller omits it. They must appear trailing (no non-default parameter may follow a defaulted one in the same declaration), can only be specified once per scope (usually in the declaration, not the definition), and are bound at the call site — so changing a default in a header requires recompiling callers.

Defaults on virtual functions are especially dangerous because the default used is the static type's, while the function executed is the dynamic type's.

Example

Example.cppcpp
void log(const std::string& msg, int level = 1);
log("boot");        // level = 1
log("disk full", 3); // level = 3

What does `inline` actually do in modern C++?

Answer

inline is primarily a linkage directive: it allows a function (or variable, since C++17) to be defined in multiple translation units, with the linker merging them into one — which is why header-defined functions must be inline. The inlining optimization hint is secondary; the compiler inlines on its own heuristics regardless of the keyword.

So: inline solves the ODR problem for header definitions first, and suggests inlining only weakly second.

Example

Example.cppcpp
// header.h
#pragma once
inline int square(int x) { return x * x; }
inline int counter = 0; // inline variable (C++17): one shared instance

💡 Say ODR-safe header definitions first, optimization hint second — that ordering is what separates modern answers from 1990s answers.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits