Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Slides:



Advertisements
Similar presentations
Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
Advertisements

Sub and Function Procedures
1.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
Fundamentals of Programming in Visual Basic
Chapter 4 General Procedures
VBA Modules, Functions, Variables, and Constants
Example 2.
Chapter 4 - Visual Basic Schneider
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
Chapter 4 - VB.Net by Schneider1 Chapter 4 General Procedures 4.1 Sub Procedures, Part I 4.2 Sub Procedures, Part II 4.3 Function Procedures 4.4 Modular.
Introduction to Computing Dr. Nadeem A Khan. Lecture 18.
Chapter 7: Sub and Function Procedures
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
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.
Chapter 41 General Procedures Sub Procedures, Part I Sub Procedures, Part II Function Procedures.
Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
Chapter 6: Functions.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Apply Sub Procedures/Methods and User Defined Functions
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
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.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
XP Chapter 7 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Enhancing User Interaction Through Programming Chapter.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Enhancing User Interaction Through Programming
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Chapter 5 - VB 2008 by Schneider1 Chapter 5 - General Procedures 5.1 Sub Procedures, Part I 5.2 Sub Procedures, Part II 5.3 Function Procedures 5.4 Modular.
Why to Create a Procedure
1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design.
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
5-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Array Processing: Exercises School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 10, Friday 3/28/2003)
1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design.
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.
Sub procedures School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 6, Friday 2/21/03)
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Subprograms CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Chapter 6 Sub Procedures
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
Sub Procedures. A Sub procedure is a block of code that is executed in response to an event. There are two types of Sub procedures, general procedures.
Chapter 8 - Visual Basic Schneider
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Introduction to Visual Basic Programming. Introduction Simple Program: Printing a Line of Text Another Simple Program: Adding Integers Memory Concepts.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
JavaScript, Fourth Edition
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable.
Programming with Microsoft Visual Basic th Edition
1Edited By Maysoon Al-Duwais. 2 General Procedures Function Procedure Sub Procedures, Part I Sub Procedures, Part II Modular Design Edited By Maysoon.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Sub Procedures; Passing Values Back From Sub Procedures Passing by reference Passing by value.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
General Procedures Chapter 4. Different Procedures 4.1 Sub Procedures, Part I 4.2 Sub Procedures, Part II 4.3 Function Procedures 4.4 Modular Design (not.
1Edited By Maysoon Al-Duwais. 2 Devices for Modularity Visual Basic has two ways for breaking problems into smaller pieces: Sub procedures Function procedures.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Functions Chapter 6-Part 2.
Using Procedures and Exception Handling
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Variables and Arithmetic Operations
Exploring Microsoft Office Access 2007
Chapter 4 - Visual Basic Schneider
Chapter 6 Sub Procedures
CS285 Introduction - Visual Basic
Function Procedures.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures

Chapter 4 - Visual Basic Schneider2 Outline & Objective u Sub Procedures u Procedure Parameters u Function Procedures u Modular Design

Chapter 4 - Visual Basic Schneider3 What is a procedure? u A general procedure is a set of commands that is given a name so that it can be invoked by another part of the program u Procedures make a program easier to develop, test, and correct

Chapter 4 - Visual Basic Schneider4 General Procedures u In Visual Basic, there are two types of procedures: –Sub –Function u Note: To distinguish procedures from event procedures, Sub and Function procedures are referred to as general procedures.

Chapter 4 - Visual Basic Schneider5 Sub Procedures Properties: u may be called u may be passed data called arguments u may return values to the calling program u may change the data stored in a received variable

Chapter 4 - Visual Basic Schneider6 Passing Arguments to Subs: u When you define a Sub procedure, sometimes you need to transfer variables that are used in different Subs. This is called passing in programming languages.

Chapter 4 - Visual Basic Schneider7 Components of Sub Procedure: u name: used to identify the Sub procedure u parameters: a Sub procedure accepts values from the caller through its parameters. It may also send values back to the caller through its parameters.

Chapter 4 - Visual Basic Schneider8 Sub Procedure's Name u The rules for naming Sub Procedures are the same as naming variables. u In this text, Sub procedure names begin with uppercase letters in order to distinguish them from variable names.

Chapter 4 - Visual Basic Schneider9 Syntax of a Sub Procedure Private Sub ProcedureName ( ) statement(s) End Sub

Chapter 4 - Visual Basic Schneider10 Creating Visual Basic Sub Procedure: u Activate the code window u Select Add Procedure from the Tools menu u Type in the name of the Sub procedure u Click on Private in the Scope box u Press the Enter key or click the OK button u Type the statements of the Sub procedure into the code window

Chapter 4 - Visual Basic Schneider11 Example of Call to a Sub Procedure: Private Sub cmdCompute_Click() Dim num As Single num = Val(InputBox("Enter a number:")) Call Triple(num) End Sub

Chapter 4 - Visual Basic Schneider12 Sub Procedure Triple: Private Sub Triple(num As Single) ' Multiply the value of the number by 3 picResult.Print "The number is"; 3 * num End Sub

Chapter 4 - Visual Basic Schneider13 Passing Arguments to Sub Procedures u Arguments : Variables or expressions placed in parentheses in a Call statement. u Not only is the value of the argument passed to the parameter, but the value of the parameter is passed back to the argument.

Chapter 4 - Visual Basic Schneider14 Example of Arguments u Call Triple(num)

Chapter 4 - Visual Basic Schneider15 Parameters u Variables placed in parentheses after a Sub Procedure's name. u When the procedure is called, the values of the corresponding arguments are placed in the parameters.

Chapter 4 - Visual Basic Schneider16 Example of Parameters u Private Sub Triple(num As Single)

Chapter 4 - Visual Basic Schneider17 Passing arguments to parameters Call Triple(num) Private Sub Triple (num As Single) Argument Parameter

Chapter 4 - Visual Basic Schneider18 Passing Arguments to Parameters u Call Add (x, y ) u Private Sub Add ( num1 As Single, num2 As Single) Parameters Arguments

Chapter 4 - Visual Basic Schneider19 Passing Arguments u A Sub procedure receives the location of the arguments, and may use and modify the value of the arguments stored at that location. u Two-way into and out of the Sub Procedure.

Chapter 4 - Visual Basic Schneider20 Passing Arguments Private Sub cmdDisplay_Click() Dim amt As Single amt = 2 picResults.Print amt; Call Triple(amt) picResults.Print amt End Sub

Chapter 4 - Visual Basic Schneider21 Sub Triple Private Sub Triple(num As Single) ' Triple a number picResults.Print num; num = 3 * num picResults.Print num; End Sub

Chapter 4 - Visual Basic Schneider22 Passing Arguments Call Triple(amt) Private Sub Triple (num As Single) amt num

Chapter 4 - Visual Basic Schneider23 Passing Data - by Reference 2266 amt num

Chapter 4 - Visual Basic Schneider24 Important Rules for Passing Arguments to a Sub u The number of arguments and parameters must match. u The data type of each argument must match its corresponding parameter.

Chapter 4 - Visual Basic Schneider25 Local Variables: u A variable that is used only in a specific procedure (Sub or Function). u The scope of the local variable is the Sub or Function in which that variable has been defined.

Chapter 4 - Visual Basic Schneider26 Local Variables: u Declared within a procedure definition u Private to a procedure definition u Variables in different procedures are totally independent u Different procedures can have variables with the same names; however, each variable will have its own memory location

Chapter 4 - Visual Basic Schneider27 Advantages of Local Variables u Extremely useful for team programming u Protects against accidental side effects (change of the value of the variable in one procedure that causes an error in another)

Chapter 4 - Visual Basic Schneider28 Example of Local Variables Private Sub cmdButton_Click() Dim var1 As Integer, var2 As Integer,num As Integer var1 = 2 var2 = 4 Call Add(num) picBox.Print num End Sub

Chapter 4 - Visual Basic Schneider29 Sub Add Private Sub Add(num As Integer) Dim var1 As Integer, var2 As Integer num = var1 + var2 End Sub

Chapter 4 - Visual Basic Schneider30 Form-Level Variables u Form-level variables are visible to every procedure. u Form-level variables appear at the top of the code window.

Chapter 4 - Visual Basic Schneider31 How to create Form-Level Variables? u Activate the code window u Click on the down arrow to the right of the object list box u Click on General u Click on Declaration in the procedure list box u Type in Dim statements for form-level variables

Chapter 4 - Visual Basic Schneider32 Example ' In Declaration section of General Dim num1 As Single, num2 As Single

Chapter 4 - Visual Basic Schneider33 Common Errors u Passing incorrect data types. u Not returning the result of the computation back to the calling program.

Chapter 4 - Visual Basic Schneider34 Another Example Private Sub cmdDisplay_Click() ' Demonstrate that variables in a Sub procedure do ' not retain their values in subsequent calls Call Three End Sub

Chapter 4 - Visual Basic Schneider35 Sub Three Private Sub Three() Dim num As Single ' Display the value of num and assign it the value 3 picResults.Print num; num = 3 End Sub

Chapter 4 - Visual Basic Schneider36 What is a function? u Similar to a Sub, performs a specific task u Unlike a Sub, returns a single value to the calling program

Chapter 4 - Visual Basic Schneider37 Types of Functions u Standard functions (built-in) u User-defined functions

Chapter 4 - Visual Basic Schneider38 User-Defined Function u A function returns a single value u The value is returned by assigning a value to the name of the function u The values of the parameters of a function should not be used for returning values from a function

Chapter 4 - Visual Basic Schneider39 The Function Syntax Private Function FunctionName(parameter-list) As dataType Statement(s)…… ….. FunctionName = value End Function u Note: value must be of the type dataType specified in the function declaration

Chapter 4 - Visual Basic Schneider40 Example of a Function (using a function to change from Fahrenheit to Celsius) Private Sub cmdConvert_Click() picTempC.Cls picTempC.Print FtoC(Val(txtTempF.Text)) End Sub Private Function FtoC(t As Single) As Single ‘ Convert Fahrenheit temperature to Celsius FtoC = (5 / 9) * (t - 32) End Function

Chapter 4 - Visual Basic Schneider41 Rules for Defining and Calling a Function u User-defined function must include a statement that assigns the function name a value. u User-defined functions are called in the same way that built-in functions are called. u A user-defined function may be called in an expression.

Chapter 4 - Visual Basic Schneider42 Returning Value A function can receive many values Only one value can be directly returned

Chapter 4 - Visual Basic Schneider43 Example of a Function Private Sub cmdDetermine_Click() Dim nom As String nom = FirstName(txtFullName.Text) picFirstName.Print "The first name is “; nom End Sub Private Function FirstName(nom As String) As String Dim firstSpace As Integer firstSpace = InStr(nom, " ") FirstName = Left(nom, firstSpace - 1) End Function

Chapter 4 - Visual Basic Schneider44 Common Errors u Passing incorrect data types u Not specifying the data type of the returned value u Not assigning a value to the Function name inside the Function definition u Misspelling of the Function name

Chapter 4 - Visual Basic Schneider45 Further Examples Private Sub cmdDisplay_Click() Call DisplayVolume(1, 2) Call DisplayVolume(3, 4) End Sub Private Sub DisplayVolume(r As Single, h As Single) PicOutput.Print "Volume of cylinder with base area "; Area(r) PicOutput.Print "and height"; h; "is"; h * Area(r) End Sub Private Function Area(r As Single) As Single Area = * r ^ 2 End Function

Chapter 4 - Visual Basic Schneider46 Another Example Private Sub cmdDisplay_Click() Dim a As String a = “Choo ” picOutput.Print = TypeOfTrain() End Sub Private Function TypeOfTrain() As String Dim a As String a = a & a TypeOfTrain = a & “train” End Function Output: Train

Chapter 4 - Visual Basic Schneider47 Last Example Private Sub cmdDisplay_Click() Dim num As Single num = 5 picOutput.Print Triple (num) picOutput.Print num End Sub Private Function Triple(x As Single) As Single Dim num As Single num = 3 Triple = num * x End Function Output: 15 5