note_05 - Chapter 5 Modularity, Functions, and Data Flow...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
73 Chapter 5 Modularity, Functions, and Data Flow Local Variables : The SCOPE of a valuable is determined by where the variable is created . A Local variable is one that is declared within a procedure . It is known and can be referenced ONLY within that procedure . Global Variables : Declared Outside Any Procedure Its scope would be over the whole program . This scope can be limited if any LOCAL VARIABLES use the same name.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
74 Program sample; Var A,B, Temp : integer; Procedure Exchange ( Var X, Y: integer); Var Temp : integer; Begin Temp := X; X := Y; Y := Temp ; End; Begin A := 1; B := 2; Temp := 3; OUTPUT Exchange(A,B); writeln(A:2, B:2, Temp :2) 2 1 3 End.
Background image of page 2
75 Procedures & Global Variables : •A procedure should be a self-contained unit . The procedures interaction with the rest of the program should be entirely through its formal parameters. A procedure should only Reference its own formal parameters and its own local declarations . • If a procedure needs access to the data in a global variable, that variable s/b passed as an actual parameter to a formal parameter in that procedure. • Side Effect – A violation of the above rules. •( See pitfall pg. 167 .)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
76 Program varDemo (input, output); {Program to demonstrate local and global variables.} var x , y : real; Procedure P( a:real); Var x : real; begin x := a * a; writeln ( x :6:1); y :=a + a: writeln(y:6:1) end; {Procedure P} begin x := 2.0; y := 10.0; p(y); writeln ( x :6:1); writeln (y:6:1) end. {VarDemo}
Background image of page 4
77 Program Example Program careless ; {Illustrates the effect of omitting a var from the parameter list of the procedure ComputePay of Figure 5.5.} const Rate = 30; {Cents per minute.} MoneyLength = 7; {Field width for PayDue.} var TimeWorked: integer; PayDue: real; Procedure ComputePay (Minutes: integer; Pay: Real); {Precondition: Minutes is the time worked in minutes. Postcondition:Pay is set to the pay due expressed in dollars.} var PennyPay: integer; begin PennyPay :=Rate * Minutes; Pay :=PennyPay/100; writeln (‘You should have earned: $ ‘, Pay:MoneyLength:2); writeln (‘But watch what happens when we leave the procedure.’); end;
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
78 Begin {Program} TimeWorked := 62; PayDue := 0; ComputePay(TimeWorked, PayDue); writeln (‘The procedure call is over.’); writeln(‘At’, Rate, ‘ cents per minute, ‘); writeln (‘you earned: $’, PayDue:MoneyLength: 2); End. {Program} OUTPUT You should have earned: $ 18.60 But watch what happens when we leave the procedure.
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 28

note_05 - Chapter 5 Modularity, Functions, and Data Flow...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online