FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
TABLE OF CONTENTS
July 10, 2008

Computer Programming and Precise Terminology

(Page 4 of 4)

Using lvalues and rvalues to Explain Argument Passing

Pass by Value

You can also use the lvalue-rvalue diagram and the Bucket Analogy to explain the concepts of "pass by value" and "pass by reference". For example, for several different languages we might find data definitions similar to those in Table 7.

Table 7: Data Definitions and Method Calling in Different Languages

Despite the minor language differences in Table 7, all of them pass the value of 10 to a method named func(). The signature for this function in the different languages might be written:

C

void func(int p)

Visual Basic

public sub func(ByVal p as Integer)

C#, Java

public void func(int p)

The lvalue-rvalue diagram is always the same, regardless of the language, so we can represent all three language variations in Figure 6, which shows how variable i looks in the calling routine.

Figure 6: Lvalue and rvalue for Variable i

Now simply tell the students that the 4-byte value 10 is copied to a memory location on a special segment of memory called a stack (you can explain what the stack is if you wish) and, once inside the function, those 4 bytes are popped off the stack into the rvalue of a variable named p. Varaiable p is a temporary variable and might look like Figure 7.

Figure 7: Lvalue and rvalue for Variable p

(The temporary variable p has a relatively large lvalue because temporary variables are allocated on the stack, which tends to be located in high memory.) The important thing to notice is that p is a defined variable that was just assigned the value that was passed to it from variable i. Stated differently, the rvalue of i has been copied into the rvalue of p.

Notice that the lvalues for i and p are very different, but the rvalues are now the same. This is what is meant by "pass by value". Pass by value means that the rvalue of a variable in one part of the program is passed to a method (or function) located at a different part of the program. However, since the lvalues for the two variables are different, anything done to p in the method has absolutely no direct impact on variable i. This also means that you cannot contaminate the contents of i accidentally by things you do to pi and p are located at entirely different places in memory and, hence, what we place into one bucket has no affect on the other bucket. (Refer to the lvalues in Figures 6 and 7.)

Pass by Reference

The student is now ready to understand what pass by reference means. In Table 8, we reproduce part of the lines from Table 7 that might be affected, plus their associated method signatures.

Table 8: Implementing Pass by Reference

The only difference in the calling routines is for the C language. In that case, we must preface the variable with the ampersand (&, or address-of) operator. This tells the compiler to place the lvalue on the stack, rather than the rvalue as was the case with pass by value.

Now look at the signatures for all three methods in Table 8. For the C language, the *p tells the compiler that the function named func() is being passed a pointer to a piece of data. A pointer is nothing more than the lvalue for the data item. For Visual Basic, the ByRef keyword says the same thing to the compiler: "Send me the lvalue of the data item, not its rvalue." For C#, the ref keyword sends the same message to the compiler: "Give me an lvalue, not an rvalue". As a result, the lvalue for the temporary variable p in Figure 8.

Figure 8: Lvalue and rvalue for Variable p

Notice that the lvalue of pi in Figure 6. In other words, the bucket for i and the bucket for p are exactly the same bucket. Variable p, therefore, is nothing more than an alias for variable ip in the func() method permanently affects the value of i as defined somewhere in another part of the program. (A good way to prove this to the students is to single-step the program with a watch window that observes the values of i and pp in the method, which simultaneously changes i in the calling code.) We often follow this discussion with the impact of pass by reference on data encapsulation.

Conclusion

Regardless of the language you use, the distinction between declare and define are important and real. These distinctions become clearer when you use the symbol table and lvalue-rvalue concepts to explain how the two terms are different. Further, the Bucket Analogy can be used to explain concepts that are often difficult for beginning students to understand, such as casting, value versus reference types, and pass-by-value versus pass-by-reference. A side benefit is that students are more adept at using and understanding the information made available by a debugger.

Previous Page | 1 The Nature of the Problem | 2 Data Definitions | 3 The Bucket Analogy | 4 Using lvalues and rvalues to Explain Argument Passing
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK