Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Slides:



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

Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
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
Example 2.
Chapter 4 - Visual Basic Schneider
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 41 General Procedures Sub Procedures, Part I Sub Procedures, Part II Function Procedures.
Chapter 6: Functions.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Apply Sub Procedures/Methods and User Defined Functions
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and.
CS0004: Introduction to Programming Variables – Numbers.
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.
Tutorial 11 Using and Writing Visual Basic for Applications Code
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.
Array Processing: Exercises School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 10, Friday 3/28/2003)
CS0004: Introduction to Programming Subprocedures and Modular Design.
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.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
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.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
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.
Chapter 8 - Visual Basic Schneider
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
Introduction to Visual Basic Programming. Introduction Simple Program: Printing a Line of Text Another Simple Program: Adding Integers Memory Concepts.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Arrays and others. Annoucement Today’s office hour move to Friday 1:00PM to 3:00PM Today’s office hour move to Friday 1:00PM to 3:00PM Today Today  Call.
1Edited By Maysoon Al-Duwais. 2 General Procedures Function Procedure Sub Procedures, Part I Sub Procedures, Part II Modular Design Edited By Maysoon.
Sub Procedures; Passing Values Back From Sub Procedures Passing by reference Passing by value.
Controlling Program Flow with Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
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.
Making Interactive Programs with Visual Basic .NET
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
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.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
1Lect7 GC20111/2/ General Procedures Function Procedure Sub Procedures, Part I Sub Procedures, Part II Modular Design Lect7 GC20111/2/2015.
Subprograms Functions Procedures.
A variable is a name for a value stored in memory.
Arithmetic Expressions Function Calls Output
Variables and Arithmetic Operations
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
Chapter 4 - Visual Basic Schneider
Chapter 6 Sub Procedures
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 Private Sub Triple(par As Single) ' Multiply the value of the number by 3 picResult.Print "The number is"; 3 * par 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, Reading the value from a text box) 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 Example of a Function (using a function to change from Fahrenheit to Celsius, Reading the value from an input box) Private Sub cmdConvert_Click() picTempC.Cls picTempC.Print FtoC(Val(InputBox("please enter a value to convert"))) 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 Schneider42 Example of a Sub (using a sub to change from Fahrenheit to Celsius, Reading the value from an input box) notice the difference from the previous slide. Private Sub cmdConvert_Click() Dim c As Single picTempC.Cls Call FtoC(Val(InputBox("please enter a value to convert")), c) picTempC.Print c End Sub Private Sub FtoC(f As Single, c As Single) ' Convert Fahrenheit temperature to Celsius c = (5 / 9) * (f - 32) End Sub

Chapter 4 - Visual Basic Schneider43 Example of a Sub (using a sub to change from Fahrenheit to Celsius, Reading the value from an input box) This example will not give a correct result because we used ( ) around c in the argument list Private Sub cmdConvert_Click() Dim c As Single picTempC.Cls Call FtoC(Val(InputBox("please enter a value to convert")), (c)) picTempC.Print c End Sub Private Sub FtoC(f As Single, c As Single) ' Convert Fahrenheit temperature to Celsius c = (5 / 9) * (f - 32) End Sub

Chapter 4 - Visual Basic Schneider44 Example of a Sub (using a sub to change from Fahrenheit to Celsius, Reading the value from an input box) This example will not give a correct result because we used ByVal in the parameter list Private Sub cmdConvert_Click() Dim c As Single picTempC.Cls Call FtoC(Val(InputBox("please enter a value to convert")), c) picTempC.Print c End Sub Private Sub FtoC(f As Single, ByVal c As Single) ' Convert Fahrenheit temperature to Celsius c = (5 / 9) * (f - 32) End Sub

Chapter 4 - Visual Basic Schneider45 Example of a Sub (using a sub to change from Fahrenheit to Celsius, Reading the value from an input box) This example will give a correct result because we used ByRef in the parameter list (removing ByRef will have the same effect) Private Sub cmdConvert_Click() Dim c As Single picTempC.Cls Call FtoC(Val(InputBox("please enter a value to convert")), c) picTempC.Print c End Sub Private Sub FtoC(f As Single, ByRef c As Single) ' Convert Fahrenheit temperature to Celsius c = (5 / 9) * (f - 32) End Sub

Chapter 4 - Visual Basic Schneider46 Example of a Sub (using a sub to change from Fahrenheit to Celsius, Reading the value from an input box) what about this?! Private Sub cmdConvert_Click() Dim c As Single picTempC.Cls Call FtoC(Val(InputBox("please enter a value to convert")), c) picTempC.Print c End Sub Private Sub FtoC(ByVal f As Single, c As Single) ' Convert Fahrenheit temperature to Celsius c = (5 / 9) * (f - 32) End Sub

Chapter 4 - Visual Basic Schneider47 Example of a Sub (using a sub to change from Fahrenheit to Celsius, Reading the value from an input box) what about this too?! Private Sub cmdConvert_Click() Dim c As Single Dim f As Single picTempC.Cls f=Val(InputBox("please enter a value to convert")) Call FtoC( f, c) picTempC.Print “The Celsius value of the Fahrenheit ” ; f ; “ is ” ; c End Sub Private Sub FtoC(ByVal f As Single, ByRef c As Single) ' Convert Fahrenheit temperature to Celsius c = (5 / 9) * (f - 32) End Sub

Chapter 4 - Visual Basic Schneider48 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 Schneider49 Returning Value A function can receive many values Only one value can be directly returned

Chapter 4 - Visual Basic Schneider50 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 Schneider51 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 Schneider52 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 Schneider53 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 Schneider54 Another 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

Chapter 4 - Visual Basic Schneider55 ByRef and ByVal Examples Private Sub cmdCall_Click() Dim a As Integer Dim b As Integer a = 10 b = 15 Print a; b Call mySub(a, b) Print a; b End Sub Sub mySub(x As Integer, y As Integer) x = 11 y = 16 End Sub Output

Chapter 4 - Visual Basic Schneider56 ByRef and ByVal Examples Private Sub cmdCall_Click() Dim a As Integer Dim b As Integer a = 10 b = 15 Print a; b Call mySub(a, b) Print a; b End Sub Sub mySub(ByRef x As Integer, ByRef y As Integer) x = 11 y = 16 End Sub Output

Chapter 4 - Visual Basic Schneider57 ByRef and ByVal Examples Private Sub cmdCall_Click() Dim a As Integer Dim b As Integer a = 10 b = 15 Print a; b Call mySub(a, b) Print a; b End Sub Sub mySub(ByRef x As Integer, ByVal y As Integer) x = 11 y = 16 End Sub Output

Chapter 4 - Visual Basic Schneider58 ByRef and ByVal Examples Private Sub cmdCall_Click() Dim a As Integer Dim b As Integer a = 10 b = 15 Print a; b Call mySub(a, b) Print a; b End Sub Sub mySub(ByVal x As Integer, ByVal y As Integer) x = 11 y = 16 End Sub Output 10 15

Chapter 4 - Visual Basic Schneider59 ByRef and ByVal Examples Private Sub cmdCall_Click() Dim a As Integer Dim b As Integer a = 10 b = 15 Print a; b Call mySub(a, b) Print a; b End Sub Sub mySub(ByRef y As Integer, ByRef x As Integer) x = 11 y = 16 End Sub Output

More Arithmetic Operators Chapter 4 - Visual Basic Schneider60 u (num1 \ num2) is the Quotient of num1 divided by num2 u (num1 Mod num2) is the whole number remainder of num1 divided by num2 17 / 2 = \ 2 = 817 Mod 2 = 1 16 / 2 = 816\ 2 =816 Mod 2 = 0 34/7= \ 7 = 434 Mod 7 = 6

Mod Rules Chapter 4 - Visual Basic Schneider61 u The whole number remainder u If the numbers are single they are rounded first u If one or both of a and b are negative the result of the Mod will have the sign of “a” u num1 Mod 1 = 0 u num1 Mod num1 = 0 u Num Mod 0 Division by zero u Result of Mod should be less than num2

Mod Examples Chapter 4 - Visual Basic Schneider62 25 mod mod mod mod mod mod 3.2 = 4 = 1 = -3 = 3 = mod -3.2 = mod -3.2 = 1 3 mod 3 = 0 3 mod -3 = 0 -3 mod 3 = 0 -3 mod -3 = 0 3 mod 1 = 0 -3 mod 1 = 0

Operators Precedence Chapter 4 - Visual Basic Schneider63 u Precedence 1.() 2.^ 3.* 4./ 5.\ 6.mod

2 Chapter 4 - Visual Basic Schneider64 Private Sub cmd_Click() Dim a As Integer Dim b As Integer a = 14 b = 5 picOutput.Print a / b \ b End Sub Output: Infinity /Illegal Division

2. Chapter 4 - Visual Basic Schneider65 Private Sub cmd_Click() Dim a As Integer Dim b As Integer a = 14 b = 5 picOutput.Print a mod b \ b End Sub Output: Infinity /Illegal Division

Chapter 4 - Visual Basic Schneider66 More Examples Private Sub cmd_Click() Dim a As Integer Dim b As Integer a = 14 b = 5 Print a / b Print a \ b Print a Mod b End Sub Output: