Functions
A function is a named block of code you can call with arguments. It has a return type, a name, a parameter list, and a body. The declaration tells callers the signature; the definition supplies the body the linker will find.
Detailed Explanation
You need to compute a price with tax in three different screens. Copying the formula is how the third screen gets a different rounding. A function puts the formula in one place: double withTax(double price, double rate). Callers pass values in; the function returns a result out.
A function has two textual forms. A declaration (often in a header) is the signature plus a semicolon: double withTax(double price, double rate);. A definition is the signature plus a body. You may declare a function many times. You may define it once, unless it is inline or a template. Including a header that contains only a declaration is how every translation unit learns the name without duplicating the body.
Parameters are passed according to their type. T is a copy. T& is a mutable alias. const T& is a read-only alias and the default for objects that are expensive to copy. T&& is an rvalue reference, used for sinks that take ownership. T* is optional or reseatable. Return is the same idea in reverse: return T when the caller should own a new value, T& when you are handing back something you already own (an element of *this), and never a reference to a local.
C++ does not have named arguments. The call is positional. Default arguments (covered in their own page) fill from the right. Overloading (also its own page) lets several functions share a name if the parameter types differ.
void as a return type means the function does not produce a value. A non-void function must return on every path; falling off the end is undefined behaviour. main is the exception: falling off main returns 0.
Functions have linkage. A function at namespace scope has external linkage by default: the linker can see it from other object files. static at namespace scope, or an unnamed namespace, gives internal linkage: the name is local to the translation unit. inline on a function defined in a header lets multiple translation units include the body; the linker keeps one copy.
Member functions are functions with an implicit this parameter. Free functions are not members. Prefer a free function when the operation does not need private state (std::swap, stream inserters). Prefer a member when the operation is part of the type's contract (v.push_back).
[[nodiscard]] on a return type or a function asks the compiler to warn if the caller ignores the result. Use it on functions whose return is the point (allocate, parse) or whose return is an error code you must check.
Recursive functions call themselves. Each call pushes a frame. C++ does not guarantee tail-call elimination. Deep recursion on large inputs is a stack overflow, not a slow loop. Interview problems that say "use recursion" still need a depth bound or an explicit stack.
Code Examples
#include <iostream>
double withTax(double price, double rate); // declaration
double withTax(double price, double rate) { // definition
return price * (1.0 + rate);
}
int main() {
std::cout << withTax(40.0, 0.10) << '\n'; // prints: 44
}#include <string>
#include <utility>
void appendSuffix(std::string& text, const std::string& suffix) {
text += suffix; // mutates the caller
}
std::string join(std::string a, std::string b) {
a += b;
return a; // named return; elision / move applies
}
int main() {
std::string name = "ada";
appendSuffix(name, "@lab");
auto both = join(std::move(name), ".txt");
(void)both;
}Common Gotchas
Falling off a non-void function
`int f(bool e) { if (e) return 1; }` has no return when `e` is false. The compiler may warn; the behaviour is undefined if that path runs.
Returning a reference to a local
`const std::string& f() { std::string s = "x"; return s; }` dangles. Return by value.
A definition in a header without `inline`
two `.cpp` files include it and the linker reports a duplicate symbol. Put the body in one `.cpp`, or mark it `inline`.
`main` cannot be overloaded or templated
its type is special. Use `int main()` or `int main(int argc, char* argv[])`.