1 Procedures Blocks of code which can be called from anywhere in a program Enable us to avoid...

23
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

Transcript of 1 Procedures Blocks of code which can be called from anywhere in a program Enable us to avoid...

Page 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.

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

Page 2: 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.

2

Simple Example - Anonymous

Declared in the implementation section,immediately after { $R *.DFM}

Declared in the implementation section,immediately after { $R *.DFM}

CallCall

Page 3: 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.

3

private Information; publicEnd;

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

CallCall

Preceeded by object namePreceeded by object name

Page 4: 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.

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

Page 5: 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.

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;

Page 6: 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.

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;

Page 7: 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.

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

Page 8: 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.

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!

Page 9: 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.

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

ActualParameters

Page 10: 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.

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 procedureprocedure MyProc(param1: SomeType);

var myVariable: AnotherType;begin // some codeend;

procedure MyProc(param1: SomeType);var myVariable: AnotherType;begin // some codeend;

Page 11: 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.

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)

Page 12: 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.

12

Exampleprocedure 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

Page 13: 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.

13

Result

Page 14: 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.

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

Page 15: 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.

15

Exampleprocedure 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;

Page 16: 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.

16

Result

Page 17: 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.

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

Page 18: 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.

18

Functions

Declaration and Use

Page 19: 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.

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• …

Page 20: 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.

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 expressionused in an expression

Page 21: 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.

21

Function Example

Page 22: 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.

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!

Page 23: 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.

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!)