6.2.1. Passing Arguments by Value
When we initialize a nonreference type variable, the value of the initializer is copied. Changes made to the variable have no effect on the initializer:
int n = 0; // ordinary variable of type intint i = n; // i is a copy of the value in ni = 42; // value in i is changed; n is unchanged
Passing an argument by value works exactly the same way; nothing the function does to the parameter can affect the argument. For example, inside fact
(§ 6.1, p. 202) the parameter val
is decremented:
ret *= val--; // decrements the value of val
Although fact
changes the value of val
, that ...
Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.