Example 2.

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.
Introduction to C Programming
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
Introduction to Visual Basic.NET Uploaded By: M.Sheraz anjum.
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.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
An Introduction to Programming with C++ Fifth Edition
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Chapter 4 - Visual Basic Schneider
Chapter 5 - Menus, Sub Procedures, and Sub Functions  Menus - controls - properties and events –menu editor - create and change –defining menus - menu.
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,
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.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Apply Sub Procedures/Methods and User Defined Functions
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
IE 212: Computational Methods for Industrial Engineering
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
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.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
CS0004: Introduction to Programming Subprocedures and Modular Design.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
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 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
CPS120: Introduction to Computer Science Functions.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
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.
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
31/01/ Selection If selection construct.
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.
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
Controlling Program Flow with Decision Structures.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
CS0004: Introduction to Programming
Subprograms Functions Procedures.
Apply Procedures to Develop Message, Input, and Dialog Boxes
Lesson 16 Sub Procedures Lesson 17 Functions
Functions Chapter 6-Part 2.
Method.
Using Procedures and Exception Handling
Chapter 4 - Visual Basic Schneider
VBScript Session 7 Dani Vainstein.
CIS16 Application Development and Programming using Visual Basic.net
Introduction to Visual Programming
Chapter 8 - Functions and Functionality
Brief description on how to navigate within this presentation (ppt)
Presentation transcript:

Example 2

Example 2

Example 3

Example 3

Sub Procedures versus Function Procedures A Sub Procedure is a procedure that performs an action A Function Procedure may perform an action, but it also returns a value (the return value) to the point in a procedure from which it was called

Creating a ‘Function Procedure’ 2 ways to create a Function Procedure [1] simply type in the procedure definition in the code window [2] use the tools / add procedure menu command Same way as creating a sub procedures

tools / add procedure menu command

Creating a ‘Function Procedure’ The Function statement declares the name, arguments, and code that form the body of a function, as well as the data type of the value returned [Private | Public] Function Name [(arguments)] [As Data Type] statements End Function

Calling a ‘Function Procedure’ A Function Procedure is almost always used as part of an expression in a statement ReturnedValue = FunctionName( ) Remember the use of system-defined functions stUserName = Inputbox(“Enter User Name”, “User Input”)

Passing Arguments

Passing ARGUMENTS to Procedures At times there may be a need to use the value of a variable in one procedure and also in a second procedure, that is called from the first The variable could be declared at Module Level, but this makes it visible to all other procedures, and the scope of variables should always be kept as narrow as possible In order to keep the scope of the variable as narrow as possible, consider declaring the variable as Local and passing it (as an argument) to any Called Procedures

Passing ARGUMENTS to Procedures When passing arguments to procedures the ProcedureName has an argument inside the Parenthesis. This specifies that when the procedure is called, an argument must be supplied When a Procedure definition names an argument, any call to that procedure must supply an argument. Also, the argument must be of the same Data Type in both the definition and call locations The Name of the argument does not have to be the same in both locations When multiple arguments are specified in both the Procedure header and the Call of the procedure, the number of arguments, their sequence, and their data types must match in both locations

Very Important to Remember In a procedure, the argument list you enter establishes the number of arguments, their data type, and their sequence. When using multiple arguments, the sequence of the arguments is critical, just as when you see the predefined VB functions Very Important to Remember

Sub Procedures and Function Procedures A Comparison The two examples, one using Sub Procedure, and the second using Function Procedures, do exactly the same thing in relation to the outputted results, however, they are coded differently Both programs are checking to see if the user has entered a value greater than the minimum allowable loan. The value entered by the user is displayed in a text box, and whether the loan amount is “Valid” or an “Invalid” request is also printed to the form

General Declarations

Scope of Variables: General Sub Procedures Example 1

Scope of Variables: General Sub Procedures Example 1

Scope of Variables: General Sub Procedures Example 1

Scope of Variables: General Sub Procedures and Function Procedures Example 2 The user clicks on the command button

Scope of Variables: General Sub Procedures and Function Procedures Example 2

Scope of Variables: General Sub Procedures and Function Procedures Example 2 Function Name Argument Data Type of the RETURN VALUE

Scope of Variables: General Sub Procedures and Function Procedures Example 2

Scope of Variables: General Sub Procedures and Function Procedures Example 3

Scope of Variables: General Sub Procedures and Function Procedures Example 3

Scope of Variables: General Sub Procedures and Function Procedures Example 3

Scope of Variables: General Sub Procedures and Function Procedures Example 3

Scope of Variables: General Sub Procedures and Function Procedures Example 3

Example 4

Change Trivial from a sub procedure to a function procedure as demonstrated below (the code in the black rectangles has been added) Example 4

Notice what has happened to the value of X as a result of these changes Example 4

Passing Arguments ByVal and ByRef

Passing Arguments ByVal and ByRef The arglist argument has the following syntax and parts: ([ByVal | ByRef] varname ) Part ByVal Indicates that the argument is passed by value ByRef Indicates that the argument is passed by reference Varname Name of the variable representing the argument

Passing Arguments ByVal and ByRef by value (ByVal) A way of passing the value, rather than the address, of an argument to a procedure This allows the procedure to access a copy of the variable As a result, the variables actual value cannot be changed by the procedure to which it is passed

Passing Arguments ByVal and ByRef by reference (ByRef) A way of passing the address, rather than the value, of an argument to a procedure A pointer to the variable is passed to the procedure This allows the procedure to access the actual variable As a result, the variables actual value can be changed by the procedure to which it is passed

Passing Arguments ByVal and ByRef If you declare an argument without using either the ByVal or ByRef keyword, the argument will be passed by reference (ByRef) by default

Passing Arguments ByVal and ByRef A Single Argument in a SUB Procedure [1] GetNumbers (X) [2] GetNumbers X In situation [1] and [2] there is no need for the optional word CALL In situation [1] the argument X never changes If you add the keyword CALL – it does change! In situation [2] the argument X changes to the new value

Call Assess (X)

Assess X

Passing Arguments ByVal and ByRef Multiple Argument [1] Call GetNumbers (X, Y) [2] GetNumbers X, Y In situation [1] CALL is required, otherwise it is interpreted as a function In situation [1] and [2] the arguments X and Y change to the new values Need to specify ByVal for unchanged values of X and Y

Passing Arguments ByVal and ByRef Note that when you use the Call keyword, arguments must be enclosed in parentheses. If you omit the Call keyword, you must also omit the parentheses around the arguments