2 User Defined Procedures Scope and Parameters Mark

  • Slides: 14
Download presentation
2. User Defined Procedures: Scope, and Parameters. Mark Dixon, School of Computing SOFT 120

2. User Defined Procedures: Scope, and Parameters. Mark Dixon, School of Computing SOFT 120 Page 1

Scope: What is it? • Scope – accessibility/visibility – Local – Global/unit • Things

Scope: What is it? • Scope – accessibility/visibility – Local – Global/unit • Things that have scope: – Variables (covered previously) – Procedures (current focus) – Functions (subject of next lecture) Mark Dixon, School of Computing SOFT 120 Page 2

Variable Scope: Example • In the following: procedure Tfrm. Test. cmd. Calc. Click(Sender: TObject);

Variable Scope: Example • In the following: procedure Tfrm. Test. cmd. Calc. Click(Sender: TObject); var x: integer; begin x : = 0; lbl. Total. Caption : = '£' + Int. To. Str(x); end; procedure Tfrm. Test. cmd. Quit. Click(Sender: TObject); Begin Undeclared identifier x : = 0; lbl. Total. Caption : = '£' + Int. To. Str(x); end; Mark Dixon, School of Computing SOFT 120 error Page 3

Procedure Scope: Example implementation … procedure Draw. Face(); procedure Draw. Nose(); begin … end;

Procedure Scope: Example implementation … procedure Draw. Face(); procedure Draw. Nose(); begin … end; • Procedures within Procedures – Only available within parent procedure Draw. Mouth(); begin … end; procedure Draw. Eyes(); begin … end; • Calling from elsewhere begin … Draw. Nose; Draw. Mouth; Draw. Eyes; end; – Gives error procedure Tfrm. Face. chk. Nose. Click(Sender: TObject); begin Draw. Face; end; procedure Tfrm. Face. opt. Mood. Click(Sender: TObject); begin Draw. Nose; Draw. Face; end. Mark Dixon, School of Computing SOFT 120 Page 4

Scope: Why? • In short – Robustness of code/software • One of many responses

Scope: Why? • In short – Robustness of code/software • One of many responses to code that is – Difficult to maintain, and – Unreliable – House of cards phenomenon • Prevent: – Uncontrolled and ad hoc interactions between code • Always define things at lowest level needed Mark Dixon, School of Computing SOFT 120 Page 5

Till Example: User Interface Mark Dixon, School of Computing SOFT 120 Page 6

Till Example: User Interface Mark Dixon, School of Computing SOFT 120 Page 6

Till Example: Code implementation. . . var Sub. Total: double; var Total: double; procedure

Till Example: Code implementation. . . var Sub. Total: double; var Total: double; procedure Tfrm. Till. cmd. Equals. Click(Sender: TObject); var Price: double; var Quaty: double; begin Price : = Str. To. Float(txt. Price. Text); Quaty : = Str. To. Float(txt. Quantity. Text); Sub. Total : = Price * Quaty; lbl. Sub. Total. Caption : = '£' + Float. To. Str(Sub. Total); end; . . . end. Mark Dixon, School of Computing SOFT 120 Page 7

Parameters • Sometimes procedures need information – Making a cup of tea: • milk,

Parameters • Sometimes procedures need information – Making a cup of tea: • milk, and number of sugars Milk Sugars Mark Dixon, School of Computing Make cup of tea SOFT 120 Page 8

Procedure for Making Cup of Tea Making a cup of tea: 1. Fill the

Procedure for Making Cup of Tea Making a cup of tea: 1. Fill the kettle with water 2. Plug the kettle in 3. Switch the kettle on 4. Wait for the kettle to boil 5. Put a tea bag into the cup 6. Add sugar to the cup 7. Stir 8. Add milk to the cup 9. Stir 10. Take the tea bag out Mark Dixon, School of Computing SOFT 120 Page 9

Parameters: Example (v 1 part 1) implementation. . . var Sub. Total: double; var

Parameters: Example (v 1 part 1) implementation. . . var Sub. Total: double; var Total: double; procedure Tfrm. Till. cmd. Equals. Click(Sender: TObject); var Price: double; var Quaty: double; begin Price : = Str. To. Float(txt. Price. Text); Quaty : = Str. To. Float(txt. Quantity. Text); Sub. Total : = Price * Quaty; lbl. Sub. Total. Caption : = '£' + Float. To. Str(Sub. Total); end; procedure Tfrm. Till. cmd. Add. Click(Sender: TObject); begin Total : = Total + Sub. Total; lbl. Total. Caption : = '£' + Float. To. Str(Total); end; procedure Tfrm. Till. cmd. Undo. Click(Sender: TObject); begin Total : = Total - Sub. Total; lbl. Total. Caption : = '£' + Float. To. Str(Total); end; Mark Dixon, School of Computing SOFT 120 Page 10

Parameters: Example (v 1 part 2) procedure Tfrm. Till. cmd. Clear. Click(Sender: TObject); begin

Parameters: Example (v 1 part 2) procedure Tfrm. Till. cmd. Clear. Click(Sender: TObject); begin txt. Price. Text : = ''; txt. Quantity. Text : = ''; Sub. Total : = 0; lbl. Sub. Total. Caption : = '£' + Float. To. Str(Sub. Total); Total : = 0; lbl. Total. Caption : = '£' + Float. To. Str(Total); end; procedure Tfrm. Till. cmd. Quit. Click(Sender: TObject); begin Close; end. Mark Dixon, School of Computing SOFT 120 Page 11

Parameters: Example (v 2 part 1) implementation. . . var Sub. Total: double; var

Parameters: Example (v 2 part 1) implementation. . . var Sub. Total: double; var Total: double; procedure Set. Sub. Total(tmp. Sub. Total: double); begin Sub. Total : = tmp. Sub. Total; frm. Till. lbl. Sub. Total. Caption : = '£' + Float. To. Str(Sub. Total); end; procedure Set. Total(tmp. Total: double); begin Total : = tmp. Total; frm. Till. lbl. Total. Caption : = '£' + Float. To. Str(Total); end; procedure Tfrm. Till. Check. Controls(); begin if (txt. Price. Text = '') or (txt. Quantity. Text = '') then begin cmd. Equals. Enabled : = false; end else begin cmd. Equals. Enabled : = true; end; Mark Dixon, School of Computing SOFT 120 tmp. Sub. Total: double Set. Sub. Total Set. Total Check Controls Page 12

Parameters: Example (v 2 part 2) procedure Tfrm. Till. Form. Create(Sender: TObject); begin Check.

Parameters: Example (v 2 part 2) procedure Tfrm. Till. Form. Create(Sender: TObject); begin Check. Controls(); end; procedure Tfrm. Till. txt. Price. Change(Sender: TObject); begin Check. Controls(); end; procedure Tfrm. Till. txt. Quantity. Change(Sender: TObject); begin Check. Controls(); end; procedure Tfrm. Till. cmd. Equals. Click(Sender: TObject); var Price: double; var Quaty: double; begin Price : = Str. To. Float(txt. Price. Text); Quaty : = Str. To. Float(txt. Quantity. Text); Set. Sub. Total(Price * Quaty); txt. Price. Set. Focus(); end; procedure Tfrm. Till. cmd. Add. Click(Sender: TObject); begin Set. Total(Total + Sub. Total); txt. Price. Set. Focus(); end; Mark Dixon, School of Computing SOFT 120 Page 13

Parameters: Example (v 2 part 3) procedure Tfrm. Till. cmd. Add. Click(Sender: TObject); begin

Parameters: Example (v 2 part 3) procedure Tfrm. Till. cmd. Add. Click(Sender: TObject); begin Set. Total(Total + Sub. Total); txt. Price. Set. Focus(); end; procedure Tfrm. Till. cmd. Undo. Click(Sender: TObject); begin Set. Total(Total - Sub. Total); end; procedure Tfrm. Till. cmd. Clear. Click(Sender: TObject); begin txt. Price. Text : = ''; txt. Quantity. Text : = ''; Set. Sub. Total(0); Set. Total(0); txt. Price. Set. Focus(); end; procedure Tfrm. Till. cmd. Quit. Click(Sender: TObject); begin Close; end. Mark Dixon, School of Computing SOFT 120 Page 14