Understanding User Defined Functions and Variables in Code
160 likes | 256 Vues
Learn to define functions, assign variables values, and declare data types in programming. Understand the logic and syntax with detailed examples.
Understanding User Defined Functions and Variables in Code
E N D
Presentation Transcript
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
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
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
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)
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
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
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
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
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
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
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;
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);
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);
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.