21 – User Defined Functions Mark Dixon, SoCCE SOFT 131 Page 1 Session Aims & Objectives • Aims – To introduce user defined functions • Objectives, by end of this week’s sessions, you should be able to: – Create your own function definitions – Call these functions Mark Dixon, SoCCE SOFT 131 Page 2 Meet George • Common Boa Constrictor – boa constrictor imperator • Native to Central & South America • No venom (no poison) Mark Dixon, SoCCE SOFT 131 Page 3 Meet George (cont.) Mark Dixon, SoCCE SOFT 131 Page 4 George (cont.) • Problem: – Difficult to keep – Require temperature and humidity controlled environment – Much of the literature is from the US • Temperature in Fahrenheit • Solution – Need a program to convert from Celsius to Fahrenheit Mark Dixon, SoCCE SOFT 131 Page 5 George (cont.) • To convert from Fahrenheit to Celsius: ( f 32) 5 c 9 e.g. 50 Fahrenheit is: (50 32) 5 c 9 c = 10 Mark Dixon, SoCCE SOFT 131 Page 6 Example: Temp v1 Temp v1 Option Explicit Calculation repeated Private Sub Form_Load() lblResult.Caption = ((txtFah.Text - 32) * 5) / 9 End Sub Private Sub txtFah_Change() lblResult.Caption = ((txtFah.Text - 32) * 5) / 9 End Sub Mark Dixon, SoCCE SOFT 131 Page 7 Procedures and Functions • Both Procedures and Functions – Group of statements – Identified by unique name – mirror real life activities • Procedures – just do something • Functions – return a value – used to perform calculations Mark Dixon, SoCCE SOFT 131 Page 8 Built in Functions: Sqr Option Explicit Private Sub txtNum_Change() Dim tmpNum As Double tmpNum = Val(Me.txtNum.Text) Me.lblResult.Caption = Sqr(tmpNum) End Sub Square Root • Sqr – gives square root of a number: X: Double Sqr Double – Examples Sqr(4) Sqr(3) Sqr(2) Mark Dixon, SoCCE returns returns returns SOFT 131 16 9 4 Page 9 Built in Functions: Rnd Option Explicit Private Sub Form_Load() Randomize End Sub Random Numbers Private Sub btnRandom_Click() Me.lblNum.Caption = Rnd() End Sub • Rnd() function – generates pseudo-random numbers – >= 0 and <1 Random Single • Randomize – initialises random number generator Mark Dixon, SoCCE SOFT 131 Page 10 User Defined Functions (how) • Syntax very similar to procedure definition: Function <name>(<parameters>) As <type> [<Statement-Block>] <name> = <value> End Function • Where – <name> represents function’s name you choose – <parameters> represent information needed – <type> represents the return type – <value> represent the return value Mark Dixon, SoCCE SOFT 131 Page 11 Function Implementation: Code • Function Header – gives: – name (e.g. Double), – parameter names and types (e.g. num: integer), and – return type (e.g. integer) • Function Body – gives code: num: integer Double integer Function Double(num As integer) As integer Double = num * 2 End Function Mark Dixon, SoCCE SOFT 131 Page 12 FtoC Animation Mark Dixon, SoCCE SOFT 131 Page 13 FtoC Function • The declaration: Function FtoC(F As double) As double FtoC = ((f-32) * 5) / 9 End Function • The call: lblResult.Caption = FtoC(50) Mark Dixon, SoCCE SOFT 131 Page 14 Example: Temp v2 Temp v2 Option Explicit Function FtoC(F As Double) As Double FtoC = ((F - 32) * 5) / 9 End Function Private Sub Form_Load() lblResult.Caption = FtoC(txtFah.Text) End Sub Private Sub txtFah_Change() lblResult.Caption = FtoC(txtFah.Text) End Sub Mark Dixon, SoCCE SOFT 131 Page 15 Question: Function Diagrams • Draw function diagram for the following code: Function Thing() As Double Thing double Function Miles(km As Double) As Double km: double Miles double Function Twice(num As Long) As Long num: long Mark Dixon, SoCCE Twice SOFT 131 long Page 16 Question: Function Headers • Generate the code for the following diagrams: Pounds: integer Euros integer Function Euros(Pounds As Integer) As Integer Mins: integer Hours: integer Minutes integer Function Minutes(Mins As Integer, _ Hours As Integer) As Integer Mark Dixon, SoCCE SOFT 131 Page 17 Function Animation: Total Mark Dixon, SoCCE SOFT 131 Page 18 Example: SnakeTemp v1 Option Explicit Sub Draw() Dim t As Long picTemp.Cls SnakeTemp v1 picTemp.Line (200, 1 * 200)-(200, 16 * 200) For t = 80 To 95 Step 1 picTemp.Line (200, (t - 79) * 200)-(300, (t - 79) * 200) Next For t = 80 To 95 Step 5 picTemp.Line (300, (t - 79) * 200)-(500, (t - 79) * 200) picTemp.Print t & "F = " & (((t - 32) * 5) / 9) & "C" Next picTemp.Line (200, (txtCur.Text - 79) * 200)-(600, (txtCur.Text - 79) * 200), vbRed picTemp.Print txtCur.Text & "F = " & (((txtCur.Text - 32) * 5) / 9) & "C" End Sub Private Sub Form_Load() Me.Show Draw End Sub Private Sub txtCur_Change() Draw End Sub Mark Dixon, SoCCE SOFT 131 Page 19 Example: SnakeTemp v2 Option Explicit Const st = 80 Const en = 95 Const x1 = 200 Const x2 = 300 Const x3 = 500 Const x4 = 600 Const yS = 200 ' Vertical scaling factor. Const yO = st - 1 ' Vertical offset (gap at top). SnakeTemp v2 Sub Draw() Dim t As Long picTemp.Cls x1 st yO) * yS)-(x1, (en - yO) * yS) picTemp.Line ( ,( For t = st To en Step 1 picTemp.Line ( Next x1, (t - yO) * yS)-(x2, (t - yO) * yS) For t = st To en Step 5 x2 yO yS x3 yO) * yS) picTemp.Line ( , (t )* )-( , (t picTemp.Print t & "F = " & (((t - 32) * 5) / 9) & "C" Next x1 yO yS x4 yO) * yS), vbRed picTemp.Line ( , (txtCur.Text )* )-( , (txtCur.Text picTemp.Print txtCur.Text & "F = " & (((txtCur.Text - 32) * 5) / 9) & "C" End Sub Private Sub Form_Load() Me.Show Draw End Sub Private Sub txtCur_Change() Draw End Sub Mark Dixon, SoCCE SOFT 131 Page 20 Example: SnakeTemp v3 Option Explicit Const st = 80 Const en = 95 SnakeTemp v3 Const x1 = 200 Const x2 = 300 Const x3 = 500 Const x4 = 600 Const yS = 200 ' Vertical scaling factor. Const yO = st - 1 ' Vertical offset (gap at top). Function FtoC(F As Double) As Double FtoC = ((F - 32) * 5) / 9 End Function Sub Draw() Dim t As Long picTemp.Cls picTemp.Line (x1, (st - yO) * yS)-(x1, (en - yO) * yS) For t = st To en Step 1 picTemp.Line (x1, (t - yO) * yS)-(x2, (t - yO) * yS) Next For t = st To en Step 5 picTemp.Line (x2, (t - yO) * yS)-(x3, (t - yO) * yS) picTemp.Print t & "F = " & Next FtoC(t) & "C" picTemp.Line (x1, (txtCur.Text - yO) * yS)-(x4, (txtCur.Text - yO) * yS), vbRed picTemp.Print txtCur.Text & "F = " & End Sub FtoC(txtCur.Text) & "C" Private Sub Form_Load() Me.Show Draw End Sub Private Sub txtCur_Change() Draw End Sub Mark Dixon, SoCCE SOFT 131 Page 21 Tutorial Exercises: Temp • Task 1: Get the temperature examples v1 and v2 (from the lecture) working Mark Dixon, SoCCE SOFT 131 Page 22 Tutorial Exercises: Snake Temp • Task 1: Get the snake temperature examples from the lecture working. • Task 2: There is another place that a function can be used to simplify the code. Hint: it’s to do with calculating the vertical plotting position Mark Dixon, SoCCE SOFT 131 Page 23 Tutorial Exercises: Snake Length • Task 1: Create a program that stores the following length (inch) data in an array, and plots a graph of it (using line method): Date Tuesday 25 June 2002 Years 6 5½ 5 4½ 4 3½ 3 2½ 2 1½ 1 Monday 25 June 2001 Sunday 25 June 2000 Saturday 25 December 1999 Friday 25 June 1999 Friday 25 December 1998 Thursday 25 June 1998 Thursday 25 December 1997 Wednesday 25 June 1997 Inches 70.5 70.0 68.0 66.0 61.0 57.5 48.5 43.0 37.5 • Task 2: Modify your code – add code that calculates an average length and plots it on the graph (use a function to calculate the total). • Task 3: Modify your code – to convert all values to cm (1 inch = 2.54 cm). Mark Dixon, SoCCE SOFT 131 Page 24
© Copyright 2026 Paperzz