Parameter passing in C is always done by call by value. This means that when a function is called, arguments of the function call are evaluated and the values are copied into the parameters of the function. As the function executes it works with the values in its parameters. Changing these values does not affect any other variable in the program.
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 ); system("pause"); } |
|
The diagram shows Changes that |
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 ); system("pause"); } |
|
In this puzzle, the argument in the function call is the expression |
What does the following code write to the monitor? #include <stdio.h> void functionB( int y ) { printf(" y=%d\n", y ); y = 999; printf(" y=%d\n", y ); } void functionA( int x ) { printf(" x=%d\n", x ); x = 123; functionB( x ); printf(" x=%d\n", x ); } void main ( void ) { int a = 77; printf("a=%d\n", a ); functionA( a ); printf("a=%d\n", a ); system("pause"); } |
|
In this puzzle |
What does the following code write to the monitor? #include <stdio.h> void newFunction( int *p ) { printf(" *p=%d\n", *p ); *p = 123; printf(" *p=%d\n", *p ); } void main ( void ) { int a = 77 ; printf("a=%d\n", a ) ; newFunction( &a ) ; printf("a=%d\n", a ) ; system("pause") ; } |
|
Call by value is used in this example, also.
However, the parameter
When printf(" *p=%d\n", *p ); follows the pointer in *p = 123; follows the pointer in |
Does the following code compile? If so, what does it write to the monitor? #include <stdio.h> void newFunction( int *p ) { printf(" p=%d\n", p ); /* Note: no * before p */ } void main ( void ) { int a = 77 ; printf("a=%d\n", a ) ; newFunction( &a ) ; system("pause") ; } |
|
It looks like |
Let us make a dreadful mistake. This time, intentionally. (Unlike those many unintentional dreadful mistakes.) What is the dreadful mistake? Will the program compile? What will happen when it runs? #include <stdio.h> void newFunction( int *p ) { printf(" p=%d\n", *p ); } void main ( void ) { int a = 77 ; printf("a=%d\n", a ) ; newFunction( a ) ; system("pause") ; } |
|
It looks like |
Of course, functions can call functions, and pass pointers along. What does the following code write to the monitor? #include <stdio.h> void functionB( int *y ) { printf(" *y=%d\n", *y ); *y = 999; printf(" *y=%d\n", *y ); } void functionA( int *x ) { printf(" *x=%d\n", *x ); functionB( x ); /* Note this!! */ printf(" *x=%d\n", *x ); } void main ( void ) { int a = 77; printf("a=%d\n", a ); functionA( &a ); printf("a=%d\n", a ); system("pause"); } |
|
functionA( &a ) printf(" *x=%d\n", *x ) Then functionB( x ) Bugs and more bugs! Look carefully. The parameter functionB( &x ) would pass a pointer to functionB( *x ) would pass the value in The call
printf(" *y=%d\n", *y ) it follows *y = 999 which follows the pointer in |
Of course, a function can be called several times, with different arguments. What does the following write to the monitor? #include <stdio.h> void aFunction( int *p ) { *p = 99; } void main ( void ) { int a = 44, b = 77 ; printf("a=%d b=%d\n", a, b ) ; aFunction( &a ) ; printf("a=%d b=%d\n", a, b ) ; aFunction( &b ) ; printf("a=%d b=%d\n", a, b ) ; system("pause") ; } |
|
In the first call, the parameter of
|
In the following,
#include <stdio.h> void swap( int x, int y ) { int temp; printf(" x=%d y=%d\n", x, y ) ; temp = x; x = y; y = temp; printf(" x=%d y=%d\n", x, y ) ; } void main ( void ) { int a = 44, b = 77 ; printf("a=%d b=%d\n", a, b ) ; swap( a, b ) ; printf("a=%d b=%d\n", a, b ) ; system("pause") ; } |
|
This puzzle shows a classic C programming error. Be sure you understand what goes wrong. |
The #include <stdio.h> void swap( int x, int y ) { int temp; printf(" x=%d y=%d\n", x, y ) ; temp = x; x = y; y = temp; printf(" x=%d y=%d\n", x, y ) ; } void main ( void ) { int a = 44, b = 77 ; printf("a=%d b=%d\n", a, b ) ; swap( a, b ) ; printf("a=%d b=%d\n", a, b ) ; system("pause") ; } |
|
Hint: you can do this by inserting sixteen characters. |