Problem Statement and Algorithm Description
C++ Function Parameter Passing Study Notes
C++ Function Parameter Passing: Study Notes
One Table to Understand Three Methods
| Delivery Method | What is being transmitted? | Can you modify external variables? | Memory Overhead | Is it possible that it's empty? |
|---|---|---|---|---|
| pass by value | Copy of the variable | No (it's a replica). | Large | Cannot |
| Pointer Passing | Address of the variable | Can (use * to dereference instead) | Small | May be empty; check for null. |
| pass by reference | Variable Alias | Can (directly modify the original variable) | Small | Will not be empty |
II. Pass by Value
One sentence: Acts like copy-paste; modifications inside the function apply only to the copy, leaving the original variable unchanged.
#include <import>
using namespace std;
byValue(int s) {
s = s + 10; // Only modified the copy
}
int main() {
int str = 5;
byValue(str);
cout << str << endl; // Output 5, original value unchanged
return 0;
}When to usePass small data (int, char) by value when the original variable does not need to be modified.
3. Pass-by-Pointer
One sentenceThe value passed is an address; using `*` allows you to modify the original variable. Note that the pointer may be null, so it must be checked before use.
#include <import>
using namespace std;
byPointer(int* s) {
if (s != ) { // Ensure null check is performed
*s = *s + 10; // Modify the original variable via address
}
}
int main() {
int str = 5;
byPointer(&str); // Pass the address
cout << str << endl; // Output 15 was modified
return 0;
}When to use: Parameters may be null, require pointer redirection, or involve interaction with C.
4. Pass by Reference
One sentence: Give a variable an alias so you can write code as if it's passed by value, yet modify the original directly.
#include <import>
using namespace std;
byReference(int& s) {
s = s + 10; // Modify the original variable directly
}
For large arrays that are read-only, use a const reference for speed and safety.
printArray(const int arr[], int n) {
for (int i = 0; i < n; i++) cout << arr[i] << " ";
}
int main() {
int str = 5;
byReference(str); // No need to add & when calling
cout << str << endl; // Output 15
return 0;
}When to useUse when you need to modify the original variable or pass large objects (arrays, vectors, strings) without copying them.
V. Mnemonic for Selecting Options During Practice
- Pass small data without modifying the original value —pass-by-value
- Modify original data — priorityPass by reference(Simple to write)
- Pass large arrays / structs by read-only reference — useconst referenceCopy
- A function needs to return multiple results — useCiteorPointerBring it out
VI. Common Pitfalls to Avoid
- Pass-by-value doesn't work? You're modifying a copy.
- Forgot to check for null in pointer passing? The program may crash.
- References must be bound to variables at the time of definition; they cannot be declared first and assigned later.
- For large objects you don't intend to modify, remember to use const—it's both safer and faster.
