Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon, SoCCE SOFT 131Page 1 10 – User Defined Functions.

Similar presentations


Presentation on theme: "Mark Dixon, SoCCE SOFT 131Page 1 10 – User Defined Functions."— Presentation transcript:

1 Mark Dixon, SoCCE SOFT 131Page 1 10 – User Defined Functions

2 Mark Dixon, SoCCE SOFT 131Page 2 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

3 Mark Dixon, SoCCE SOFT 131Page 3 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

4 Mark Dixon, SoCCE SOFT 131Page 4 Built in Functions: Sqr Sqr – gives square root of a number: –Examples Sqr(4)returns16 Sqr(3)returns9 Sqr(2)returns4 Square Root Option Explicit Private Sub txtNum_Change() Dim tmpNum As Double tmpNum = Val(Me.txtNum.Text) Me.lblResult.Caption = Sqr(tmpNum) End Sub Sqr Double X: Double

5 Mark Dixon, SoCCE SOFT 131Page 5 Rnd() function –generates pseudo-random numbers –>= 0 and <1 Randomize – initialises random number generator Built in Functions: Rnd Random Single Random Numbers Option Explicit Private Sub Form_Load() Randomize End Sub Private Sub btnRandom_Click() Me.lblNum.Caption = Rnd() End Sub

6 Mark Dixon, SoCCE SOFT 131Page 6 User Defined Functions (how) Syntax very similar to procedure definition: Function ( ) As [ ] = End Function Where – represents function’s name you choose – represent information needed – represents the return type – represent the return value

7 Mark Dixon, SoCCE SOFT 131Page 7 Exercise 1: Function Diagrams 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 Thing double Miles doublekm: double Twice longnum: long

8 Mark Dixon, SoCCE SOFT 131Page 8 Exercise 2: Function Headers Generate the code for the following diagrams: Minutes integer Hours: integer Mins: integer Euros integerPounds: integer Function Minutes(Mins As Integer, _ Hours As Integer) As Integer Function Euros(Pounds As Integer) As Integer

9 Mark Dixon, SoCCE SOFT 131Page 9 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 Function Implementation: Code Double integernum: integer

10 Mark Dixon, SoCCE SOFT 131Page 10 Meet George Common Boa Constrictor –boa constrictor imperator Native to Central & South America No venom (no poison)

11 Mark Dixon, SoCCE SOFT 131Page 11 Meet George (cont.)

12 Mark Dixon, SoCCE SOFT 131Page 12 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

13 Mark Dixon, SoCCE SOFT 131Page 13 George (cont.) To convert from Fahrenheit to Celsius: To convert from Celsius to Fahrenheit:

14 Mark Dixon, SoCCE SOFT 131Page 14 Example 1: 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

15 Mark Dixon, SoCCE SOFT 131Page 15 FtoC Function The declaration: Function FtoC(F As double) As double FtoC = ((f-32) * 5) / 9 End Function The call: lblResult.Caption = FtoC(50)

16 Mark Dixon, SoCCE SOFT 131Page 16 Example 2: 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

17 Mark Dixon, SoCCE SOFT 131Page 17 Example 3: Total Total


Download ppt "Mark Dixon, SoCCE SOFT 131Page 1 10 – User Defined Functions."

Similar presentations


Ads by Google