Parameters vs Arguments
Lesson outline
Detailed Explanation
Parameters are the variables declared in the function signature. They act as placeholders for the values that will be passed to the function. Parameters define the function's interface - what kind of data it expects to receive.
Arguments are the actual values passed to the function when it's called. They must be compatible with the parameter types (implicit conversions may apply) and appear in the correct order and count - unless some parameters have default arguments or a parameter pack is used.
| Aspect | Parameter | Argument |
|---|---|---|
| Definition | Variable in function declaration/definition | Value/expression passed during call |
| Lifetime | Exists only for the duration of the function call (automatic storage for the parameter itself) | Exists in the caller; temporaries can be created for the call (lifetime may extend for a bound const& only until end of the full-expression) |
| Memory | Storage for the parameter is created when the function is invoked | May be an existing object, a literal, or a temporary produced by an expression |
| Type Matching | Type is fixed by the function signature | Must be convertible to the parameter type (implicit conversions and promotions may occur) |
| Modification | Modifying a by-value parameter affects only the local copy; modifying a reference or pointer parameter can affect the original object | Original value is unaffected unless passed by reference or via a pointer to non-const (or if the argument itself is a modifiable object passed by reference) |
Advanced Considerations
In C++, parameters can be passed by value, reference, or pointer - each with different implications for copying, aliasing, and mutability.
Modern guidance: pass small/trivial types by value; pass large objects by const& to avoid copies when you only need to read; use T&& (rvalue reference) and std::move/std::forward when you can benefit from moves.
Default arguments let callers omit trailing arguments; they are specified on declarations (not repeated across multiple declarations in the same scope).
Default Parameters
Default arguments are used when a call omits trailing arguments. They are evaluated at the call site and must appear on a declaration (typically in a header).
Only trailing parameters can have defaults; once a parameter has a default, all following parameters must also have defaults.
Templates & Terminology
The same distinction exists for templates: template parameters (in the template definition) vs template arguments (supplied at instantiation).
Example: in template, T is a template parameter; in the call f, int is the template argument and 42 is the function argument.
Common Pitfalls & Notes
Top-level const on by-value parameters does not affect the function signature (void f(int) and void f(const int) are the same signature).
Arrays passed to functions typically decay to pointers; consider std::span (C++20) to preserve size information.
Do not rely on the relative evaluation order of function arguments - it's unspecified; avoid side effects that depend on a particular order.
Prefer clear const-correctness on references/pointers in parameter lists.
Real-World Analogy
Think of parameters like an order form (what information is needed) and arguments like the actual order details you fill in. The form structure stays the same, but the specific details change with each order.