1 / 16

4. User Defined Functions (part 2)

4. User Defined Functions (part 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

Download Presentation

4. User Defined Functions (part 2)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 4. User Defined Functions(part 2)

  2. Revision: Maths - Substitution • Expression for y, in terms of m, x, and c:y = mx + cthis is the formula of a straight line graph. • Given values for m, x, and cm = 2, x = 3, c = 5 • Possible to calculate y: y = mx + c from above y = (m * x) + c computing terms y = (2 * x) + c substitute m with 2 y = (2 * 3) + c substitute x with 3 y = (2 * 3) + 5 substitute c with 5

  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. Revision: Variables • Variables – storedata 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 x 23 Integer Identifier Type Value Memory

  5. Revision: Variables • Variables are: • Declared, using the following syntax (grammar/form):var<identifier>:<type>;for example:var weight: double; • Assigned values, using the following syntax:<identifier>:=<value expression>;for example:weight := 109.45;Note: the data flows backwards (from right to left)

  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. 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. 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. 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. 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. Functions k m := k / 1.6 m • 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 kinto the calculation, and • Get a value for mout

  12. Functions: defining k: double m := k / 1.6 m: double • The diagram (with types added): • The function code:function m(k: double): double;begin Result := k / 1.6;end;

  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. 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. Example: Functions Demo (form)

  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.

More Related