Sub Procedures; Passing Values Back From Sub Procedures Passing by reference Passing by value.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 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.
Sub and Function Procedures
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
Passing Arguments Question Example: IS1102 Exam Autumn 2001.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
VBA Modules, Functions, Variables, and Constants
Example 2.
Sub Programs To Solve a Problem, First Make It Simpler.
Chapter 4 - Visual Basic Schneider
Chapter 4 (cont) Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
Chapter 41 Sub Procedures, Part II Passing by Value Passing by Reference Local Variables Class-Level Variables Debugging.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Chapter 41 Sub Procedures, Part II (Continue). Chapter 42 Local Variable A variable declared inside a Sub procedure with a Dim statement Space reserved.
Introduction to Computing Dr. Nadeem A Khan. Lecture 18.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Chapter 41 General Procedures Sub Procedures, Part I Sub Procedures, Part II Function Procedures.
VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Apply Sub Procedures/Methods and User Defined Functions
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
IE 212: Computational Methods for Industrial Engineering
1 VBA – podstawowe reguły języka Opracowanie Janusz Górczyński wg Microsoft Help.
Why to Create a Procedure
Array Processing: Exercises School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 10, Friday 3/28/2003)
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
CS0004: Introduction to Programming Subprocedures and Modular Design.
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
Sub procedures School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 6, Friday 2/21/03)
Subprograms CE 311 K - Introduction to Computer Methods Daene C. McKinney.
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.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition.
Tutorial 6 The Repetition Structure
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.
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.
Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -
Repetition Structures
Visual Basic Programming I 56:150 Information System Design.
Arrays and others. Annoucement Today’s office hour move to Friday 1:00PM to 3:00PM Today’s office hour move to Friday 1:00PM to 3:00PM Today Today  Call.
Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable.
Chapter 8: Advanced Method Concepts. Understanding Parameter Types Mandatory parameter – An argument for it is required in every method call Four types.
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
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.
More on Variables and Subroutines. Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
CSC 162 Visual Basic I Programming. Storage Classes Determines the “lifetime” of an identifier Types: –Automatic Default Memory is allocated for the variable.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
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
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
Sub Procedures And Functions
Object-Oriented Programming: Classes and Objects
Functions Chapter 6-Part 2.
Function There are two types of Function User Defined Function
Method.
Object-Oriented Programming: Classes and Objects
Chapter 4 - Visual Basic Schneider
5.2 Sub Procedures, Part I Defining and Calling Sub Procedures
Procedures: Functions and Subroutines
Intro to Programming Concepts
Presentation transcript:

Sub Procedures; Passing Values Back From Sub Procedures Passing by reference Passing by value

Passing Arguments By Reference Passing arguments by reference gives the procedure access to the actual variable contents in its memory address location. The variable's value can be permanently changed by the procedure to which it is passed. Passing by reference is the default in Visual Basic.

Private Sub cmdDisplay_Click() Dim amt As Single 'Illustrate effect of value of parameter on value of argument picResults.Cls amt = 2 picResults.Print amt; Call Triple(amt) picResults.Print amt End Sub Private Sub Triple(num As Single) 'Triple a number picResults.Print num; num = 3 * num picResults.Print num; End Sub

Private Sub cmdCompute_Click() Dim x As Single, y As Single 'Display the sum of the two numbers Call GetNumbers(x, y) Call Add(x, y) End Sub Private Sub Add(num1 As Single, num2 As Single) Dim sum As Single 'Display numbers and their sum picResult.Cls sum = num1 + num2 picResult.Print "The sum of"; num1; "and"; num2; "is"; sum End Sub Private Sub GetNumbers(num1 As Single, num2 As Single) 'Record the two numbers in the text boxes num1 = Val(txtFirstNum.Text) num2 = Val(txtSecondNum.Text) End Sub

Passing Arguments By Value Only a copy of a variable is passed when an argument is passed by value. If the procedure changes the value, the change affects only the copy and not the variable itself. Use the ByVal keyword to indicate an argument passed by value. Sub PostAccounts(ByVal intAcctNum As Integer).. ' Place statements here.. End Sub

Private Sub cmdCompute_Click() Dim x As Single, y As Single 'Display the sum of the two numbers Call GetNumbers(x, y) Call Add(x, y) End Sub Private Sub Add(ByVal num1 As Single, ByVal num2 As Single) Dim sum As Single 'Display numbers and their sum picResult.Cls sum = num1 + num2 picResult.Print "The sum of"; num1; "and"; num2; "is"; sum End Sub Private Sub GetNumbers(ByVal num1 As Single, ByVal num2 As Single) 'Record the two numbers in the text boxes num1 = Val(txtFirstNum.Text) num2 = Val(txtSecondNum.Text) End Sub

Scope of Variables; Procedure-level Variables The scope of a variable defines which parts of your code are aware of its existence. Procedure-level variables are recognized only in the procedure in which they're declared. These are also known as local variables. You declare procedure-level variables with the Dim or Static keywords: Dim intTemp As Integer Static intPermanent As Integer

Private Sub cmdDisplay_Click() Dim x As Single x = 2 picResults.Print x; Call Trivial picResults.Print x; Call Trivial picResults.Print x; End Sub Private Sub Trivial() Dim x As Single 'Do something trivial picResults.Print x; x = 3 picResults.Print x; End Sub

Private Sub cmdDisplay_Click() Dim x As Single x = 2 picResults.Print x; Call Trivial picResults.Print x; Call Trivial picResults.Print x; End Sub Private Sub Trivial() Static x As Single 'Do something trivial picResults.Print x; x = 3 picResults.Print x; End Sub

Form-level Variables A form-level variable is available to all procedures in the form. You create form-level variables by declaring them with the Private keyword in the Declarations section at the top of the form. When a form-level variable is assigned a value in a procedure, it retains that value when the procedure is exited.

Dim num1 As Single, num2 As Single Private Sub cmdDisplay_Click() 'Display the sum of two numbers num1 = 2 num2 = 3 picResults.Cls Call AddAndIncrement picResults.Print picResults.Print "num1 = "; num1 picResults.Print "num2 = "; num2 End Sub Private Sub AddAndIncrement() 'Display numbers and their sum picResults.Print "The sum of"; num1; "and"; num2; "is"; num1 + num2 num1 = num1 + 1 num2 = num2 + 1 End Sub