Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Slides:



Advertisements
Similar presentations
Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
Advertisements

Sub and Function Procedures
1.
Chapter 4 General Procedures
Example 2.
Chapter 4 - Visual Basic Schneider
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
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 6: User-Defined Functions I
Chapter 7: Sub and Function Procedures
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.
Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
Chapter 6: Functions.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Apply Sub Procedures/Methods and User Defined Functions
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
XP Chapter 7 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Enhancing User Interaction Through Programming Chapter.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Enhancing User Interaction Through Programming
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
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.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Why to Create a Procedure
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
5-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhancing the Wage Calculator Application Introducing Function Procedures and.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
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.
Sub procedures School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 6, Friday 2/21/03)
Chapter 6 Sub Procedures
VB – Debugging Tools Appendix D. Why do we need debugging? Every program has errors, and the process of finding these errors is debugging Types of errors.
CPS120: Introduction to Computer Science Functions.
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.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Introduction to Visual Basic Programming. Introduction Simple Program: Printing a Line of Text Another Simple Program: Adding Integers Memory Concepts.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Computing Higher – SD Unit - Topic 8 – Procedure and Standard Algorithms P Lynch, St Andrew’s High School Unit 2 Software Development Process Topic.
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.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
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.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Programming Right from the Start with Visual Basic .NET 1/e
A variable is a name for a value stored in memory.
Chapter 7 Top-Down Development
Functions Chapter 6-Part 2.
Method.
CSCI 161: Introduction to Programming Function
Using Procedures and Exception Handling
Microsoft Office Illustrated
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Chapter 4 - Visual Basic Schneider
Chapter 6 Sub Procedures
CS285 Introduction - Visual Basic
Function Procedures.
Topics Introduction to Functions Defining and Calling a Function
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 Creating Visual Basic Sub Procedures Creating programmer-defined Function Procedures Parameter Passing Modularizing in Programming Languages

Chapter 4 - Visual Basic Schneider3 What is Modularization Breaking the program into smaller parts A Sub procedure or Function performs a well-defined task Easier to test, debug and correct

Chapter 4 - Visual Basic Schneider4 Modularizing Programs in Visual Basic In Visual Basic, there are three types of procedures: Event Sub Function Note: To distinguish procedures from event procedures, Sub and Functions are referred to as general procedures.

Chapter 4 - Visual Basic Schneider5 Passing Arguments to Subs: 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 Schneider6 Sub Procedures Properties: may be called may be passed data called arguments may return values to the calling program may change the data stored in a received variable

Chapter 4 - Visual Basic Schneider7 Components of Sub Procedure: name: used to identify the Sub procedure parameters: a Sub procedure accepts values from the caller through its parameters; it may also send values back to the caller through it’s parameters.

Chapter 4 - Visual Basic Schneider8 Sub Procedure's Name 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: Activate a code window Select Add Procedure from the Tools menu Type in the name of the Sub procedure Click on Private in Scope frame Press the Enter key or click the OK button Type the statements of the Sub procedure into this window

Chapter 4 - Visual Basic Schneider11 Example of Call to a Sub Procedure: Private Sub cmdCompute_Click() Dim sngNum As Single sngNum = Val(InputBox("Enter a number:")) Call Triple(sngNum) 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

Chapter 4 - Visual Basic Schneider13 Passing Arguments to Sub Procedures Arguments : Variables or expressions placed in parentheses in a Call statement. 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 Parameters Variables placed in parentheses after a Sub Procedure's name. When the procedure is called, the values of the corresponding arguments are placed in the parameters.

Chapter 4 - Visual Basic Schneider15 Example of Parameters Private Sub Triple(num As Single)

Chapter 4 - Visual Basic Schneider16 Passing arguments to parameters Call Triple(num ) Private Sub Triple (num As Single) Argument Parameter

Chapter 4 - Visual Basic Schneider17 Passing Arguments to Parameters Call Add (x, y ) Private Sub Add ( num1 As Single, num2 As Single) Parameters Arguments

Chapter 4 - Visual Basic Schneider18 Passing Arguments The Sub Procedure receives the location of the arguments, the Sub Procedure may use and modify the value of the arguments.

Chapter 4 - Visual Basic Schneider19 Important Rules for Passing Arguments to a Sub The number of arguments and parameters must match. The data type of each argument must match its corresponding parameter.

Chapter 4 - Visual Basic Schneider20 Review Visual Basic has three types of procedures: Event Sub Function Each Sub procedure performs a distinct task. The Call statement causes a Sub procedure to be executed.

Chapter 4 - Visual Basic Schneider21 Review Values can be passed between the calling program and Sub by passing arguments. The number and type of arguments in the calling program and Sub must match.

Chapter 4 - Visual Basic Schneider22 Review Variables that are used in a particular Sub are local variables. Values that are assigned to them are not returned to the calling module.

Chapter 4 - Visual Basic Schneider23 Common Errors Passing incorrect data types. Not returning the result of the computation back to the calling program.

Chapter 4 - Visual Basic Schneider24 What is a function? A function designed to perform a specific task also. A function designed to return a single value to the calling program.

Chapter 4 - Visual Basic Schneider25 Types of Functions Standard functions (built-in) programmer-defined functions

Chapter 4 - Visual Basic Schneider26 programmer-defined Function A function designed to return a single value. The value is returned in the function itself. The arguments of a function should not be changed in the function body.

Chapter 4 - Visual Basic Schneider27 The Function Syntax Private Function FunctionName (parameter-list) As datatype Statement(s)…… ….. FunctionName = …….. End Function

Chapter 4 - Visual Basic Schneider28 Example of a Function (using a function to change from Fahrenheit to Celsius) 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 Schneider29 Rule for Defining and Calling a Function programmer-defined function must include a statement that assigns the function name a value. programmer-defined functions are called in the same way that built-in functions are called. A programmer-defined function may be called in an expression.

Chapter 4 - Visual Basic Schneider30 Common Errors Passing incorrect data types Not specifying the data type of the returned value Forgetting the data type of a function's parameter

Chapter 4 - Visual Basic Schneider31 Common Errors Not assigning a value to the function name inside the function definition Misspelling of the Function name Wrong invoking of the function in an expression