Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon, School of Computing SOFT 120Page 1 5. Passing Parameters by Reference.

Similar presentations


Presentation on theme: "Mark Dixon, School of Computing SOFT 120Page 1 5. Passing Parameters by Reference."— Presentation transcript:

1 Mark Dixon, School of Computing SOFT 120Page 1 5. Passing Parameters by Reference

2 Mark Dixon, School of Computing SOFT 120Page 2 Variables and Memory Addresses The computer keeps trace of where variables are stored in memory by using memory addresses. Every byte (position) in memory has a memory address: In the above example the variable identified by the name x is stored at location (this is the address of the first byte of data allocated to the variable x) 23xInteger Identifier Value Type Memory

3 Mark Dixon, School of Computing SOFT 120Page 3 Parameter Passing Methods There are 2 ways to pass parameters to functions and procedures: –Passing by Value: a literal value is passed from the call to the definition (you have already used this) procedure p1(x: integer); begin … end; –Passing by Reference: a variable’s memory address (a reference to the variables position in memory) is passed from the call to the definition procedure p2(var y: integer); begin … end;

4 Mark Dixon, School of Computing SOFT 120Page 4 Why pass by reference? It allows the value of the passed variable to be changed –i.e. it allows functions and procedures to change the value of things passed to them Normally parameters are for input data – only functions can output data via the return value Pass by reference allows data to be input and output via parameters

5 Mark Dixon, School of Computing SOFT 120Page 5 Example: Change the Value procedure P1(x: integer); begin x := x * 2; end; procedure P2(var x: integer); begin x := x * 2; end; var a, b: integer; a := 11; b := 12; P1(a);// What is the value of a? P2(b);// What is the value of b?

6 Mark Dixon, School of Computing SOFT 120Page 6 What can be passed Pass by value – both literals and variables can be passed (variables are substituted by their value) p1(y); // This is fine. p1(21); // This is fine. Pass by reference – only variables can be passed (in fact the variables memory address is passed) literals cannot be passed – they have no memory address p2(y); // This is fine. p2(21); // This generates an error. 

7 Mark Dixon, School of Computing SOFT 120Page 7 Example: Pass by Ref vs. Function procedure P2(var x: integer); begin x := x * 2; end; function F2(x: integer): integer; begin Result := x * 2; end; var b: integer; b := 4; P2(b);// What is the value of b? b := 4; b := F2(b);// What is the value of b? P1 var x: integer F1 x: integerinteger

8 Mark Dixon, School of Computing SOFT 120Page 8 Pass by Ref vs. Function a procedure that changes the value of a single parameter is equivalent to a function, –the procedure P2: P2(b); was equivalent to: –the function F2: b := F2(b); However, –F2 is far more explicit, –P2 is a bit cryptic: not obvious that value of b changes this makes code difficult to read, which can lead to errors

9 Mark Dixon, School of Computing SOFT 120Page 9 Example: Total var Nums: array[1..5] of integer; var tot: integer; function Total(): integer; var tmpTot: integer; var i: integer; begin tmpTot := 0; for i := 1 to 5 do tmpTot := tmpTot + Nums[i]; Result := tmpTot; end; Nums[1] := 23; Nums[2] := 17; Nums[3] := 28; Nums[4] := 12; Nums[5] := 25; tot := Total();// What is the value of tot?

10 Mark Dixon, School of Computing SOFT 120Page 10 Example: Average var ave: double; function Average(): double; var tmpTot: integer; var i: integer; begin tmpTot := 0; for i := 1 to 5 do tmpTot := tmpTot + Nums[i]; Result := tmpTot / 5; end; ave := Average();// What is the value of ave?

11 Mark Dixon, School of Computing SOFT 120Page 11 Two results? Total and Average functions share a lot of code Useful to combine them Problem: –a function can only have 1 output –This: is not possible (in Delphi anyway) double (average) integer (total) TotAve

12 Mark Dixon, School of Computing SOFT 120Page 12 Example: Total and Average procedure TotAve(var T: integer; var A: double); var i: integer; begin T := 0; for i := 1 to 5 do begin T := T + Nums[i]; end; A := T / 5; end; tot := 12; ave := 15; TotAve(tot, ave); // What is the value of ave and tot? TotAve var T: integer var A: double


Download ppt "Mark Dixon, School of Computing SOFT 120Page 1 5. Passing Parameters by Reference."

Similar presentations


Ads by Google