|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
7-3 Global and Local VariablesBoth the formal parameters and the variables declared in a procedure are called local variables because they are accessible only within their procedure; in other words, they are invisible to the main program or to any other subprogram. The variables declared in the main program, on the other hand, are called global variables because they are accessible from any program unit. In the program 7-2, for example, the variable TestSentence is a global variable and may be accessed from the procedure DrawLine without passing it as a parameter. Any assignment to this variable in the procedure will change its value in the main program. The local variable Counter, however, is not accessible from the main program. Consider now the case if you declared two variables with the same name (such as X), one in the main program and one in a procedure. The redeclaration of the global variable X in a procedure will create a local variable with the same name and hide the global variable from the procedure. This means you will have two different variables that correspond to two different locations in memory. When the procedure exits, there will be one global variable with the name X. These restrictions help the programmer not to modify the value of a global variable from a subprogram by accident. The variables in the main program can only be modified from other procedures if they are global (and not redeclared in the procedure) or are passed by reference as variable parameters to the procedure. Accessing global variables from a subprogram is not recommended, as it repeals the modularity of the program. Using parameters is safer, and it also keeps the subprogram independent and useful with different programs. Example: Sorting ProcedureGo back to program 5-5, and let us split it into generic procedures. This program was used to read, sort, and display an array of six elements. What you need to do now is write three procedures that read, sort, and display an array of any size. By passing the array and the number of elements to the procedures, the same results will be achieved as before. The main body of the program will contain only three calls: ReadNumbers(ArraySize, Numbers); SortNumbers(ArraySize, Numbers); PrintNumbers(ArraySize, Numbers); In this way, any one of the three procedures can be used in any program. One important point to mention here is that when you pass an array to a procedure or function, it must be declared in the TYPE section. The formal parameters in the procedure header will then look something like this: PROCEDURE ReadNumbers(L: INTEGER; VAR R:NumbersArray); The parameter L corresponds to ArraySize, and the array R corresponds to the array Numbers. As you can see in the parameter declaration it is of the type NumbersArray, which is the same type as the array Numbers. Here is the complete program: { ----------------------- figure 7-4 --------------------------- } PROGRAM Sorting(INPUT,OUTPUT); CONST ArraySize = 6; TYPE Range = 1..ArraySize; NumbersArray = ARRAY[Range] OF INTEGER; VAR Numbers:NumbersArray; { ----------------- Read procedure --------------- } PROCEDURE ReadNumbers(L: INTEGER; VAR R:NumbersArray); VAR I:INTEGER; BEGIN FOR I:= 1 TO L DO BEGIN WRITE('Enter element #', I,': '); READLN(R[I]) END END; { ----------------- Sort procedure --------------- } PROCEDURE SortNumbers(M: INTEGER; VAR S:NumbersArray); VAR I, J, Pot:INTEGER; BEGIN FOR I:= 1 TO M-1 DO FOR J:= I+1 TO M DO IF S[I] > S[J] THEN BEGIN { swap contents } Pot:= S[J]; S[J]:= S[I]; S[I]:= Pot END END; { ---------------- Print procedure --------------- } PROCEDURE PrintNumbers(N: INTEGER; T:NumbersArray); VAR I:INTEGER BEGIN WRITELN; WRITE('The sorted array is: '); FOR I:= 1 TO N DO WRITE(T[I],' '); WRITELN; END; { --------------- Main Program ------------------- } BEGIN ReadNumbers(ArraySize, Numbers); SortNumbers(ArraySize, Numbers); PrintNumbers(ArraySize, Numbers); WRITELN('Press ENTER to continue..'); READLN END. Sample run: Enter element #1: 44 Enter element #2: 22 Enter element #3: 8 Enter element #4: 1 Enter element #5: 667 Enter element #6: 3 The sorted array is: 1 3 8 22 44 667 Press ENTER to continue.. Note that the array is passed as a variable parameter to the procedures which are expected to change the value of the array (e.g., ReadNumbers and SortNumbers), but there was no need to do that for the procedure PrintNumbers, which displays the array without returning any value to the main program. In the latter case the array was passed as a value parameter. Notice also the use of local variables in different procedures, which makes each an independent unit. If any of these procedures has to be used with a different type of array, you need only change the type NumbersArray or use the same type name for the new array in the main program. In this example it is possible to use procedures without any parameters at all and process the global variables directly, but in that case you would have to use the same variable names in all of the procedures and the main program, which is a lot of effort and also entails the risk of dealing with global variables.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |