VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime.

Slides:



Advertisements
Similar presentations
Chapter 6, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 6 Sub Procedures And Functions.
Advertisements

Introduction to C Programming
Sub and Function Procedures
1 Procedural Programming Paradigm Stacks and Procedures.
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
VBA Modules, Functions, Variables, and Constants
Example 2.
Chapter 5 - Menus, Sub Procedures, and Sub Functions  Menus - controls - properties and events –menu editor - create and change –defining menus - menu.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
Multiple Forms & Procedures. Form Methods: –Show, Hide, Activate, Close Events: –Load, Activated, Closing, Closed.
PSU CS 106 Computing Fundamentals II VB Statements HM 5/19/2008.
PSU CS 106 Computing Fundamentals II VB Subprograms & Functions HM 4/29/2008.
Passing Arrays to Procedures: ByVal vs. ByRef. Passing Arrays to Procedures Passing the Array – Specify the name of the array without using parentheses.
Chapter 41 Sub Procedures, Part II Passing by Value Passing by Reference Local Variables Class-Level Variables Debugging.
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Apply Sub Procedures/Methods and User Defined Functions
Subroutines and Functions Chapter 6. Introduction So far, most of the code has been inside a single method for an event –Fine for small programs, but.
IE 212: Computational Methods for Industrial Engineering
Chapter 6 Procedures and Functions Instructor: Bindra Shrestha University of Houston – Clear Lake CSCI
1 Subroutines and Functions Chapter 6 in Deitel, Deitel and Nieto.
Why to Create a Procedure
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
Microsoft Access Using Visual Basic Routines. Visual Basic Datatypes Boolean Byte Currency Date Double Integer Long Object Single String Variant Hyperlink.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
CS0004: Introduction to Programming Subprocedures and Modular Design.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
Subroutines and Functions. Introduction So far, most of the code has been inside a single method for an event –Fine for small programs, but inconvenient.
VB Core II Conditional statements Exception handling Loops Arrays Debugging.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
1 Chapter 4 – Breaking It Up: Functions spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Introduction to VB.NET 2005 Dr. McDaniel IDS4704 Spring 2005.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
MS Visual Basic 6 Walter Milner. VB 6 0 Introduction –background to VB, A hello World program 1 Core language 1 –Projects, data types, variables, forms,
Overview of VBA Programming & Syntax. Programming With Objects u Objects –Properties: attributes or characteristics of an object (e.g., font size, color,
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
I Power Higher Computing Software Development High Level Language Constructs.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
ME 142 Engineering Computation I Using Subroutines Effectively.
PSU CS 106 Computing Fundamentals II VB Declarations HM 5/4/2008.
Top-down approach / Stepwise Refinement & Procedures & Functions.
ME 142 Engineering Computation I Using Subroutines Effectively.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
Sub Procedures; Passing Values Back From Sub Procedures Passing by reference Passing by value.
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
More on Variables and Subroutines. Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Subroutines and Functions Chapter 6. Introduction So far, all of the code you have written has been inside a single procedure. –Fine for small programs,
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Sub Procedures and Functions Visual Basic. Sub Procedures Slide 2 of 26 Topic & Structure of the lesson Introduction to Modular Design Concepts Write.
Lecture 7 Methods (functions and subroutines) Parameter Passing
Sub Procedures Chapter 6-Part 1. Chapter 6 Part 12 Event Procedures Code that occurs based upon event. Mouse click Got focus Repetitive code that might.
COMPUTATIONAL CONSTRUCTS
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
2. Understanding VB Variables
CIS16 Application Development and Programming using Visual Basic.net
Tonga Institute of Higher Education
Procedures: Functions and Subroutines
The structure of programming
Chapter 8 - Functions and Functionality
STARTING OUT WITH Visual Basic 2008
Introduction to VB programming
10.2 Procedures Passing Parameters 30/08/2019.
Presentation transcript:

VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime

Private Sub Command1_Click() Dim x, y As Double x = 1 y = 2 Debug.Print addUp(x, y) End Sub Private Function addUp(a as double, b As Double) addUp = a + b End Function User-defined functions x and y copied to a and b

Exercise – user-defined functions Write a function which calculates square root Use Newton's iteration – x approximates the square root of a hint – loop until abs(x*x-a)<1e-6

Subroutines = procedures Private Sub Command1_Click() Dim x As Double, y As Double x = 1 y = 200 Call bigger(x, y) End Sub Private Sub bigger(first As Double, second As Double) If first > second Then Debug.Print first Else Debug.Print second End If End Sub Functions return values Procedures do not – they just do something

Parameter passing – 2 methods Or parameters are passed by reference The addresses of the parameters are passed to the function Changing the parameter does affect the original Less memory and faster Dangerous – less modularity Use keyword ByRef in VB Parameters can be passed by value Copies of parameters are passed to the function (on a stack) Changing parameter in the function does not affect the original Uses extra memory – significant for large array And time for copy Default in VB – keyword ByVal

Parameter passing by reference Private Sub Command1_Click() Dim x As Double, y As Double x = 1 y = 200 Call swap(x, y) Debug.Print x, y End Sub Private Sub swap(ByRef first As Double, ByRef second As Double) Dim temp As Double temp = first first = second second = temp End Sub

Named arguments Private Sub Command1_Click() Dim x As Double, y As Double x = 1 y = 2 Debug.Print addUp(second:=x, first:=y) End Sub Private Function addUp(first As Double, second As Double) addUp = first End Function

Modules Separate files Contain VB functions, procedures and global variables Use for code and data which is needed in more than 1 form

Module example

Scope – public private dim Variables should be as local as possible scope of a variable : part of a program in which a variable can be referred to

Lifetime – how long a variable lasts DIM – local to a sub – execution of the sub Private/public in a form – while form is open Public in a module – while application runs Static – local to a sub – retains value

Modules and scope - exercise In a module, write a procedure which reverses the elements in an array The array should be public The array has 10 elements. The procedure should exchange elements 1 and 10, 2 and 9 and so on Call the procedure from a button on a form