Parameters, Argument Passing and Return Values in C++
A parameter is a named input in a function declaration or definition; an argument is the expression supplied in a function call.
Summary
A parameter is a named input in a function declaration or definition; an argument is the expression supplied in a function call. Parameter types determine whether a function works with an independent value or accesses an existing object.
| Passing method | Behavior | Effect on caller's object |
|---|---|---|
| Value: int x | Initializes a separate parameter object | Assigning to x does not change the caller's integer |
| Reference: int& x | Binds to an existing modifiable integer | Assigning to x changes that integer |
| Const reference: const std::string& s | Accesses a string without copying it into a value parameter | Modification through s is prohibited |
A pointer parameter such as int* p receives the pointer by value. Assigning through *p can modify a valid target, but assigning to p does not replace the caller's pointer. Dereferencing requires a valid target; nullptr identifies no object.
The return type specifies a function's result. Returning by value supplies a result without requiring a local object to remain alive. Returning a reference or pointer to a local automatic object creates a dangling result after the function exits. Using such dangling results can cause undefined behavior.
A void function supplies no result value and can exit using return;. Reaching the end of a non-void function is generally undefined behavior; main is an exception and implicitly returns zero.
Common Misconceptions: A pointer parameter is not automatically passed by reference. A const reference restricts modification through that parameter and can bind to a temporary. Returning a reference does not extend the lifetime of a local automatic object.
🧠 Key Concepts
- Parameters and arguments
- Pass by value
- Pass by reference
- Const references
- Pointer parameters
- Return values
- Object lifetime
- Dangling references
- Void functions
🧠 Quick Check
See what you remember from the summary.
Given void f(int x) and the call f(count), what roles do x and count have?
🧠 Flashcards Preview
Tap a card to reveal the definition.
Ready to quiz yourself?
Test what you remember with a full practice quiz on this note. Create a free account and start in seconds.
Full Notes
Read the original note content before deciding whether to save or study from it.
Parameters, Argument Passing and Return Values in C++
📘 Overview A parameter is a named input in a function declaration or definition; an argument is the expression supplied when the function is called. Parameter types determine whether the function receives a separate value or accesses an existing object. The return type specifies the function's result.
🧠 Key Idea Passing by value creates an independent parameter object, while passing by reference binds a parameter to an existing object. Returning by value produces a result without requiring a local object to remain alive after the function finishes.
⚔️ Core Details: - Pass by value: void f(int x) initializes a separate parameter from the argument; assigning to x does not change the caller's integer. - Pass by reference: void f(int& x) binds x to the caller's modifiable integer; assigning to x changes that integer. - Pass by const reference: void f(const std::string& s) provides read-only access through s without copying the string into a value parameter. - Pointer parameter: void f(int* p) receives a pointer by value; changing *p can modify the pointed-to integer, but assigning to p does not replace the caller's pointer. - Return values: int square(int x) { return x * x; } supplies an integer result to its caller. - Reference or pointer returns must not expose local automatic objects whose lifetimes end when the function returns.
🎯 Why It Matters: - Parameter choices express interface intent: independent input, read-only access, or permission to modify caller-owned state. - Const references can avoid expensive copies of large objects while restricting modification through the parameter. - Lifetime-aware return types prevent dangling references and pointers, which can cause undefined behavior when used. - Returning results directly makes data flow explicit and supports composing function calls into larger expressions.
🧠 Quick Recall: - Parameter versus argument - In void f(int x), x is a parameter; in f(count), count is the argument expression. - Reference binding - int& normally requires a modifiable int lvalue; const int& can also bind to a temporary. - Pointer validity - Dereferencing a pointer requires a valid target; nullptr does not identify an object. - void - A return type indicating no result value; return; exits the function without supplying a value. - Missing return - Reaching the end of a non-void function is generally undefined behavior; reaching the end of main returns zero.
More ways to study when you copy this note
Copy this note into your library to unlock focused practice sessions and long-term review.
Answer all questions first, then see feedback at the end — the way real exams work.
Focuses each session on what you got wrong, not what you already know.
Full timed exam with all questions, no pausing, and results at the end. Built for board exam prep.
Part of 🗄️ BS Information Systems — 1st Year
Browse this Review Set →More Computer Science notes
See all →More in Programming Fundamentals
See all →More from NoteLib
Browse NoteLib's public notes →Copy this note to your library and get the full Study Pack instantly — summary, key concepts, and practice quiz included.