STARTING OUT WITH Visual Basic 2008 FOURTH EDITION
STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis Haywood Community College Kip Irvine Florida International University Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6 - 1
Chapter 6 Procedures And Functions A procedure is a collection of statements that performs a task Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Introduction n A procedure is a collection of statements that performs a task n Event handlers are a type of procedure A function is a collection of statements that performs a task and returns a value to the VB statement that executed it n Functions work like intrinsic functions, such as CInt and Is. Numeric A method can be either a procedure or a function Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6 - 3
6. 1 Procedures You Can Write Your Own General Purpose Procedures That Perform Specific Tasks General Purpose Procedures Are Not Triggered by Events but Called From Statements in Other Procedures Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Procedure Uses n n n Ann event handler is a type of procedure n Automatically executed when an event such as a mouse click occurs General purpose procedures are triggered by statements in other procedures, not by events Procedures help simplify & modularize code by: n Breaking it into small, manageable pieces n Performing a task that is needed repeatedly n Dividing a program into a set of logical tasks Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6 - 5
Sample Procedure, Tutorial 6 -1 Private Sub btn. Go_Click(By. Val sender As System. Object, _ By. Val e As System. Event. Args) Handles btn. Go. Click ' This procedure calls the Display. Message procedure. lst. Output. Items. Add("Hello from btn. Go_Click procedure. ") lst. Output. Items. Add("Calling the Display. Message " & _ "procedure. ") Display. Message() lst. Output. Items. Add("Now I am back ” _ & “in the btn. Go_Click procedure. ") End Sub Returns to btn. Go_Click Calls Display. Message procedure Sub Display. Message() 'A Sub procedure that displays a message. lst. Output. Items. Add("") lst. Output. Items. Add("Hello from Display. Message. ") lst. Output. Items. Add("") End Sub Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6 - 6
Declaring a Procedure [Access. Specifier] Sub Procedure. Name ([Parameter. List]) [Statements] End Sub n n Access. Specifier is optional and establishes accessibility to the program Sub and End are keywords Procedure. Name used to refer to procedure st n Use Pascal casing, capitalize 1 character of the name and each new word in the name Parameter. List is a list of variables or values being passed to the sub procedure Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6 - 7
More on the Access Specifier n n n Private allows use only from that form Public allows use from other forms If not specified, default is Public There are other access specifiers such as: n Protected n Friend n Protected Friend n These will be discussed in later chapters Access specifiers won’t be used for now Practice writing procedures in Tutorial 6 -2 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6 - 8
Procedures and Static Variables n n Variables needed only in a procedure, should be declared within that procedure n Creates a local variable with scope only within the procedure where declared n Local variable values are not saved from one procedure call to the next To save value between procedure calls, use Static keyword to create a static local variable n n n Static Variable. Name As Data. Type Scope is still only within the procedure But variable exists for lifetime of application Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6 - 9
6. 2 Passing Arguments to a Procedure When calling a procedure, you can pass it values known as arguments Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Arguments n n Argument – a value passed to a procedure We’ve already done this with functions n Value = CInt(txt. Input. Text) Calls the CInt function and passes txt. Input. Text as an argument A procedure must be declared with a parameter list in order to accept an argument n n Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6 - 11
Passing Arguments By Value Display. Value(5) ‘calls Display. Value procedure Sub Display. Value(By. Val int. Number As Integer) ' This procedure displays a value in a message box. Message. Box. Show(int. Number. To. String) End Sub n n n int. Number declared as an integer argument Storage location int. Number created by procedure A value, 5 in this case, must be supplied and is copied into the storage location for int. Number The Display. Value procedure then executes Tutorial 6 -3 demonstrates passing arguments Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6 - 12
Passing Multiple Arguments Show. Sum(int. Value 1, int. Value 2) ‘calls Show. Sum procedure Sub Show. Sum(By. Val int. Num 1 As Integer, _ By. Val int. Num 2 As Integer) ' This procedure accepts two arguments, and prints ' their sum on the form. Dim int. Sum As Integer int. Sum = int. Num 1 + int. Num 2 Message. Box. Show("The sum is " & int. Sum. To. String) End Sub n n n Multiple arguments separated by commas Value of first argument is copied to first Second to second, etc. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6 - 13
Passing Arguments By. Val or By. Ref n n Arguments are usually passed By. Val n New storage location created for procedure n Storage location gets a copy of the value n Any changes in value are made to the copy n Calling procedure won’t “see” the changes Arguments can also be passed By. Ref n Procedure points to (references) argument’s original storage location n Any changes are made to the original value n Calling procedure “sees” the changes Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6 - 14
By. Val or By. Ref Argument Example n Tutorial 6 -4 demonstrates the difference between parameters passed By. Val & By. Ref n Passed By. Val n Calling procedure does not “see” changes made to the value of an argument n Passed By. Ref n Calling procedure “sees” changes made to the value of an argument Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6 - 15
6. 3 Functions A Function Returns a Value to the Part of the Program That Called the Function Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Declaring a Function [Access. Specifier] Function. Name ([Parameter. List]) _ As Data. Type [Statements] End Function n New keyword Function Also new is As Data. Type which states the data type of the value to be returned Return value is specified in a Return expression Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6 - 17
Function Call Example sng. Total = Sum(sng. Value 1, sng. Value 2) Function Sum(By. Val sng. Num 1 As Single, _ By. Val sng. Num 2 As Single) As Single Dim sng. Result As Single sng. Result = sng. Num 1 + sng. Num 2 Return sng. Result End Function n sng. Value 1 & sng. Value 2 must be data type Single n Data types must agree with parameter list sng. Total must be Single, agrees with return value Tutorial 6 -5 demonstrates function use Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6 - 18
Returning Nonnumeric Values Function Full. Name(By. Val str. First As String, _ By. Val str. Last As String) As String Dim str. Name As String str. Name = str. Last & ", " & str. First Return str. Name End Function Is. Valid(int. Num As Integer) As Boolean Dim bln. Status As Boolean If int. Num >= 0 And int. Num <= 100 Then bln. Status = True Else bln. Status = False End If Return bln. Status End Function Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6 - 19
6. 4 More About Debugging Step Into Step Over Step Out Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Debugging Involving Procedures n n Step Into - continue to debug by single-stepping through a procedure Step Over - run procedure without singlestepping, continue single-step after the call Step Out - end single-stepping in procedure, continue single-step after the call Tutorial 6 -6 provides examples Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6 - 21
6. 5 Building the Bagel and Coffee Price Calculator Application Use procedures and functions to calculate the total of a customer order. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Bagel and Coffee Price Calculator Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6 - 23
Button Click Event Flowcharts Calculate Button Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Reset Button Slide 6 - 24
Cost Calculation Functions Bagel Cost Function Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Topping Cost Function Slide 6 - 25
Cost Calculations Functions Coffee Cost Function Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Calc Tax Function Slide 6 - 26
- Slides: 26