Channels ▼
RSS

Design

Computer Programming and Precise Terminology


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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video