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.

Slides:



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

Chapter 6, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 6 Sub 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
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight 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.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
VBA Modules, Functions, Variables, and Constants
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.
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
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.
VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime.
Procedures and Functions
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
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
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Multiple Forms and Standard Modules
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Why to Create a Procedure
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
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 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
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.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
JavaScript, Fourth Edition
Problem Solving with Decisions
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Programming with Microsoft Visual Basic th Edition
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
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.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
Object-Oriented Programming: Classes and Objects.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Sub Procedures And Functions
Visual Basic I Programming
Chapter 9: Value-Returning Functions
IS 350 Application Structure
Object-Oriented Programming: Classes and Objects
Functions Chapter 6-Part 2.
UNIT - V STORED PROCEDURE.
JavaScript: Functions
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Working with Forms in Visual Basic
Object-Oriented Programming: Classes and Objects
C++ for Engineers and Scientists Second Edition
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 4 - Visual Basic Schneider
VISUAL BASIC.
Chapter 4 void Functions
6 Chapter Functions.
Procedures and Functions
CIS16 Application Development and Programming using Visual Basic.net
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
Tonga Institute of Higher Education
Methods.
Chapter 8 - Functions and Functionality
Based on slides created by Bjarne Stroustrup & Tony Gaddis
STARTING OUT WITH Visual Basic 2008
Presentation transcript:

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 be simplified. Similar events; same process

Chapter 6 Part 13 Real-World Applications Easily have thousands of lines of code. Very difficult to modify and maintain if code is not modularized.

Chapter 6 Part 14 General Sub Procedures Reusable code that can be used in > 1 procedure. Reduces chances of typos in retyping code. Simplifies program by avoiding duplication of code. Code that is not processed solely linked to a control, such as a button. Collection of statements that performs a task, such as general calculations and routine processing. Code that is “called” from event procedures or other general procedures. Call means to “invoke the procedure.” The procedure call executes the procedure.

Chapter 6 Part 15 Event Procedure vs. General Sub Procedure Event Procedure Private Sub btnProcess_Click() Control prefix & name, underscore, event General Sub Procedure Private Sub CalcTax() Not based on control; has own procedure name. Not based on event—called from other procedures.

Chapter 6 Part 16 Declaring a Sub Procedure AccessSpecifier gives accessibility to the program Private: only accessible by other procedures declared on the same form Public: accessible by procedures that are declared in the current form and by other forms ProcedureName reflects purpose of procedure; use consistent casing. ParameterList is a list of variable values that are being passed to the sub procedure 'Sub' and 'End' are keywords AccessSpecifier Sub ProcedureName (ParameterList) [Statement(s)] End Sub

Chapter 6 Part 17 Declaration Example Private Sub DisplayGrade() Limited accessibility to current form Designated as a sub procedure Pascal casing (similar to camel casing without a three-letter prefix)

Chapter 6 Part 18 Example of VB6 Want the Spouse contribution question grayed-out and the Spouse Yes/No options disabled when these option buttons are selected: Single Married Filing Separate Return Head of Household Qualifying Widow(er)

Chapter 6 Part 19 Example If user clicks one of these options Single Married Filing Separately Head of Household Widow(er) Then program does these consistent tasks: Disables Yes/No Spouse radio buttons. Grays out Spouse options.

Chapter 6 Part 110 Redundant Code for Single, Etc. Redundant code for all four event procedures

Chapter 6 Part 111 Revised Code as Sub Procedure Shared code typed once; Various event procedures call the general procedure Code that calls (invokes) sub procedure

Chapter 6 Part 112 Enable for Joint Return

Chapter 6 Part 113 Procedure Call w/in Event Procedure Private Sub radSingle_Click() WithdrawPECSpouseQuestion End Sub Or Private Sub radSingle_Click() Call WithdrawPECSpouseQuestion End Sub The keyword Call is optional; the code works with or without it.

Chapter 6 Part 114 Execution of General Sub Procedures 1) Transfers execution from event procedure or another general procedure to the general sub procedure. 2) Executes statements in the general sub procedure. 3) Returns execution to the event procedure and resumes with the event procedure’s next statement.

Chapter 6 Part 115 Sub Procedures Can… Access class-level (form-level) variables. Have their own local variables that are… Created when the sub procedure starts. Destroyed when the sub procedure ends. Not maintained from one procedure call to the next. Declared and initialized again when called again. Maintained for static local variables. Not destroyed when the procedure ends. Exist for lifetime of application. Scope limited to procedure itself, however. Static VariableName as DataType

Chapter 6 Part 116 Sub Procedures Do Not… Destroy local variables from procedure that calls sub procedure. Event procedure and general sub procedure maintain their own local variables for their duration.

Chapter 6 Part 117 Variable Scope Event procedure variables still in scope even when call procedure; go out of scope when event procedure terminates. Sub procedure variables in scope during execution—until End Sub.

Chapter 6 Part 118 Conceptual Exercise Dim X As Integer Private Sub btnScopeQuiz_Click() Dim Y As Integer Dim Z As Integer Y = 2 Z = 5 X = Y + Z MessageBox.Show(X & Y & Z) SQuizProcedureA SQuizProcedureB MessageBox.Show(X & Y & Z) SQuizProcedureA End Sub Private Sub SQuizProcedureA() Dim X As Integer Dim Y As Integer Y = Y + 4 X = X + Y MessageBox.Show(X & Y) End Sub Private Sub SQuizProcedureB() Dim Y As Integer Dim Z As Integer Y = Y - 1 Z = 3 X = X + Y + Z MessageBox.Show(X & Y & Z) End Sub

Chapter 6 Part 119 Tutorial 6-2

Chapter 6 Part 120 Program Specs Show Grade Gets user input and stores into variables. Calculates & displays test average. Determines & displays letter grade. Drop Lowest Test Score Gets user input and stores into variables. Drops lowest score, calculates & displays test average. Determines & displays letter grade.

Chapter 6 Part 121 Need 2 Sub Procedures for Common Tasks (used for both calc grade buttons) Gets scores and places them into variables. Displays the letter grade.

Chapter 6 Part 122 Validation Procedure

Chapter 6 Part 123 Gets User Input Procedure

Chapter 6 Part 124 Determines Grade Procedure

Chapter 6 Part 125 Show Grade Event Procedure

Chapter 6 Part 126 Drop Lowest Score Event Procedure

Chapter 6 Part 127 Procedures w/ Parameters Can pass data between the calling procedure and the general procedure. Way to share data between event procedures and general procedures (or between two general procedures). Offers more flexibility than by using module-level or global variables. Global or module variables allow access to be changed whereas some procedures should not be allowed to change variable contents.

Chapter 6 Part 128 Parameter Passing The calling procedure gives (passes) a variable to the procedure. Values sent into a procedure are called arguments. The called procedure uses the variable to do its own processing. The called procedure hands the variable back to the procedure that called it.

Chapter 6 Part 129 Passing by Value A copy of the argument is passed to the procedure. The procedure uses the copy to perform tasks. The procedure does not change the original argument.

Chapter 6 Part 130 Pass by Reference A argument itself is passed to the procedure. The procedure uses an alias of the variable being passed to perform tasks. The alias in the procedure does change the original argument. The alias actually points to the memory location of the argument variable itself, thus being able to change the argument variable.

Chapter 6 Part 131 Syntax Call ProcedureName ArgumentPassed Call Rotate(MyInitials)‘variable passed Private Sub ProcedureName(ParametersDeclared) Private Sub Rotate(TwoChars As String)

Chapter 6 Part 132 Passing Multiple Arguments Call Statement ShowSum(intValue1, strYourName) Procedure Private Sub ShowSum(ByVal intNum1 as Integer, ByVal strName as String)

Chapter 6 Part 133 Passing Rules Must use same data types in argument and parameter. May use different variable names in argument and parameter. Procedure uses parameter variable names declared within the procedure