Functions and Parameter Passing
Compare pass-by-value, pointers, and references.
What is Functions and Parameter Passing?
Function parameters are copied by default; a caller must explicitly grant access when a function should modify its object.
Important points
- Compile with warnings enabled and fix every warning.
- Know the lifetime and type of every value.
- Validate input and keep array bounds explicit.
C and C++ code examples
#include <stdio.h>
void swap_ints(int *left, int *right) {
int temporary = *left;
*left = *right;
*right = temporary;
}
int main(void) {
int a = 4, b = 9;
swap_ints(&a, &b);
printf("%d %d\n", a, b);
return 0;
}
9 4
#include <iostream>
void swap_ints(int& left, int& right) {
int temporary = left;
left = right;
right = temporary;
}
int main() {
int a = 4, b = 9;
swap_ints(a, b);
std::cout << a << ' ' << b << '\n';
}
9 4
C and C++ comparison
C passes an address and dereferences it. C++ references provide alias syntax while preserving mutation semantics. const parameters document read-only access.
C exposes small procedural APIs and makes representation details explicit.
C++ retains the low-level model while adding safer library types and abstractions.
Practice exercises
Run both versions, then modify them to observe the different language guarantees.
- Add invalid and boundary-value inputs.
- Compile with -Wall -Wextra -Wpedantic.
- Move reusable declarations into a header and implementation file.
Functions in C and C++
int add(int a, int b) { return a + b; } | Define a function that returns a value |
void greet(void); | Declare a function before use (prototype) |
void swap(int *a, int *b) | C: pass pointers so the function can change the caller's variables |
void swap(int& a, int& b) | C++: pass by reference |
void print(const std::string& s) | C++: pass large objects by const reference to avoid copying |
int area(int w, int h = 1) | C++: default argument |
double area(double r); int area(int w, int h); | C++: overloading, same name with different parameters |
static int counter(void) | C: function visible only inside this file |
Common mistakes with functions
Expecting a by-value parameter to change the caller's variable
Parameters are copies. Pass a pointer (C) or a reference (C++) to modify the original.
void reset(int x) { x = 0; }
int score = 10;
reset(score); /* score is still 10 */
void reset(int *x) { *x = 0; }
int score = 10;
reset(&score); /* score is now 0 */
Calling a function before it is declared
C needs to know a function's signature before the call. Put a prototype at the top, or define the function first.
int main(void) {
printf("%d\n", square(4)); /* implicit declaration error */
}
int square(int n) { return n * n; }
int square(int n); /* prototype */
int main(void) {
printf("%d\n", square(4));
}
int square(int n) { return n * n; }
Missing return in a non-void function
Falling off the end of a function that should return a value is undefined behavior.
int sign(int n) {
if (n > 0) return 1;
if (n < 0) return -1;
} /* nothing returned for 0 */
int sign(int n) {
if (n > 0) return 1;
if (n < 0) return -1;
return 0;
}
Frequently asked questions
What is the difference between pass by value and pass by reference?
By value, the function gets a copy, so changes stay inside it. By reference (a pointer in C, a & reference in C++), it works on the caller's variable, so changes are visible after the call.
What is a function prototype?
A declaration of a function's name, return type and parameters, ending with a semicolon, such as int add(int a, int b);. It lets you call the function before its definition appears.
Can a C function return multiple values?
Not directly. Return a struct, or pass pointers to variables the function fills in. In C++ you can also return a std::pair or std::tuple.
What is function overloading?
In C++, several functions can share a name if their parameter types differ; the compiler picks the right one from the arguments. C does not support overloading.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Distance Between Points Compute the straight-line distance between two points. Easy
- Days in a Month Return how many days a month has, including leap years. Easy