C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1

The next chapter finishes with the passing of values between functions by showing you how to return a
value from one function to another. Also, you will finally understand the true use of stdio.h.


The Absolute Minimum
The goal of this chapter was to show you how to share local variables between
functions. When one function needs access to a local variable defined in another
function, you must pass that variable. The parentheses after function names contain the
variables you’re passing and receiving.
Normally, you pass non-array variables by value, which means that the receiving
function can use them but not affect their values in the calling function. Arrays are
passed by address, which means that if the receiving function changes them, the array
variables are also changed in the calling function. You can pass non-array variables by
address by preceding them with the address-of operator, &, and receiving them with the
dereference operator, *. Key concepts from this chapter include:


  • Pass local variables from one function to another if you want the functions to share
    local variables.

  • Pass variables by value if you want their values protected from the called function.

  • Pass variables by address if you want their values changed by the called function.

  • Place an & before non-array variables you want to pass by address. Leave off the &
    if you want to pass arrays.

  • Don’t pass an array variable by value; C has no way to do that.

Free download pdf