Pass by value

Give a function its own value without modifying the caller's variable.

Syntax

void setToZero(int value)
{
	value = 0;
}

int number { 5 };
setToZero(number);
// number is still 5

The parameter is a separate object initialized from the argument. Changing the parameter does not change the caller’s variable.

When to use it

  • Small and inexpensive types such as int, double, and char
  • Values the function needs to store or modify independently
  • Cases where copying is intentional