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

(Romina) #1

31. Passing Variables to Your Functions


In This Chapter


  • Passing arguments to functions

  • Differentiating between passing by value and passing by address


The preceding chapter left some questions unanswered. If multiple functions are good (they are), and
if local variables are good (they are), then you must have a way to share local variables between
functions that need to share them (there is a way). You don’t want all functions to have access to all
variables because not every function needs access to every variable. If full variable access between
functions is needed, you might as well use global variables.


To share data from function to function, you must pass variables from function to function. When one
function passes a variable to another function, only those two functions have access to the variable
(assuming that the variable is local). This chapter explains how to pass variables between functions.


Passing Arguments


When you pass a variable from one function to another, you are passing an argument from the first
function to the next. You can pass more than one variable at a time. The receiving function receives
the parameters from the function that sent the variables.


Warning

The words variable, argument, and parameter are sometimes used interchangeably
when passing and receiving values. The name is not as important as understanding
what is happening. Figure 31.1 helps explain these terms.

FIGURE 31.1 Getting the terms correct.
Free download pdf