OnlineCBT Logo OnlineCBT
C++ · LESSON 6

Functions, Default Arguments, and Inline Functions

By Ratan Updated: 15 Oct 2026

Write reusable functions with default parameter values and inline function optimization.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

Design a clear function interface

A function declaration specifies a name, parameter types and return type. Default arguments make trailing inputs optional at a call site. inline affects definition and linkage rules; it is not a command forcing the compiler to replace every call with the function body.

Example with a default argument

#include 
#include 
std::string greeting(const std::string& name,
                     const std::string& prefix = "Hello");
int main() {
    std::cout << greeting("Asha") << '\n';
    std::cout << greeting("Asha", "Welcome") << '\n';
}
std::string greeting(const std::string& name, const std::string& prefix) {
    return prefix + ", " + name;
}

Expected lines: Hello, Asha and Welcome, Asha. The default is visible in the declaration used by callers. Do not repeat the same default in the definition. Put required parameters before optional trailing parameters.

Choose value, reference or const reference

Pass a small numeric input by value. Pass a larger read-only object by const reference when avoiding a copy is appropriate. Use a non-const reference only when the function's contract intentionally modifies the caller's object. Returning a std::string by value is safe: the caller receives a result with its own lifetime.

Inline definitions in headers

// score.h
#ifndef ONLINECBT_SCORE_H
#define ONLINECBT_SCORE_H
inline bool passed(int mark) {
    return mark >= 40;
}
#endif

An inline function may be defined in multiple translation units subject to the one-definition rule's requirements. Its definitions must agree. A compiler may inline an ordinary function or decline to inline one marked inline. Functions defined inside a class definition are normally implicitly inline.

Defaults and overloads can conflict

If one overload accepts one argument and another accepts two with a default for the second, a one-argument call can become ambiguous. Use defaults to simplify a single clear operation, not to create a maze of overlapping signatures. Default argument expressions are evaluated when the call is made.

Exercises

  1. Write a function repeat(char ch, int count = 3) returning a string. repeat('*') should return three stars.
  2. Define what happens when count is negative; reject it before conversion to an unsigned size.
  3. Why must the default be visible to the caller? Default arguments are resolved from declarations at the call site.
  4. Does inline guarantee faster execution? No. Measure performance if that question matters.

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
Address Contact

+91 7877547686

E-mail

onlinecbtportal@gmail.com

Helpline Number

+91 7877547686


Click To Download
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat