June 2025
Intermediate to advanced
1093 pages
33h 24m
English
In C++, the declaration of the parameter in the function header determines how the parameter behaves when called. The most important distinction is whether the parameter is passed as a value or as a reference.
If you do not take any special precautions, the parameters of a function in C++ are passed as values (call-by-value). This means that if a variable x currently has the value 5 and you pass x to a function, then the 5 ends up in the function—not the x.
// https://godbolt.org/z/8zcvMdM3b#include <iostream>void print_val8(int n) { // parameter as value std::cout << n << " "; n = 8; // sets parameter to 8 std::cout << n << "\n";}int main() { int x = 5; print_val8(x); // x as value: prints ...
Read now
Unlock full access