  |   
		  
		  
      
      
	  
      
	  
        
        
          
            
              
              
                
				 
				
                Chapter 1  
				  Introduction to DOS 
  
				  Chapter 2  
				  Introduction to Turbo Pascal
  
				  Chapter 3  
				  Parts of a Pascal Program
  
				  Chapter 4  
				   Control Structures and Looping
  
				  Chapter 5  
				  Looping
  
				  Chapter 6  
				  Procedures
  
				  Chapter 7  
				  Parameters Passing
  
				  Chapter 8  
				  Functions
  
				  Chapter 9  
				  Arrays
  
				  Chapter 10  
				  Searching and Sorting
  
				  Chapter 11  
				  Records and File of Records 
				  
                  | 
				 
				   
                  
				  CHAPTER 7
  
				  ( Parameter Passing )   
                  
				   
				   
                  All of us have to prepare income tax returns. 
                  It would be nice if we all had accounting background. But, 
                  that is not possible. Instead, we give yearly records to 
                  accountants or CPA's. Since CPA's are specialized in income 
                  tax accounting, several clients would submit records to them. 
                  CPA's in turn will complete income tax forms and return them 
                  to their clients. Let me emphasize this point: you pass some 
                  things to the CPA and you get some things back. The records 
                  you need to pass to the CPA can be transmitted two ways, (1) 
                  give all original documents, or (2) give copies of the 
                  documents. If copies are given, we don't have to be concerned 
                  with the CPA loosing documents, or writing notes on the 
                  documents, because we have the original documents with us. 
                  Think of this scenario, you give the CPA copies of all 
                  necessary documents, and give the original tax forms the IRS 
                  sent you. The CPA can fill out the original tax forms and 
                  return them to you.  
                  This is the same concept behind procedures 
                  and functions in Pascal. A procedure is supposed to perform a 
                  specialized function, and it can be called by different parts 
                  of a program. The calling program may pass some parameters to 
                  the procedure and the procedure may return certain parameters 
                  back. It is essential to understand how this is done. In 
                  Chapter 6 you were introduced to the concept of passing a 
                  parameter to the procedure. In Program 6-2 we passed 
                  temperature to the procedure. Here is what happens: a copy of 
                  the value of the variable 'Temperature' is placed in the 
                  receiving variable. Since only a copy was passed the value of 
                  temperature remains unchanged. Now let us change this program 
                  so that the calling program will receive the result back. 
                  In order to receive anything back from the 
                  procedure, we need to have a variable that can be accessed and 
                  changed by both the calling program and the procedure. What we 
                  need to do in this situation is to inform the procedure of the 
                  address of the memory location that will be used to do this. 
                  This sounds very complicated, but it really is not. All we 
                  have to do is to use the reserved word VAR in front of the 
                  formal parameter list. 
                   
				   |    
            PROGRAM 7-1 
            
              
                | 
                     
                  Program Convert (input, output); 
                  var 
                  temperature, result : real;
                    
                  whichOne : char;  
                  
                  (*******************************************************) 
                  Procedure ConvertToCelsius(Fahrenheit:real; var result:real); 
                  begin 
                  result := (Fahrenheit-32)*5/9;
                    
                   end;
                  
                  (******************************************************) 
                  Procedure ConvertToFahren(Celsius:real; var result:real); 
                  begin 
                  result := Celsius*9/5+32;
                    
                   end;
                  
                  (******************************************************) 
                  begin {main Program} 
                  write('This program will convert 
                    Fahrenheit');
                    
                  writeln(' to Celsius or Celsius to Fahrenheit '); 
                  writeln; 
                  write('Convert Celsius or Fahrenheit (C/F) '); 
                  readln (whichOne); 
                  writeln; 
                  write('Enter temperature ');readln(temperature); 
                  Case whichOne of 
                  'C','c': begin
                      
                  ConvertToFahren(temperature, result);  
                  write(temperature:7:2, ' degrees Celsius is equal to'); 
                  writeln(result:7:2, ' degrees of Fahrenheit.'); 
                   end;
                    
                  'F','f': begin 
                  ConvertToCelsius(temperature,result);
                      
                  write(temperature:7:2, ' degrees Fahrenheit is equal to 
                  '); 
                  writeln(result:7:2, ' degrees of Celsius.'); 
                   end;
                    
                  else writeln('You did not enter C or F as instructed!'); 
                     end; {case}
                  
                  
                   end.
                   |   
				  
            Program run: 
            This program will convert Fahrenheit to Celsius or 
            Celsius to Fahrenheit  
            Convert Celsius or Fahrenheit (C/F) F  
            Enter temperature 97  
            97.00 degrees Fahrenheit is equal to 36.11 degrees 
            of Celsius.  
              
            This program will convert Fahrenheit to Celsius or 
            Celsius to Fahrenheit  
            Convert Celsius or Fahrenheit (C/F) C  
            Enter temperature 37  
            37.00 degrees Celsius is 
              equal to 98.60 degrees of Fahrenheit.          
              
            
			
              
                      
            I have tried to demonstrate sharing of the common memory location 
            called 'result'. In this program we happened to call this memory 
            location 'result' in the procedures as well. Inside the procedures 
            we could have called it anything we wanted. It would still share the 
            same memory location. Whatever is placed in this memory location by 
            the procedures would be accessible to the main program as well.  
            This is how we are able to pass data between the calling
			module and the procedures. Passing 
            parameters like this it is referred to as 'passing parameters by 
            reference'. Recall that temperature was passed to either Fahrenheit 
            or Celsius depending on which procedure was called. Since only a 
            copy of the value of the variable 'temperature' was passed to the 
            receiving variable, such a passing is referred to as 'passing 
            parameters by value'. 
            At this point you might question, why don't we just 
            use global and local parameters to handle this instead of VAR and 
            value parameters? While it is possible to do this if you are writing 
            a small program, you certainly can't keep track of all the variable 
            names properly. A large commercial program is written by many 
            programmers and there is no way to keep track of the names. A 
            programmer writing a procedure may use any variable names he/she 
            wishes as long as the position in the parameter list is kept the 
            same and the types are compatible. 
            You might also ask another question, why don't we 
            use VAR parameters all the time? Since these parameters are passed 
            by reference, any changes that is made is also changed in the 
            calling program. If that is not what you intended, the program will 
            yield wrong results. This phenomenon is called a side effect. To 
            avoid unwanted side effects you should use value parameters and 
            local variables where appropriate. However, there is one 
            disadvantage to this. If the computer has limited main memory, it 
            may not be possible to make copies of variables.  
            EXPLANATION OF THE 
            PROGRAM      
            Let us look at the main block of the program first. 
            The write statements display the purpose of the program. Then it 
            asks if you wish to convert Celsius or Fahrenheit. The response is 
            stored as a character in the variable "whichOne". If the response is 
            "F" or "f" then convertToCelsius procedure will be called. If the 
            response is "C" or "c" then convertToFahren will be called. The 
            program needs one more input, the value for "temperature". Suppose 
            you entered F and 212 as input for these two variables. 
            Procedure calling is done by writing the name of 
            the procedure followed by the actual parameters in parentheses. 
            There are two actual parameters in these procedure calls, 
            "temperature" and "result", which contain 212 and garbage 
            respectively. The reason I called it garbage is that nothing has 
            been placed in "result" yet. There is some value in that memory 
            location, we just don't know what it is. The procedure will place 
            the result of the calculation in it.  
            Let us now look at the procedure ConvertToCelsius. 
            There are two formal parameters here, one is a value parameter, and 
            the other is a VAR parameter. The first value of the actual 
            parameter is passed to the first formal parameter. Since it is a 
            value parameter, a new local memory location is created called 
            Fahrenheit and 212 is placed in it. Keep in mind that the value of 
            the original memory location has not changed, it still has 212. The 
            second formal parameter is passed by reference, which means that the 
            procedure now shares the original memory location. Remember it still 
            has garbage. The result of the calculation is 100 and is assigned to 
            this memory location. The procedure now has a value of 212 for 
            Fahrenheit and 100 for result. 
            Upon returning from the procedure, the program now 
            prints out the result which has the value 100.  
            ASSIGNMENTS FOR CHAPTER 7 1 )  
			What would be the output of Program?  
           		    2 )  identify all local and global variables in all 
                         the procedures of Program 7-2. 
            		3 )  Make a block diagram of Program 7-2.  
            		4 ) Identify value and var parameters in Program 7-2? 
           
            PROGRAM 7-2 
            
            
              
              
                | 
                   Program VarValueExample 
                  (output); 
                  {This program illustrates the concept of 
                  value parameters and VAR parameters} 
                  var 
                  
                  
                  (***********************************************) 
                  Procedure ProcedureOne(var X:integer); 
                  var 
                  
                  
                  (*---------------------------------------------*) 
                  Procedure ProcedureThree(var Z:integer); 
                  var 
                  
                  
                  begin {ProcedureThree} 
                  A := 100;
                    
                  Z := A+B; 
                  write ('ProcdureThree, Values of A, B, and Z: '); 
                  writeln(A:5, B:5, Z:5);  
                  
                  end; {ProcedureThree} 
                  (*--------------------------------------------*) 
                  begin {ProcedureOne} 
                  A := 40; B := 50;
                    
                  X := C+A; 
                  write ('ProcedureOne, Values of A,B,C and X:'); 
                  writeln (A:5, B:5, C:5, X:5); 
                  ProcedureThree(X); {calling ProcedureThree} 
                     end; {ProcedureOne}
                  
                  (**********************************************) 
                  Procedure ProcedureTwo(Y :integer); 
                  var 
                  
                  
                  begin {ProcedureTwo} 
                  X := Y + B;
                    
                  write('ProcedureTwo, Values of Y,X, and B: '); 
                  writeln(Y:5, X:5, B:5); 
                     end; {ProcedureTwo}
                  
                  begin {main program} 
                  A := 10; B := 20; C:= 30;
                    
                  write ('Main program, Values of A,B,C : '); 
                  writeln(A:5, B:5, C:5); 
                  procedureOne(C); {procedure calls} 
                  ProcedureTwo(A); 
                  writeln ('Main program, after procedure calls.'); 
                  write ('Values of A,B,C: ', A:5, B:5, C:5); 
                   end. {main}
                   |   
				  
           
            Program run: 
            Main program, Values of A,B,C : 10 20 30 
              
            ProcedureOne, Values of A,B,C and X: 40 50 70 70 
             
            ProcdureThree, Values of A, B, and Z: 100 50 150 
             
            ProcedureTwo, Values of Y,X, and B: 10 30 20  
            Main program, after 
              procedure calls.   
            
            Values of A,B,C: 10 20 150 Type EXIT to return to 
            Turbo Pascal...  
            Microsoft(R) MS-DOS(R) Version 5.00  
            (C)Copyright Microsoft Corp 1981-1991. 
            
					 
			
			Go to top of this chapter
			       
       
                     |  
			          |