Chapter 7: Using Functions, Subs, and Modules

Slides:



Advertisements
Similar presentations
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
Advertisements

IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
VBA Modules, Functions, Variables, and Constants
Example 2.
Chapter 4 - Visual Basic Schneider
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Chapter 6 Procedures and Functions Instructor: Bindra Shrestha University of Houston – Clear Lake CSCI
Why to Create a Procedure
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
Chapter 7 Code Tables. VB Code Box 7-1 Event Procedure for Compute Button Private Sub hsbExemptions_Change() txtExemptions.Text =Str(hsbExemptions.Value)
CS0004: Introduction to Programming Subprocedures and Modular Design.
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
Sub procedures School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 6, Friday 2/21/03)
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
Visual Basic CODE. Basics of Code Declaration Declaration Set aside a named place to put things Set aside a named place to put things Assignment Assignment.
Introduction to VB.NET 2005 Dr. McDaniel IDS4704 Spring 2005.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
I Power Higher Computing Software Development High Level Language Constructs.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Chapter 3 w Variables, constants, and calculations DIM statements - declaration temporary memory locations identifier, data type, scope data types - values.
String and General Procedures. Answer to the last question of Exam 1 Start Get a number Divide the Number By 2 Is the quotient Equal to 1? Print The Remain.
Programming with Microsoft Visual Basic th Edition
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
Understanding Visual Basic Fundamentals CHAPTER 13 Understanding Visual Basic Fundamentals.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
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.
CS0004: Introduction to Programming
Sub Procedures And Functions
Visual Basic I Programming
Programming Right from the Start with Visual Basic .NET 1/e
A variable is a name for a value stored in memory.
VBA - Excel VBA is Visual Basic for Applications
Object-Oriented Programming: Classes and Objects
Spreadsheet-Based Decision Support Systems
Functions Chapter 6-Part 2.
Method.
2. Understanding VB Variables
Object-Oriented Programming: Classes and Objects
Engineering Innovation Center
11/10/2018.
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Exploring Microsoft Office Access 2007
Chapter 4 - Visual Basic Schneider
VISUAL BASIC.
Chapter 6 Variables What is VBScript?
Procedures and Functions
Introduction to Visual Programming
CS285 Introduction - Visual Basic
CHAPTER FOUR VARIABLES AND CONSTANTS
If, Subroutines and Functions
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
The structure of programming
Chapter 8 - Functions and Functionality
STARTING OUT WITH Visual Basic 2008
Introduction to Computer Programming IT-104
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

Chapter 7: Using Functions, Subs, and Modules Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Plan for Revising Vintage Videos Application Need a way of adding videos to system Need a better way of managing membership list Need capability to add late fees to customer’s bill Need a system to print alphabetical list of members or videos Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Design for Expanded Vintage Videos Project Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Membership Management Form Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Modified Videos Form Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Using General Procedures Event procedures are associated with a particular event and are not usually available to other forms. General procedures are used for specific tasks that are not associated with an event. General procedures are defined in the General Object of a form and then invoked elsewhere in the project. Two types of general procedures: subs (subroutines) functions Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Relationship Between General and Event Procedures Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Subs and Functions A sub is a unit of code that performs a specific task but returns no value. A function is similar to the built in functions in that arguments are passed to it and processed to compute a single value returned by its name. Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Primary Purposes of Subs and Functions Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Working with General Procedures General procedures must be created and then invoked. Invoking a function: variable = functionname(arg1, arg2, …, argn) Invoking a sub: subname arg1, arg2, …, argn Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Creating Subs and Functions To create a sub or function you can Use the Tools|Add Procedure menu command and select the type of procedure to add or simply type the word Sub or Function and press Enter after any existing event or general procedure. In either case, then add the parameters. Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Creating a Function The general form of the function definition statement is: Function FuncName(parameter1 as type, parameter2 as type, …) as type For example Function intFindMax(intNum1 as Integer, intNum2 as Integer) as Integer An important rule is that the name of the function must be assigned a value in the function. Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Creating a Sub The general form of the sub definition statement is: Sub SubName (parameter1 as type, parameter2 as type, …) Note that the sub name is not assigned a data type Example Sub Reverse(curFirst as Currency, curSecond as Currency) Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Relationship Between Sub Definition Statement and Statement Invoking the Sub Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Relationship Between Function Definition Statement and the Statement Invoking the Function Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Matching Arguments and Parameters For both sub and functions, the number and type of arguments in the invoking statement must match the number and type of parameters in the procedure definition statement. In both the argument and parameter list, fixed-size arrays are referenced by the name of the array followed by parentheses. Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Function to Compute Income Taxes Public Function curComputeTaxes(intNumDep As Integer, _ curGrossIncome As Currency) as Currency Dim curTaxIncome As Currency curTaxIncome = curGrossIncome - 4150 - intNumDep * 2650 Select Case curTaxIncome Case Is <= 24650 curComputeTaxes = 0.15 * curTaxIncome Case Is <= 59750 curComputeTaxes = 3697.50 + 0.28 * (curTaxIncome - 24650) Case Is <= 124650 curComputeTaxes = 13525.50 + 0.31 * (curTaxIncome - 59750) Case Is < 271050 curComputeTaxes = 33644.50 + 0.36 * (curTaxIncome - 124650) Case Else curComputeTaxes = 86348.5 + 0.396 * (curTaxIncome - 271050) End Select End Function Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Sub to Reverse Two Values Sub Reverse(curFirst as Currency, curSecond as _ Currency) Dim curTemp as Currency curTemp = curFirst curFirst = curSecond curSecond = curTemp End sub Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Sub to Sort Arrays Public Sub Sort(curFirstList() As Currency, _ curSecondList()As Currency, intNumList As Integer) Dim blnNoReversal As Boolean, intCounter As Integer blnNoReversal = False Do Until blnNoReversal blnNoReversal = True For intCounter = 0 To intNumList - 2 If curFirstList(intCounter ) > _ curFirstList(intCounter + 1) Then Reverse curFirstList(intCounter ), _ FirstList(intCounter + 1) StrReverse curSecondList(intCounter ), _ curSecondList(intCounter +1) End If Next Loop End Sub Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Global Declarations and the Code Module In a global declaration, the scope of global variables, as compared to form-level variables or procedure-level variables, includes all parts of the project. The Code Module is the section of pure code that is known to all parts of the project. Use Public statement to declare variables Public varName1 as type, varName2 as type, ... Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Scope of Global Variables Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Use of String Functions Len(string) --returns number of characters in string. Left(string, N) or Right(string, N) --returns the leftmost or rightmost N characters in a string. Mid(String,P,N) --returns N characters in a string starting at Pth character. LTrim(string) or RTrim(string) --trims blank characters from left (right) end of string. Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

Passing by Value in Subs If there is a two-way communication between arguments and parameters, then you have passing by reference. If there is a one-way communication between arguments and parameters, then you have passing by value. It is possible to force passing by value by adding the keyword ByVal prior to a variable in the procedure definition statement. Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy