31. How Do Functions Share Data?

By Passing Variables

image

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 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

image

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.

image

Methods of Passing Arguments

There are two ways to pass arguments from a function to another function: by value and by address. Both of these methods pass arguments to a receiving function from a calling function. There is also a way to return a value from a function back to the calling function (see the next chapter).

Clue

image

All this passing of values talk focuses on the parentheses that follow function names. That’s right, those empty parentheses have a use after all! The variables you want to pass go inside the parentheses of the function call, and also in the receiving function, as you’ll see in the next section.

Note

image

Yes, this passing values stuff is important! It’s easy, though, as you’ll see.

Passing by Value

Sometimes passing by value is called passing by copy. You’ll hear these terms used interchangeably because they mean the same thing. Passing by value means that the value of the variable is passed to the receiving function, not the variable itself. Here is a program that passes a value from main() to half():

image

Here is the program’s output:

image

Study this first line of the half() function:

half(int i)  /* Receives value of i */

Notice that you must put the data type (int) inside the receiving function’s parameter list. As Figure 31.2 shows, the contents of i are passed to half(). The i in main() is never changed because only a copy of its value is passed.

Figure 31.2. The value of i is passed, not the variable i.

image

If you passed more than one variable separated by commas, all would have to have their data types listed as well, even if they were all the same type. Here is a function that receives three variables: a floating-point, a character array, and an integer:

aFun(float x, char name[15], int age)  /* Receives three
arguments */

Warning

image

Passing by value protects a variable. If the receiving function changes a passed-by-value variable, the calling function’s variable is left unchanged. Therefore, passing by value is always safe because the receiving function can’t change the passing function’s variables—only use them.

Note

image

If the previous program’s receiving function called its parameter i2, the program would still work the way it does now. The i2 would be local to half(), whereas the i in main() would be local to main(). The i2s would be local to the half() function and distinct from main().

C uses the passing by value method for all non-array variables. Therefore, if you pass any variable that is not an array to a function, only a copy of that variable’s value is passed. The variable will be left unchanged in the calling function no matter what the called function does with the value.

Passing by Address

When you pass an array to another function, the array is passed by address. Instead of a copy of the array being passed, the memory address of the array is passed. The receiving function then places its receiving parameter array over the address passed. The bottom line is that the receiving function works with the same address as the calling function. If the receiving function changes one of the variables in the parameter list, the calling function’s argument changes as well.

The following program passes an array to a function. The function puts x throughout the array and then main() prints the array. Notice that main() prints all xs because the function changed the argument.

image

This program produces the following output:

Back in main(), the name is now xxxxxxxxxxxxxxx.

If you want to override the passing of non-arrays by value, you can force C to pass regular non-array variables by address. However, doing so looks really crazy! Here is a program, similar to the first one you saw in this chapter, that produces a different output:

image

Here is the output from the program:

image

Clue

image

Now scanf() is not so unfamiliar. Remember that you put an & before non-array variables but not before array variables that you pass to scanf(). When you call scanf(), you must pass it the address of variables so that scanf() can change the variables. Because strings are arrays, when you get a string from the keyboard, you don’t put an address-of operator before the array name.

Here is a program that passes an integer i by value, a floating-point x by address, and an integer array by address (as all arrays should be passed):

image

image

Here is the output from the program:

image

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.

Rewards

image

• 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.

Pitfalls

image

• Don’t pass an array variable by value. There is no way to do that in C.

In Review

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, *.

Code Example

image

Code Analysis

This program accepts three variables in main() and then prints their total in the addThem() function. The variables are passed by value to addThem(). When a function receives variables, as done here, you must put the data type in front of each of them. The following is not allowed for the first line of addThem():

addThem(int i, j, k)  /* Invalid. int is required
                         three times */

The local variable total holds the sum of the three passed variables. After the total is printed, control is returned to main(). The 0 that you see following the return statements in this book’s programs will finally be explained in the next chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.119.251