Ad
  • Custom User Avatar

    If you don't want to copy the parameter (time and memory constraints), but want to use it without altering it, you pass it by const reference.

  • Default User Avatar

    if you don't want to change number you shouldn't pass it as a reference

  • Custom User Avatar

    Because he is passing number as a reference.
    This would mean if he would change number in the body of opposite, the value would be also manipulated outside the function.
    This is clearly not wanted.
    const declares that number will be treated as constant in this function and has two purposes:

    • the caller knows that even though he is passing number as a reference, it will not be changed
    • the programmer of opposite can not accidently change number as this would result into a compilation error.