Call by Value
- That last program, power changed i but nothing
strange happened. This is because C is a call by value language:
when you pass a function a variable, it gets only the variables' value,
which is then put into a local variable. This local variable can then
be freely changed, and it disappears when the function is done.
- Call by value is one way in which C encourages you to write
functions without side effects, functions that don't change
the programming enviornment except in returning a value.
- Two types of side effects we are already familiar with are
- Changing a global variable from inside a function, and
- Printing or getting input from inside a function.
- People don't like side effects because they violate our
intuitive idea of what a function should be.
Next Slide