C uses call by value for passing of parameters in contrast to C++ which
uses call by reference.How is call by value advantageous and how is it
implemented?
Say we've got this code.
double distsquared = 100000000;
double dist;
double force;
double radius = 8000.0;
dist = sqrt(distsquared);
if(dist > radius)
force = 1 / distsquared;
perfectly unexceptional code. We are calculating the force between two
planets, which is the reciprocal of the square of distance. However if the
distance is less than the radius, then the calculation breaks down.
Now the code works because we know that the variable distsquared hasn't been
changed by the call to sqrt().
If we had a call by reference semantic, we wouldn't know this.
distsquared could be prserved with its original value, it could be set to
the square root, it could be corrupted or set to zero. We don't know, so our
finger is in the back of the manual, looking for the convention the writer
of sqrt() has chosen.
In call be reference the fucntion sqrt() can "see" distsquared, and work on
it directly.
It is however harder to implement call by value.
The normal way is to maintain a "stack".
So when sqrt() is called, the value of distsquared is pushed onto the stack.
The sqrt() function can then corrupt it to its heart's content. When control
returns, the stack is popped, and the copy discarded.
Incidentally your C++ comment is only partly correct. See the comp.lang.c++
faq for some more infomration about C++ parameter passing.