O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Passing by Reference

Sometimes you’ll need to pass an updated value of a variable back to the calling function and update the variable at the same time. A good example of this is in reading a number from the keyboard with the familiar scanf library function:

scanf ( "%d", &nNumber );

In this function call to scanf, the nNumber variable is passed by reference, (denoted by <type> and <name>). The reference in question is an actual pointer to the memory that contains the variable, and subsequently, the value can be modified.

This modification will hold outside the function, because you’ve allowed the function to access the place where it is stored. Of course, a constant value should not be passed by reference.

You can declare a function as containing ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required