Puzzle P12

a in main value copied to x in myFunction

Review of Call by Value

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+11 );
  printf("a=%d\n", a );
 
}

In this puzzle, the argument in the function call is the expression a+11. When the function is called, the expression is evaluated, and the resulting value of 88 is copied into the parameter x.

The point of this example is to emphasize that what is copied into a parameter is calculated at run-time using a but is not directly a and does not give access to a.



Answer         Next Page         Previous Page Home