Stage 4: Functions and Argument Passing in C++
An in-depth analysis of function definitions, prototypes, and the differences between passing by value and by reference.
Functions in C++: Modularity and Reusability
Functions are the basic building blocks of a program, allowing you to break down complex problems into smaller, manageable pieces. At your level, it is crucial not only to know how to write a function but also how to efficiently transfer data to it.
1. Function Structure
Every function consists of a return type, a name, a parameter list, and the function body.
return_type function_name(parameters) {
// body
return value; // if the type is not void
}Important: If the function is located below the main() function, you must place its prototype (declaration) at the top of the file.
double calculateAverage(int a, int b); // Prototype2. Passing Arguments by Value
This is the default way in C++. The function receives a copy of the variable. Changes made inside the function do not affect the original variable.
void increase(int x) {
x = x + 10; // Only changes the local copy
}
int main() {
int number = 5;
increase(number);
// number is still 5
}3. Passing by Reference (&)
By using the & symbol, you create an alias for the original variable. The function operates directly on the data from the calling site. This is more efficient (no copying of large structures) and allows a function to "return" more than one value.
void actuallyIncrease(int &x) {
x = x + 10; // Changes the original
}4. Passing Arrays to Functions
Arrays are specific – in C++, an array passed to a function automatically "decays" into a pointer to its first element. This means the function always operates on the original, and you must additionally pass the size of the array.
void printArray(int tab[], int size) {
for(int i = 0; i < size; i++) {
std::cout << tab[i] << " ";
}
}Pitfalls and Best Practices for Exams
- Returning Values (
return): Remember that a function terminates immediately upon hitting areturn. If a function is supposed to return anint, ensure every logical path (e.g., inside anif) ends with areturnstatement. - Local Variables: Variables declared inside a function cease to exist when it finishes. Never try to return a reference to a local variable!
- void: Use
voidwhen a function is meant to perform an action (e.g., print text) rather than compute a value. Invoidfunctions, thereturn;statement (without a value) is used to exit the function early.
Example: Swap Function
This is a classic exam task testing the understanding of references:
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}You might also like
Stage 11: Pointer Arithmetic in C++
Understanding how C++ operates on memory addresses. Learn why ptr++ is more than just adding one.
Stage 10: Pointers, Address-of, and Dereference Operators
Understanding the foundations of memory addressing in C++: how pointers and low-level operators work.
Stage 6: C-Style String Handling (char[])
A guide to low-level string processing as null-terminated character arrays, following exam constraints.