Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Procedures Blocks of code which can be called from anywhere in a program Enable us to avoid needless repetition of the same code Can take parameters.

Similar presentations


Presentation on theme: "1 Procedures Blocks of code which can be called from anywhere in a program Enable us to avoid needless repetition of the same code Can take parameters."— Presentation transcript:

1 1 Procedures Blocks of code which can be called from anywhere in a program Enable us to avoid needless repetition of the same code Can take parameters Ordinary (‘Value’) Parameters ‘Variable’ Parameters Procedures must be defined before they are used

2 2 Simple Example - Anonymous Declared in the implementation section, immediately after { $R *.DFM} Declared in the implementation section, immediately after { $R *.DFM} Call

3 3 private Information; public End; Implementation {$R *.DFM} Procedure TForm1.Information; Begin MessageDlg(‘Hello World!’, mtInformation, [mbOK], 0); End; Procedure TForm1GoBtnClick(Sender: Tobject); Begin Information; End; Simple Example - method Declared in the interface section, as either private or public Declared in the interface section, as either private or public Call Preceeded by object name

4 4 Making it more flexible The example procedure encapsulates an Information dialog, and might be particularly useful if we needed to call it several times in a program However, it’s not very useful Unless ‘Hello, World!’ is the only message we ever want to display! We can give it a parameter the message we want to display

5 5 Example (2) procedure Information(msg: String); begin MessageDlg(msg, mtInformation, [mbOK], 0); end; procedure Information(msg: String); begin MessageDlg(msg, mtInformation, [mbOK], 0); end; Why is the parameter not called message? It’s a KEYWORD (Reserved Word) We can’t use its name for a variable Such words are displayed by Delphi in bold How do we call it? procedure TForm1.GoBtnClick(Sender: TObject); begin Information('Your Message Here'); end; procedure TForm1.GoBtnClick(Sender: TObject); begin Information('Your Message Here'); end;

6 6 Parameters We wouldn’t expect to be limited to only one parameter And we’re not But we must declare the names and types of all parameters Procedure Declaration Syntax procedure name(param1, param2: Type1; param3: Type2); {optional local declarations} begin {statements go here} end; procedure name(param1, param2: Type1; param3: Type2); {optional local declarations} begin {statements go here} end;

7 7 Parameter Declarations Generally, parameters are declared by giving their name, then a colon (:) and then their type Individual parameters are separated by semicolons (;) If there are several adjacent parameters of the same type, we can save ourselves some typing by separating their names with commas, and only putting the colon followed by the type after the last of them

8 8 Calling a Procedure We call a procedure simply by using its name (see Information above) If there are parameters they must be listed between ( … ) following the name If more than one parameter (of whatever type) they are separated by commas Not semicolons!

9 9 Calling a Procedure procedure MyProc(param1: Integer; param2: String); begin ShowMessage(param2 + IntToStr(param1)); end; procedure MyProc(param1: Integer; param2: String); begin ShowMessage(param2 + IntToStr(param1)); end; MyProc(5, ‘Value’); Parameters are matched by position. The procedure gets a COPY of each value when it is called. Types of actual parameters must MATCH those of the formal parameters (normally, be the same type) Formal Parameters Actual Parameters

10 10 Local Declarations We can declare variables locally in procedures using the var statement, in the way we’ve already seen The parameters are automatically declared — we should NOT declare these locally ourselves Variables declared locally inside a procedure are visible only inside that procedure procedure MyProc(param1: SomeType); var myVariable: AnotherType; begin // some code end; procedure MyProc(param1: SomeType); var myVariable: AnotherType; begin // some code end;

11 11 Variable Parameters Ordinarily, a procedure gets a COPY of the values passed to it Any changes we make to the value inside the procedure only apply to the copy NOT to the original variable (if one was used)

12 12 Example procedure TestParams(x, y: Integer; log: TMemo); begin log.Lines.Add(Format('Entering procedure: x = %d y = %d', [x,y])); x := x + 10; y := y * 3; log.Lines.Add(Format('Leaving procedure : x = %d y = %d', [x,y])); end; procedure TForm1.GoBtnClick(Sender: TObject); var x, y : Integer; begin x := 5; y := 9; Memo1.Lines.Add(Format('Calling procedure : x = %d y = %d', [x,y])); TestParams(x, y, Memo1); Memo1.Lines.Add(Format('After procedure : x = %d y = %d', [x,y])); end; procedure TestParams(x, y: Integer; log: TMemo); begin log.Lines.Add(Format('Entering procedure: x = %d y = %d', [x,y])); x := x + 10; y := y * 3; log.Lines.Add(Format('Leaving procedure : x = %d y = %d', [x,y])); end; procedure TForm1.GoBtnClick(Sender: TObject); var x, y : Integer; begin x := 5; y := 9; Memo1.Lines.Add(Format('Calling procedure : x = %d y = %d', [x,y])); TestParams(x, y, Memo1); Memo1.Lines.Add(Format('After procedure : x = %d y = %d', [x,y])); end; The procedure can’t get at the memo directly so we must pass it as a parameter

13 13 Result

14 14 Variable Parameters Often we WANT a change to a parameter to affect the program outside of the procedure ‘Variable Parameters’ — often called ‘var parameters’ — let us do this But do not overuse

15 15 Example procedure TestParams(var x, y: Integer; log: TMemo); begin log.Lines.Add(Format('Entering procedure: x = %d y = %d', [x,y])); x := x + 10; y := y * 3; log.Lines.Add(Format('Leaving procedure : x = %d y = %d', [x,y])); end; procedure TForm1.GoBtnClick(Sender: TObject); var x, y : Integer; begin x := 5; y := 9; Memo1.Lines.Add(Format('Calling procedure : x = %d y = %d', [x,y])); TestParams(x, y, Memo1); Memo1.Lines.Add(Format('After procedure : x = %d y = %d', [x,y])); end; procedure TestParams(var x, y: Integer; log: TMemo); begin log.Lines.Add(Format('Entering procedure: x = %d y = %d', [x,y])); x := x + 10; y := y * 3; log.Lines.Add(Format('Leaving procedure : x = %d y = %d', [x,y])); end; procedure TForm1.GoBtnClick(Sender: TObject); var x, y : Integer; begin x := 5; y := 9; Memo1.Lines.Add(Format('Calling procedure : x = %d y = %d', [x,y])); TestParams(x, y, Memo1); Memo1.Lines.Add(Format('After procedure : x = %d y = %d', [x,y])); end;

16 16 Result

17 17 Important Note We said earlier that the types of the formal and actual parameters must MATCH For ordinary (‘value’) parameters, this means that the actual parameter must be: a variable of the same type an expression of a matching type a value of a matching type In practice, ‘matching type’ means either the same type or an Integer if the formal parameter is of type Real For variable (‘var’) parameters, the actual parameter MUST BE A VARIABLE OF THE SAME TYPE

18 18 Functions Declaration and Use

19 19 Functions A function, like a procedure, is a block of code The difference is that it returns a value to where it is called from Format (see above) is a function So are mathematical functions: Sin Cos Log …

20 20 Function Example function Average(data: TStrings): Real; var i: Integer; sum, count: Integer; begin sum := 0; count := 0; for i := 0 to data.Count -1 do begin sum := sum + StrToInt(data[i]); count := count + 1; end; result := sum/count; end; procedure TForm1.AvgBtnClick(Sender: TObject); var res : Real; begin res := Average(Memo1.Lines); ResultLbl.Caption := FloatToStr(res); end; function Average(data: TStrings): Real; var i: Integer; sum, count: Integer; begin sum := 0; count := 0; for i := 0 to data.Count -1 do begin sum := sum + StrToInt(data[i]); count := count + 1; end; result := sum/count; end; procedure TForm1.AvgBtnClick(Sender: TObject); var res : Real; begin res := Average(Memo1.Lines); ResultLbl.Caption := FloatToStr(res); end; used in an expression

21 21 Function Example

22 22 Notes We specify the RETURN TYPE of the function after the parameter list and following a colon (:) We use the word function in the declaration instead of procedure We should NOT write a function which uses var parameters The value which the function will return is assigned to the special variable result Notice that result is not shown in bold in Delphi => It isn’t reserved So need to take care not to declare a variable with this name!

23 23 Summary Procedures and functions useful for code which is needed repeatedly Save us typing Also make program easier to read Like making new commands Parameters Make procedures and functions more flexible Variable parameters Functions return a result Used in an expression Special variable, result (NOT reserved!)


Download ppt "1 Procedures Blocks of code which can be called from anywhere in a program Enable us to avoid needless repetition of the same code Can take parameters."

Similar presentations


Ads by Google