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

Slides:



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

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
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
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.
0 - 0.
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
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Starting Out with Java: From Control Structures through Objects Fourth.
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.
VBA Modules, Functions, Variables, and Constants
Example 2.
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.
Chapter 7: Sub and Function Procedures
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modularizing Your Code with Methods.
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.
Procedures and Functions
Apply Sub Procedures/Methods and User Defined Functions
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Chapter 6 Procedures and Functions Instructor: Bindra Shrestha University of Houston – Clear Lake CSCI
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
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.
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.
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.
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.
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.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Procedures Subs and Functions. Procedures Before OOP, subroutines were the primary high-level way to organize a program. In OOP, this role has been taken.
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.
Chapter 5 Menus, Sub Procedures and Sub Functions Programming In Visual Basic.NET.
HNDIT Rapid Application Development
Sub Procedures; Passing Values Back From Sub Procedures Passing by reference Passing by value.
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
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.
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.
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 and Functions Visual Basic. Sub Procedures Slide 2 of 26 Topic & Structure of the lesson Introduction to Modular Design Concepts Write.
Lecture 7 Methods (functions and subroutines) Parameter Passing
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
Royal University of Phnom Penh
Functions Chapter 6-Part 2.
Object-Oriented Programming: Classes and Objects
Starting Out with Java: From Control Structures through Objects
Procedures and Functions
CIS16 Application Development and Programming using Visual Basic.net
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Tonga Institute of Higher Education
STARTING OUT WITH Visual Basic 2008
Presentation transcript:

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

Chapter 6, Slide 2Starting Out with Visual Basic 3 rd Edition Chapter 6 Introduction A procedure is a collection of statements that performs a task.

Chapter 6, Slide 3Starting Out with Visual Basic 3 rd Edition Chapter 6 Topics A Sub procedure is a collection of statements that performs a task An abbreviation of the older term subroutine Event procedures are Sub procedures A Function procedure is a collection of statements that performs a task and returns a value to the VB statement that executed it Function procedures work like intrinsic functions, such as Val and IsNumeric A method is a procedure declared in a class

Chapter 6, Slide 4Starting Out with Visual Basic 3 rd Edition Section 6.1 Sub Procedures You Can Write Your Own General Purpose Sub Procedures That Perform Specific Tasks General Purpose Sub Procedures Are Not Triggered by Events but Called From Statements in Other Procedures

Chapter 6, Slide 5Starting Out with Visual Basic 3 rd Edition Sub Procedure Uses May handle events such as a click event Also used to simplify a program by Breaking it into small, manageable pieces or Performing a task that is needed repeatedly Sub procedures help to modularize code Divides a program into a set of logical tasks

Chapter 6, Slide 6Starting Out with Visual Basic 3 rd Edition Sample Sub 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

Chapter 6, Slide 7Starting Out with Visual Basic 3 rd Edition Declaring a Sub 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]) [Statement(s)] End Sub

Chapter 6, Slide 8Starting Out with Visual Basic 3 rd Edition More on Access Specifier Private allows use only from that form or class Public allows use from other forms or classes If not specified, default is Public Additional access specifiers: 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

Chapter 6, Slide 9Starting Out with Visual Basic 3 rd Edition Procedures and Static Variables Variables needed only in a Sub procedure, should be declared within the Sub procedure Creates a local variable with scope only within the sub procedure where declared Local variable values are not saved from one sub 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 only within the procedure But variable exists for lifetime of procedure

Chapter 6, Slide 10Starting Out with Visual Basic 3 rd Edition Section 6.2 Passing Arguments to a Sub Procedure When calling a procedure, you can pass it values known as arguments

Chapter 6, Slide 11Starting Out with Visual Basic 3 rd Edition Arguments Argument – a value passed to a procedure Weve already done this with functions Value = CInt(txtInput.Text) Calls CInt function and passes txtInput.Text A Sub must be declared so it accepts an argument

Chapter 6, Slide 12Starting Out with Visual Basic 3 rd Edition Passing Arguments By Value Number declared as an integer argument Storage location number created A value, 5 in this case, must be supplied and is copied into the storage location for number DisplayValue then executes Tutorial 6-3 demonstrates passing arguments DisplayValue(5)calls DisplayValue procedure Sub DisplayValue(ByVal number As Integer) ' This procedure displays a value in a message box. MessageBox.Show(number.ToString) End Sub

Chapter 6, Slide 13Starting Out with Visual Basic 3 rd Edition Passing Multiple Arguments Multiple arguments separated by commas Value of first argument is copied to first Second to second, etc. ShowSum(5, 10)calls ShowSum procedure Sub ShowSum(ByVal num1 As Integer, ByVal num2 As Integer) ' This procedure accepts two arguments, and prints ' their sum on the form. Dim sum As Integer sum = num1 + num2 MessageBox.Show("The sum is " & sum.ToString) End Sub

Chapter 6, Slide 14Starting Out with Visual Basic 3 rd Edition 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 Tutorial 6-4 demonstrates this difference

Chapter 6, Slide 15Starting Out with Visual Basic 3 rd Edition Section 6.3 Function Procedures A Function Procedure Returns a Value to the Part of the Program That Called the Function Procedure

Chapter 6, Slide 16Starting Out with Visual Basic 3 rd Edition Declaring a Function Procedure 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

Chapter 6, Slide 17Starting Out with Visual Basic 3 rd Edition Function Call Example total = Sum(value1, value2) Function Sum(ByVal num1 As Single, ByVal num2 As Single) _ As Single Dim result As Single result = num1 + num2 Return result End Function value1 and value2 must be data type Single Total must be declared as data type Single Tutorial 6-5 demonstrates function use

Chapter 6, Slide 18Starting Out with Visual Basic 3 rd Edition Returning Nonnumeric Values Function IsValid(num As Integer) As Boolean Dim status As Boolean If num >= 0 And num <= 100 Then status = True Else status = False End If Return status End Function Function FullName(ByVal first As String, ByVal last As String)_ As String Dim name As String name = last & ", " & first Return name End Function

Chapter 6, Slide 19Starting Out with Visual Basic 3 rd Edition Section 6.4 More About Debugging Step Into Step Over Step Out

Chapter 6, Slide 20Starting Out with Visual Basic 3 rd Edition 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

Chapter 6, Slide 21Starting Out with Visual Basic 3 rd Edition Section 6.5 Building the Bagel and Coffee Price Calculator Application Use Sub procedures and functions to calculate the total of a customer order.