Argument-Passing by Value

Just like in MATLAB, each function has a separate workspace -- you can have a variable in each workspace with the same name as a variable in the other function's workspace, and they'll both be recognized as separate variables.
This is illustrated in the example arg_by_value.c

 

 

When MyFunction( ) is called in main( ), the input argument is the value of 'a'.
MyFunction (a);


The 'a' in the MyFunction( ) is not the same as the 'a' in main( ). But since the value of 'a' in main( ) was passed to MyFunction( ) when it was called, the 'a' in MyFunction is initialized to 2. This could be seen by printing out the value of 'a' inside MyFunction( ).
The value of variable 'a' is then changed in MyFunction( ).
void MyFunction (int a) { printf ("Value of 'a' inside MyFunction( ) before being changed is %d\n", a); a = 9; printf ("Value of 'a' inside MyFunction( ) after being changed is %d\n", a); }
But this only changes the value of 'a' that belongs to MyFunction( ), and not the 'a' in main( )'s workspace.
The value of 'a' in main( ) is still 2.

Back to Index