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

Slides:



Advertisements
Similar presentations
Mark Dixon, SoCCE SOFT 131Page 1 15 – Object Oriented Analysis, Design, and Programming.
Advertisements

Mark Dixon, SoCCE SOFT 131Page 1 22 – Object Oriented Analysis, Design, and Programming.
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 136Page 1 11 – User Defined Functions.
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 15 – Object Associations.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Debugging & Graphics.
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 07 – Constants, Arrays, & Structures.
Mark Dixon, SoCCE SOFT 131Page 1 19 – Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Object Associations.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Procedures.
Mark Dixon, SoCCE SOFT 131Page 1 02 – Software Development Lifecycle, & User Interface Design.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Variable Scope, and Arrays of Structures.
Introduction to Java Programming, 4E Y. Daniel Liang.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Passing Data between pages: Sessions, Query Strings, & Self Posting.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Conditional Execution.
Mark Dixon Page 1 02 – Queries: Query by Example.
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.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Mark Dixon, SoCCE SOFT 131Page 1 09 – User Defined Procedures: Scope, and Parameters.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Mark Dixon, SoCCE SOFT 131Page 1 25 – Soft 131 Examination Revision.
Mark Dixon, SoCCE SOFT 131Page 1 11 – Arrays of Structures & Modules.
Function.
Section 3.6 BUILT-IN FUNCTIONS involving numbers & strings.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
02 – Client-side code: JavaScript
1 Subroutines and Functions Chapter 6 in Deitel, Deitel and Nieto.
Mark Dixon Page 1 24 – Object Oriented Programming in ASP.
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, SoCCE SOFT 131Page 1 03 – Information Processing: Expressions, Operators & Functions.
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
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)
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, 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.
3. User Defined Functions
04 – Information Processing: Expressions, Operators & Functions
Sub Procedures and Functions
08 – Iterative Execution.
11 – Functions and Modules
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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)

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

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