Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.

Slides:



Advertisements
Similar presentations
1 VBScript Session What we learn last session?
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Classes, Exceptions, Collections, and Scrollable Controls
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Chapter 6, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 6 Sub Procedures And Functions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Lists, Loops, Validation, and More
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 6 Procedures and Functions.
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Starting Out with Java: From Control Structures through Objects Fourth.
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
1.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
VBA Modules, Functions, Variables, and Constants
Example 2.
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modularizing Your Code with Methods.
Procedures and Functions
Apply Sub Procedures/Methods and User Defined Functions
Chapter 6 Procedures and Functions Instructor: Bindra Shrestha University of Houston – Clear Lake CSCI
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Why to Create a Procedure
5-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
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 6 Sub Procedures
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
Copyright © 2014 Pearson Education, Inc. Chapter 6 Procedures and Functions.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3 Variables and Calculations.
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,
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.
Sub Procedures And Functions
Programming Right from the Start with Visual Basic .NET 1/e
Functions Chapter 6-Part 2.
STARTING OUT WITH Visual Basic 2008
Starting Out with Java: From Control Structures through Objects
Procedures and Functions
CIS16 Application Development and Programming using Visual Basic.net
Tonga Institute of Higher Education
STARTING OUT WITH Visual Basic 2008
Presentation transcript:

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 1 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 Chapter Procedures And Functions 6 A procedure is a collection of statements that performs a task

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 3 Introduction A procedure is a collection of statements that performs a task 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 Functions work like intrinsic functions, such as CInt and IsNumeric A method can be either a procedure or a function

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedures 6.1 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 Slide 6- 5 Procedure Uses Ann event handler is a type of procedure 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: Breaking it into small, manageable pieces Performing a task that is needed repeatedly Dividing a program into a set of logical tasks

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 6 Sample Procedure, Tutorial 6-1 Sub DisplayMessage() 'A Sub procedure that displays a message. lstOutput.Items.Add("") lstOutput.Items.Add("Hello from DisplayMessage.") lstOutput.Items.Add("") End Sub Private Sub btnGo_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnGo.Click ' This procedure calls the DisplayMessage procedure. lstOutput.Items.Add("Hello from btnGo_Click procedure.") lstOutput.Items.Add("Calling the DisplayMessage " & _ "procedure.") DisplayMessage() lstOutput.Items.Add("Now I am back _ & in the btnGo_Click procedure.") End Sub Calls DisplayMessage procedure Returns to btnGo_Click

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 7 Declaring a Procedure AccessSpecifier is optional and establishes accessibility to the program Sub and End are keywords ProcedureName used to refer to procedure Use Pascal casing, capitalize 1 st character of the name and each new word in the name ParameterList is a list of variables or values being passed to the sub procedure [AccessSpecifier] Sub ProcedureName ([ParameterList]) [Statements] End Sub

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 8 More on the Access Specifier 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: Protected Friend Protected Friend These will be discussed in later chapters Access specifiers wont be used for now Practice writing procedures in Tutorial 6-2

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 9 Procedures and Static Variables Variables needed only in a procedure, should be declared within that procedure Creates a local variable with scope only within the procedure where declared 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 Static VariableName As DataType Scope is still only within the procedure But variable exists for lifetime of application

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Passing Arguments to a Procedure 6.2 When calling a procedure, you can pass it values known as arguments

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Arguments Argument – a value passed to a procedure Weve already done this with functions Value = CInt(txtInput.Text) Calls the CInt function and passes txtInput.Text as an argument A procedure must be declared with a parameter list in order to accept an argument

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Passing Arguments By Value intNumber declared as an integer argument Storage location intNumber created by procedure A value, 5 in this case, must be supplied and is copied into the storage location for intNumber The DisplayValue procedure then executes Tutorial 6-3 demonstrates passing arguments DisplayValue(5)calls DisplayValue procedure Sub DisplayValue(ByVal intNumber As Integer) ' This procedure displays a value in a message box. MessageBox.Show(intNumber.ToString) End Sub

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Passing Multiple Arguments Multiple arguments separated by commas Value of first argument is copied to first Second to second, etc. ShowSum(intValue1, intValue2)calls ShowSum procedure Sub ShowSum(ByVal intNum1 As Integer, _ ByVal intNum2 As Integer) ' This procedure accepts two arguments, and prints ' their sum on the form. Dim intSum As Integer intSum = intNum1 + intNum2 MessageBox.Show("The sum is " & intSum.ToString) End Sub

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Passing Arguments ByVal or ByRef Arguments are usually passed ByVal New storage location created for procedure Storage location gets a copy of the value Any changes in value are made to the copy Calling procedure wont see the changes Arguments can also be passed ByRef Procedure points to (references) arguments original storage location Any changes are made to the original value Calling procedure sees the changes

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley ByVal or ByRef Argument Example Tutorial 6-4 demonstrates the difference between parameters passed ByVal & ByRef Passed ByVal Calling procedure does not see changes made to the value of an argument Passed ByRef Calling procedure sees changes made to the value of an argument Slide 6- 15

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Functions 6.3 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 Slide Declaring a Function New keyword Function Also new is As DataType which states the data type of the value to be returned Return value is specified in a Return expression [AccessSpecifier] Function FunctionName ([ParameterList]) _ As DataType [Statements] End Function

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Function Call Example sngTotal = Sum(sngValue1, sngValue2) Function Sum(ByVal sngNum1 As Single, _ ByVal sngNum2 As Single) As Single Dim sngResult As Single sngResult = sngNum1 + sngNum2 Return sngResult End Function sngValue1 & sngValue2 must be data type Single Data types must agree with parameter list sngTotal must be Single, agrees with return value Tutorial 6-5 demonstrates function use

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Returning Nonnumeric Values Function IsValid(intNum As Integer) As Boolean Dim blnStatus As Boolean If intNum >= 0 And intNum <= 100 Then blnStatus = True Else blnStatus = False End If Return blnStatus End Function Function FullName(ByVal strFirst As String, _ ByVal strLast As String) As String Dim strName As String strName = strLast & ", " & strFirst Return strName End Function

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Debugging 6.4 Step Into Step Over Step Out

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Debugging Involving Procedures Step Into - continue to debug by single-stepping through a procedure Step Over - run procedure without single- stepping, 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 Building the Bagel and Coffee Price Calculator Application 6.5 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 Slide 6- 23

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Button Click Event Flowcharts Slide Calculate ButtonReset Button

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Cost Calculation Functions Slide Bagel Cost Function Topping Cost Function

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Cost Calculations Functions Slide Calc Tax FunctionCoffee Cost Function