Mark Dixon, SoCCE SOFT 136Page 1 11 – User Defined Functions.

Slides:



Advertisements
Similar presentations
Mark Dixon Page 1 23 – Object Oriented Programming in PhP.
Advertisements

Mark Dixon, SoCCE SOFT 131Page 1 15 – Object Oriented Analysis, Design, and Programming.
Mark Dixon, SoCCE SOFT 131Page 1 22 – Object Oriented Analysis, Design, and Programming.
Bar Graphs. Bar Graph or Double Bar Graph (REVIEW) A bar graph uses length of bars to represent numbers and COMPARE data. In some cases, the use of a.
Mark Dixon Page 1 12 – Functions and Modules. Mark Dixon Page 2 Questions: Parameters Name a parameter in the following code: Sub Move(ByRef obj, ByVal.
Mark Dixon, SoCCE SOFT 131Page 1 09 – Procedures.
Mark Dixon Page 1 22 – Object Oriented Programming in ASP.
Mark Dixon, SoCCE SOFT 136Page 1 05 – Conditional Execution.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Object Oriented Analysis, Design, and Programming.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –Revise Variables and Conditional execution.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 19 – Procedure Parameters.
Mark Dixon Page 1 21 – Object Oriented Programming in ASP.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Procedures.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Variable Scope, and Arrays of Structures.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Conditional Execution.
Mark Dixon Page 1 02 – Queries: Query by Example.
Mark Dixon, SoCCE SOFT 131Page 1 10 – User Defined Functions.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 12 – Enumerated Data-Types & Pass-by-reference.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Control Arrays & Container Controls.
Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables.
Mark Dixon, SoCCE SOFT 131Page 1 09 – User Defined Procedures: Scope, and Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 11 – Arrays of Structures & Modules.
Function.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon Page 1 10 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of code to declare a variable called h Write a line of.
02 – Client-side code: JavaScript
Mark Dixon Page 1 24 – Object Oriented Programming in ASP.
Equations of straight lines
Mark Dixon Page 1 09 – Arrays. Mark Dixon Page 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
Mark Dixon, SoCCE SOFT 136Page 1 9 – Procedures. Mark Dixon, SoCCE SOFT 136Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
Mark Dixon 1 11 – Array Variables. Mark Dixon 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 136Page 1 SOFT – Module Introduction & Overview.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Variables. Mark Dixon, SoCCE SOFT 131Page 2 Admin: Test (next week) In class test –teaching week 6 –university week.
Mark Dixon 1 14 – Functions and Modules. Mark Dixon 2 Questions: Parameters Consider the following code: Sub Move(ByRef obj, ByVal dist) obj.style.posLeft.
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon 1 03 – Information Processing. Mark Dixon 2 Questions: Events Consider the following code: a) How many unique events does it contain? b) Name.
Mark Dixon, SoCCE SOFT 131Page 1 23 – Object Associations.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Datatypes and Object Association.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, School of Computing SOFT 120Page 1 4. User Defined Functions (part 2)
Introduction to Temperature Lecture Notes. Schedule for the Week  Monday- Notes over Temperature  Tuesday- Notes over Air Pressure  Wednesday- Notes.
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 18 – Enumerated Data Types and Arrays of Structures.
Mark Dixon SOFT 131Page 1 12 – Object Oriented Analysis, Design, and Programming.
Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2.
Mark Dixon Page 1 15 – Structured Programming. Mark Dixon Page 2 Admin: Coursework 3 – Test In class test –9 Feb 2010 –2 Feb 2010: revision (technique)
Mark Dixon, SoCCE SOFT 131Page 1 17 – Procedures.
Mark Dixon Page 1 03 – Information Processing: Expressions, Operators & Functions.
Mark Dixon 1 15 – Structured Programming. Mark Dixon 2 Admin: Test 2 In class test –11 Feb 2014 –4 Feb 2014: revision (technique) session 50 mins short.
Mark Dixon 1 13 – Parameters. Mark Dixon 2 Question: Arrays How many array variables are in the following code: Dim x Dim y Dim f(4) x = 12 y = 6 f(2)
Mark Dixon, SoCCE SOFT 131Page 1 25 – Soft 131 Examination Revision.
Mark Dixon 1 Soft051 Examination Sample Questions.
Mark Dixon Page 1 09 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of VBScript code to declare a variable called h Write.
Mark Dixon Page 1 03 – Information Processing: Expressions, Operators & Functions.
Pseudo-code. Pseudo-code Task 1 Preparation Use the following code to declare, initialise and populate an array in a new VB.NET console application:
3. User Defined Functions
04 – Information Processing: Expressions, Operators & Functions
11 – Enumerated Data Types, & Procedure Parameters
08 – Iterative Execution.
11 – Functions and Modules
Presentation transcript:

Mark Dixon, SoCCE SOFT 136Page 1 11 – User Defined Functions

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

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

Mark Dixon, SoCCE SOFT 136Page 4 Meet George (cont.)

Mark Dixon, SoCCE SOFT 136Page 5 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 136Page 6 George (cont.) To convert from Fahrenheit to Celsius: e.g. 50 Fahrenheit is: c = 10

Mark Dixon, SoCCE SOFT 136Page 7 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 Calculation repeated

Mark Dixon, SoCCE SOFT 136Page 8 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 136Page 9 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

Mark Dixon, SoCCE SOFT 136Page 10 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

Mark Dixon, SoCCE SOFT 136Page 11 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

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

Mark Dixon, SoCCE SOFT 136Page 13 FtoC Animation

Mark Dixon, SoCCE SOFT 136Page 14 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 136Page 15 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 136Page 16 Question: 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

Mark Dixon, SoCCE SOFT 136Page 17 Question: 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

Mark Dixon, SoCCE SOFT 136Page 18 Function Animation: Total

Mark Dixon, SoCCE SOFT 136Page 19 Example: SnakeTemp v1 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

Mark Dixon, SoCCE SOFT 136Page 20 Example: SnakeTemp v2 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

Mark Dixon, SoCCE SOFT 136Page 21 Example: SnakeTemp v3 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

Mark Dixon, SoCCE SOFT 136Page 22 Tutorial Exercises: Temp Task 1: Get the temperature examples v1 and v2 (from the lecture) working

Mark Dixon, SoCCE SOFT 136Page 23 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 136Page 24 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): DateYearsInches Tuesday 25 June ½ Monday 25 June ½ Sunday 25 June Saturday 25 December 19993½66.0 Friday 25 June Friday 25 December 19982½57.5 Thursday 25 June Thursday 25 December 19971½43.0 Wednesday 25 June 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).