1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.

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 Procedural Programming Paradigm Stacks and Procedures.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
Chapter 4 General Procedures
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Example 2.
CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.
Chapter 4 (cont) Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
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 41 Sub Procedures, Part II Passing by Value Passing by Reference Local Variables Class-Level Variables Debugging.
Introduction to Computing Dr. Nadeem A Khan. Lecture 18.
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.
Subroutines and Functions Chapter 6. Introduction So far, most of the code has been inside a single method for an event –Fine for small programs, but.
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.
Why to Create a Procedure
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.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
Local Variables A local variable is a variable that is declared within a method declaration. Local variables are accessible only from the method in which.
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
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.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
Subroutines and Functions. Introduction So far, most of the code has been inside a single method for an event –Fine for small programs, but inconvenient.
CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that.
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.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
Chapter 8: Advanced Method Concepts. Understanding Parameter Types Mandatory parameter – An argument for it is required in every method call Four types.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
1Edited By Maysoon Al-Duwais. 2 General Procedures Function Procedure Sub Procedures, Part I Sub Procedures, Part II Modular Design Edited By Maysoon.
Sub Procedures; Passing Values Back From Sub Procedures Passing by reference Passing by value.
Week 12 Methods for passing actual parameters to formal parameters.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
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,
1Edited By Maysoon Al-Duwais. 2 Devices for Modularity Visual Basic has two ways for breaking problems into smaller pieces: Sub procedures Function procedures.
1Lect7 GC20111/2/ General Procedures Function Procedure Sub Procedures, Part I Sub Procedures, Part II Modular Design Lect7 GC20111/2/2015.
Sub Procedures And Functions
Subprograms Functions Procedures.
Chapter 10: Void Functions
Functions Chapter 6-Part 2.
Chapter 5 Functions DDC 2133 Programming II.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Chapter#8 General Procedures
5.2 Sub Procedures, Part I Defining and Calling Sub Procedures
6.12 Default Arguments.
Functions, Part 2 of 2 Topics Functions That Return a Value
6 Chapter Functions.
Array and Method.
Chapter#8 General Procedures
Chapter 5 - General Procedures
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
Simulating Reference Parameters in C
Chapter 5 - General Procedures
Procedures: Functions and Subroutines
Corresponds with Chapter 5
10.2 Procedures Passing Parameters 30/08/2019.
Introducing Modularity
C Parameter Passing.
Presentation transcript:

1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging

2 ByVal and ByRef Parameters in Sub procedure headers are proceeded by ByVal or ByRef ByVal stands for By Value ByRef stands for By Reference

3 Passing by Value When a variable argument is passed to a ByVal parameter, a copy of the value of the argument is passed. After the Sub procedure terminates, the original value of the actual parameter that was passed is unchanged, even if the procedure changes the formal parameter value.

4 Passing by Reference When a variable argument is passed to a ByRef parameter, the actual parameter (argument) and the formal parameter are one and the same…they are the same location in memory. After the Sub procedure terminates, the original value of the actual parameter that was passed will be changed when the formal parameter is changed.

5 Example Sub procedure has a ByVal parameter.

6 About to call. Note the value in variable amt, the actual parameter (argument).

7 In the called Sub procedure. Formal parameter num has a copy of actual parameter amt.

8 Formal parameter num’s value has changed.

9 But the value in amt remains the same. This is because amt and val are two different memory locations.

10 Therefore, any change to a ByVal formal parameter has no effect on its associated actual parameter.

11 Example (modified) Let’s try the Sub procedure with a ByRef parameter.

12 About to call. Note the value in variable amt, the actual parameter (argument).

13 In the called Sub procedure. Formal parameter num is identical to actual parameter amt.

14 Formal parameter num’s value has changed.

15 Since num is a ByRef parameter, the value in amt changes also. This is because amt and val are the same memory location.

16 Therefore, any change to a ByRef formal parameter remains in the associated actual parameter after the called procedure terminates.

17 Example 5.3.2

18 Example Sub (or Function) procedures with ByRef parameters can be used to “return” values. If you want a procedure to “return” more than one value, you can use ByRef parameters for this.

Example

Example If you want to swap the values in two variables, you need to have a temporary variable.