Call by Value

C passes arguments to functions by value. This is different from Fortran (which uses call by reference) and Pascal (which provides both call by value and call by reference). Let's see if we can understand what's going on in C.

Take a look at ``examples/value/value.c'' (or view it directly). Notice that it reads an integer, stores the value in ``n'', and passes ``n'' to the function ``changeit'', which alters the value of its parameter ``x''. Back in the main procedure, the final value of ``n'' is printed out.

Look at this program very carefully and try to predict how this program will behave. Then compile and run it a few times to see whether your intuition was correct. Try compiling it directly in the UNIX Shell window to get some practice doing it that way.

Were you surprised with what happened?

Click here for an explanation


Hamlet Project
hamlet@cs.utah.edu