What does the following code write to the monitor?
#include <stdio.h> void myFunction( int x ) { printf(" x=%d\n", x ); x = 123; printf(" x=%d\n", x ); } void main ( void ) { int a = 77; printf("a=%d\n", a ); myFunction( a ); printf("a=%d\n", a ); }
The diagram shows main()
and myFunction()
, each as a module with its
own local variable or parameter. The variable a
has block scope and can be seen
only by main()
. The parameter x
has block scope and
can be seen only by myFunction()
.
When myFunction()
is called by main()
the value in a
is copied into x
. In this example 77 is copied into x
.
Changes that myFunction()
make to its parameter
x
do not affect a
. In this example, x
starts out as 77, but then is changed to 123. But this change does not
affect a
.
How to Confuse People:
Usually programmers say "myFunction is called with a."
It sounds as if a
somehow is connected to the function.
But programmers should say "myFunction is called with the value in a."