Upcoming SlideShare
×

# computer notes - Reference variables

421
-1

Published on

The “&” indicates a parameter that is a reference variable. Because it is the value that is passed. We can always pass a literal or even an expression in call-by-value computer notes

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
421
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

### computer notes - Reference variables

1. 1. ecomputernotes.com Data Structures Lecture No. 17___________________________________________________________________Data StructuresLecture No. 17After demonstrating the use of const and reference variables in the interface class for abinary search tree in the previous lectures, we will now see what these referencevariables are and how does internal process go on during the use of reference variables.Reference VariablesBefore proceeding ahead, there is need to know why the reference variables are used;and what benefits a programmer can get while employing them. We will discuss theseall things in detail with the help of examples.The symbol &, used for reference variable has a few different purposes with respect toits occurrence in the code. In C++ programming, we have seen that when theampersand sign i.e. & appears in front of a variable name, it is the address operator. Itreturns the address of the variable in front of which it is written. Thus for example, if xis a variable name, then &x ; will return the address of the variable x. In general we cansay that &variablename ;will return the address of the variable. We know that an address can be stored in apointer. To further understand this concept, let s suppose that there are following linesin our code. int x ; int* ptr = &x;The first line declares a variable x of type int while the second one declares a pointer toint and assigns it the address of x. This address of variable x is returned by the & signwritten in front of the variable x. Thus a pointer variable is initialized by assigning it theaddress of a variable. This address of the variable is gotten by using the & sign with thevariable name.The other place where & sign can appear is the signature of the function where itappears after the type of the parameter. Consider the insert and remove methods fromBinarySearchTree class that were declared as the following. void insert( const EType& x ); void remove( const EType& x );Notice that the & sign is after the type. Here we define the class and functions astemplates. Whenever, we use these methods, the type EType will be replaced with aproper data type.Suppose that we have designed the BinarySearchTree class to hold the integers only.This means that there are no templates and the class can be defined only for integers. Page 1 of 3
2. 2. ecomputernotes.com Data Structures Lecture No. 17___________________________________________________________________So the insert and remove methods of this class will be as follows. void insert( const int& x ); void remove( const int& x );Here, in the function signature, & sign after the type of the parameter indicates that theparameter is a reference variable. We can use this & sign with any data type i.e. built-in or user defined, of an argument to show that this is a reference variable. This is thesyntax for the reference variable. Now let s see why and how we use it and what is itsadvantage? Let s look at an example that will explain the reference variable. Supposewe have the following three different functions.The name of the first function is intMinus1, written as under.//Function 1int intMinus1( int oldVal){ oldVal = oldVal 1; return oldVal;}This function takes an integer as an argument. The local name of this argument isoldVal. The first statement in the body of the function decreases the value of oldVal by1 then the next statement returns this value to the calling function.The second function is intMinus2. This function takes a pointer to integer as anargument. In the statement *oldVal = *oldVal 2 ;*oldVal means the value at the location where oldVal is pointing to. To get the valuethis way is called dereferencing. After experiencing a decrease by 2, this value remainsat the same location i.e. where oldVal is pointing to. Then the pointer to integer isreturned. This function is defined as follows.// Function 2int intMinus2( int* oldVal){ *oldVal = *oldVal 2; return *oldVal;}The third function intMinus3 takes a reference variable as an argument. Its definition isas under.// Function 3 Page 2 of 3
3. 3. ecomputernotes.com Data Structures Lecture No. 17___________________________________________________________________int intMinus3( int& oldVal){ oldVal = oldVal 3; return oldVal;}The & sign after the type in the signature (declaration line) indicates that this argumentis a reference variable. Notice that & sign is used only in the function declaration,leaving no need for its use in the body of the function. The statements in the body ofthe function decrease the vale of oldVal by 3 and return it to the calling function.We can see that these three functions have the same return type and their bodyperforms the task of decreasing the passed value. However it is clear from thedeclaration of these functions that the way of passing argument to these functions isdifferent. We will now see what changes occur while passing the argument in differentways. We will write calling functions those call the above three functions one by one.We will see how these calls are made and what differences happen during the processof passing same value (in different ways) to these functions. The use of call stack willhelp in ascertaining what happens to these local variables and parameters.Following is the function that calls the first function i.e. intMinus1.void caller(){ int myInt = 31; int retVal; retVal = intMinus1( myInt ); cout << myInt << retVal;}In this function (caller), we declare an integer variable myInt and assign it a value 31.We also declare a variable retVal of type int. In the statement retVal = intMinus1( myInt );We call the function intMinus1 by passing it the variable myInt (the value of which is31) and assign the return value of this function call to retVal. In the next statement, weprint the value of these two variables.While talking about runtime environment, we noticed that an executable program whilein run, is loaded in the memory. It later becomes a process. This process is given ablock of memory, which it uses during its execution. Suppose we are running manyprograms simultaneously i.e. browser, MS Word, Excel and dev-C++. We can also runprograms written by us. The figure 17.1 shows that process4 is a program written byus. Every program, which we run takes a block of memory and becomes a process.The memory occupied by a process is further partitioned into different parts. The firstpart of the memory is for the code of the program. This code is in binary form i.e. it isthe compiled code. Afterwards, there is some area of memory for static data that holdsstatic and global variables. Then in the memory, there becomes the stack. This stack isused in function calls. At the end, there is some area, called heap. When we allocate Page 3 of 3
4. 4. ecomputernotes.com Data Structures Lecture No. 17___________________________________________________________________memory dynamically by using new operator, this memory is allocated from the heap.The following figure shows the internal memory organization of a program(ourtest.exe). Process 1 Code (browser) Process 3 Static data (Word) Stack Process 4 (ourtest.exe) Process 2 (Dev-C++) Windows Os Heap Fig 17.1: Internal memory organization of a process (ourtest.exe)We have discussed in detail the call stack that is used in function calls. We know thatwhen a function call is made, it s all the arguments go on the stack. The return addressof the function also goes to the stack. The local variables are placed on the stack.When a function calls some other function, an activation record is made. It has muchinformation including all these things (parameters, return address etc). The detail ofactivation record relates to the compiler construction course. Here we are concernedwith only the call stack. Following figure shows the call stack layout. Parameters (caller) Local variables(caller) Return address (caller) Parameters (intMinus1) Local variables(intMinus1) Return address sp (intMinus1) stack grows downwards Fig 17.2: call stack layoutIn the stack layout figure, there are the entries of the caller in the upper portion. This isdue to the fact that the caller function itself was called (executed) by some one else. Page 4 of 3