Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon, School of Computing SOFT 120Page 1 4. User Defined Functions (part 2)

Similar presentations


Presentation on theme: "Mark Dixon, School of Computing SOFT 120Page 1 4. User Defined Functions (part 2)"— Presentation transcript:

1 Mark Dixon, School of Computing SOFT 120Page 1 4. User Defined Functions (part 2)

2 Mark Dixon, School of Computing SOFT 120Page 2 Revision: Maths - Substitution Expression for y, in terms of m, x, and c: y = mx + c this is the formula of a straight line graph. Given values for m, x, and c m = 2, x = 3, c = 5 Possible to calculate y: y = mx + cfrom above y = (m * x) + ccomputing terms y = (2 * x) + csubstitute m with 2 y = (2 * 3) + csubstitute x with 3 y = (2 * 3) + 5substitute c with 5

3 Mark Dixon, School of Computing SOFT 120Page 3 Substitution Examples Calculate b given that: b = z + yf, z = 10, y=2, and f = 0.5 Calculate L given that: L = v + y + x + 2v, v = 4, y = 3, x = 2

4 Mark Dixon, School of Computing SOFT 120Page 4 Revision: Variables Variables – store data in computer’s memory Variables have –Identifier (name) – you choose this, used to refer to (reference) variable –Type – you choose this (to suit purpose) –Value – you set/change this 23xInteger Identifier Value Type Memory

5 Mark Dixon, School of Computing SOFT 120Page 5 Revision: Variables Variables are: –Declared, using the following syntax (grammar/form): var : ; for example: var weight: double; –Assigned values, using the following syntax: := ; for example: weight := 109.45; Note: the data flows backwards (from right to left)

6 Mark Dixon, School of Computing SOFT 120Page 6 Variable Declaration: Examples Write a line of code that: –Declares a variable called x of type double –Declares a variable called y of type integer –Declares a variable called surname of type string –Declares a variable called age of type integer

7 Mark Dixon, School of Computing SOFT 120Page 7 Variable Assignment: Examples Write a line of code that: –Assigns the value of 23 to the variable y –Assigns the value of 14.6 to the variable x –Assigns the value of ‘John’ to the variable surname –Assigns the value of 21 to the variable age

8 Mark Dixon, School of Computing SOFT 120Page 8 Variable Assignment: Examples Write a line of code that: –Increases the value of x by 2.89 –Increases the value of y by 43 –Decreases the value of age by 1 –Decreases the value of x by y

9 Mark Dixon, School of Computing SOFT 120Page 9 Functions Functions often used to hide detail of calculations For example, the following: m = k / 1.6 gives –expression for m in terms of k –expression for miles in terms of kilometres So, if we’ve walked 8 kilometres (k = 8) m = k / 1.6 m = 8 / 1.6 m = 5

10 Mark Dixon, School of Computing SOFT 120Page 10 Functions To convert from km to m in code, we could type: m := k / 1.6; Assuming: –m and k have been declared as variables earlier in the program. This would become monotonous, if the program did it many times Especially if the calculation were more complex To solve this we can define and use a function

11 Mark Dixon, School of Computing SOFT 120Page 11 Functions To replace the following with a function: m := k / 1.6; Ask: –what goes into the calculation, and –what comes out we: –Put a value for k into the calculation, and –Get a value for m out m k m := k / 1.6

12 Mark Dixon, School of Computing SOFT 120Page 12 Functions: defining The diagram (with types added): The function code: function m(k: double): double; begin Result := k / 1.6; end; m: double k: double m := k / 1.6

13 Mark Dixon, School of Computing SOFT 120Page 13 Functions: calling The function code: function miles(km: double): double; begin Result := km / 1.6; end; Note: –m has changed to miles and k to km –to be meaningful, and unique (different from other identifiers) Can be called: m := miles(k);

14 Mark Dixon, School of Computing SOFT 120Page 14 Functions: calling Function Definition: function miles(km: double): double; begin Result := km / 1.6; end; Function Call: var m: double; var k: double; k := 8; m := miles(k);

15 Mark Dixon, School of Computing SOFT 120Page 15 Example: Functions Demo (form)

16 Mark Dixon, School of Computing SOFT 120Page 16 Example: Functions Demo (code) implementation... const KMperMile = 1.6;... function Miles(km: real): real; begin Result := km / KMperMile; end;... procedure TfrmFuncts.cmdMilesClick(Sender: TObject); var tmpKM: real; var resMiles: real; begin tmpKM := StrToFloat(txtKM.Text); resMiles := Miles(tmpKM); lblMiles.Caption := FloatToStr(resMiles); end; end.


Download ppt "Mark Dixon, School of Computing SOFT 120Page 1 4. User Defined Functions (part 2)"

Similar presentations


Ads by Google