210 likes | 369 Views
12 – User Defined Functions. 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. Procedures and Functions. Both Procedures and Functions
E N D
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
Procedures and Functions • Both Procedures and Functions • Group of statements • Identified by uniquename • mirror real life activities • Procedures – just do something • Functions – return a value
Built in Functions: Sqr X: Double Sqr Double • Sqr – gives square root of a number: • Examples Sqr(4) returns 16 Sqr(3) returns 9 Sqr(2) returns 4 Option Explicit Private Sub txtNum_Change() Dim tmpNum As Double tmpNum = Val(Me.txtNum.Text) Me.lblResult.Caption = Sqr(tmpNum) End Sub SquareRoot
Built in Functions: Rnd Random Single • Rnd() function • generates pseudo-random numbers • >= 0 and <1 • Randomize – initialises random number generator RandomNumbers Option Explicit Private Sub Form_Load() Randomize End Sub Private Sub btnRandom_Click() Me.lblNum.Caption = Rnd() End Sub
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
Exercise: Function Diagrams Thing double km: double Miles double num: long Twice long • Draw function diagram for the following code: Function Thing() As Double Function Miles(km As Double) As Double Function Twice(num As Long) As Long
Exercise: Function Headers Mins: integer Minutes integer Hours: integer Pounds: integer Euros integer • Generate the code for the following diagrams: Function Euros(Pounds As Integer) As Integer Function Minutes(Mins As Integer, _ Hours As Integer) As Integer
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: Function Double(num As integer) As integer Double = num * 2 End Function num: integer Double integer
Example: Total Total
Meet George • Common Boa Constrictor • boa constrictor imperator • Native to Central & South America • No venom (no poison)
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
George (cont.) • To convert from Fahrenheit to Celsius: • To convert from Celsius to Fahrenheit:
Example: Temp v1 Temp v1 Option Explicit 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
FtoC Function • The declaration: Function FtoC(F As double) As double FtoC = ((f-32) * 5) / 9 End Function • The call: lblResult.Caption = FtoC(50)
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
Example: SnakeTemp v1 Option Explicit Sub Draw() Dim t As Long picTemp.Cls 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 SnakeTemp v1
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). 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 = " & (((t - 32) * 5) / 9) & "C" Next picTemp.Line (x1, (txtCur.Text - yO) * yS)-(x4, (txtCur.Text - yO) * yS), 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 SnakeTemp v2
Example: SnakeTemp v3 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). 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 = " & FtoC(t) & "C" Next picTemp.Line (x1, (txtCur.Text - yO) * yS)-(x4, (txtCur.Text - yO) * yS), vbRed picTemp.Print txtCur.Text & "F = " & FtoC(txtCur.Text) & "C" End Sub Private Sub Form_Load() Me.Show Draw End Sub Private Sub txtCur_Change() Draw End Sub SnakeTemp v3