10.2 Procedures Passing Parameters 30/08/2019.

Slides:



Advertisements
Similar presentations
Sub and Function Procedures
Advertisements

1 Procedural Programming Paradigm Stacks and Procedures.
30/04/ Selection Nested If structures & Complex Multiple Conditions.
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
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.
VBA Modules, Functions, Variables, and Constants
Example 2.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
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 7: Sub and Function Procedures
VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime.
8.2 Procedures Function Procedures 20/04/2017.
08/09/ Arrays Defining, Declaring & Processing.
Multiple Forms and Standard Modules
Visual Basic I Programming
Why to Create a Procedure
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Tutorial 111 The Visual Studio.NET Environment The major differences between Visual Basic 6.0 and Visual Basic.NET are the latter’s support for true object-oriented.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
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.
27/05/ Iteration Loops Nested Loops & The Step Parameter.
30/10/ Iteration Loops Do While (condition is true) … Loop.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
22/11/ Selection If selection construct.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
1 Advanced Computer Programming Lab Calculator Project.
COPYRIGHT 2010: Dr. David Scanlan, CSUS OBJECTIVES: How to write classes How to create an object from a class using the "New" keyword. How to communicate.
Top-down approach / Stepwise Refinement & Procedures & Functions.
Programming with Microsoft Visual Basic th Edition
31/01/ Selection If selection construct.
04/02/ Procedures Top-down approach / Stepwise Refinement & Sub Procedures.
1 4.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works. Directly testing if text boxes.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
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,
Visual Basic I Programming
Introduction to Computers
Chapter 9: Value-Returning Functions
A variable is a name for a value stored in memory.
User-Written Functions
COMPUTATIONAL CONSTRUCTS
ASSIGNMENT OBJECTIVES
Administrator Training
User-Defined Functions
Introduction to Computers
STARTING OUT WITH Visual Basic 2008
Top-down approach / Stepwise Refinement & Sub Procedures
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Programming paradigms
Data Types & File Size Calculations
If selection construct
Do While (condition is true) … Loop
PROGRAMMING Sub Procedures I.
Introduction to Visual Programming
If selection construct
Do … Loop Until (condition is true)
Nested Loops & The Step Parameter
Tonga Institute of Higher Education
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Procedures: Functions and Subroutines
Chapter 8 - Functions and Functionality
10.3 Procedures Function Procedures 07/06/2019.
3.2 Working with Data Scope of variables 29/07/2019.
Presentation transcript:

10.2 Procedures Passing Parameters 30/08/2019

Learning Objectives Define parameters. Define passing in relation to parameters. Define actual, formal, value and reference parameters. State how to declare value and reference parameters. 30/08/2019

Variables When a program runs, the results to any calculations it performs are stored in RAM. The results to these calculations will be lost as soon as the computer starts doing something else. A variable is a name made up by a programmer to reserve and identify an address in RAM where data can be stored as long as the program runs.

Scope of a variable Location declared Level Description Within a procedure. Local scope Available only within the module in which it is declared Outside a procedure. Global scope Available to all modules.

Narrow scope It is good programming practice and efficient to declare variables with as narrow a scope as possible. Certain errors/problems can occur if you don’t: Name Conflict Avoidance – several different procedures can use the same variable name/identifier. As long as each instance is declared as a (local) procedure variable, each procedure recognizes only its own version of the variable. Memory Consumption – (Local) Procedure variables consume memory only while their procedure is running. Their memory is released when the procedure finishes. (Global) Module variables obviously use memory until the module finishes i.e. longer.

How do we find the area of a 'house' made up from a square and a triangle? 30/08/2019

AreaTriangle = ½ * TriangleHeight * TriangleBase Sub Procedures: CalcTriangleArea TriangleHeight Main Program TriangleBase AreaTriangle = ½ * TriangleHeight * TriangleBase Input TriangleHeight Input TriangleBase Input RectangleWidth Input RectangleLength CalcTriangleArea CalcRectangleArea CalcHouseArea Output HouseArea TriangleArea TriangleArea RectangleLength CalcAreaRectangle RectangleWidth AreaRectangle = RectangleWidth * RectangleBreadth RectangleArea RectangleArea AreaRectangle CalcHouseArea AreaTriangle HouseArea HouseArea = AreaTriangle + AreaRectangle Parameters – see the next few slides for details. HouseArea Note that the variable to be returned e.g. TriangleArea also has to be sent to the sub procedure so its value can be changed by the sub procedure - see Value and Reference parameters – later.

Parameters Variables passed between (sent to and / or from) modules. Necessary if the called procedure needs a local variable from the calling procedure.

Parameters Passing = sending to or from Variables (with stored data) Sent to the called procedure from the calling procedure. Or Sent from the called procedure to the calling procedure after it has finished. Needed only when: The called procedure requires data to perform its task. The calling procedure requires data which the called procedure produces. Passing = sending to or from 30/08/2019

Actual and Formal Parameters Actual Parameters Variables that are passed to a procedure when it is called. i.e. Call …(…, …, …) Formal Parameters Variables which must be declared in the procedure which must match in number, position and data type the actual parameters sent to it. i.e. Private Sub …(…, …, …) 30/08/2019

Formal Parameters You can use the same identifiers / names as the actual parameters they match. It is optional whether you declare the data types of formal parameters (as they must be the same as the actual parameters they match so are inherited from them). However, your code is clearer if you do. My programs will follow this suggestion. 30/08/2019

Value and Reference Parameters Formal Parameters can be declared as: Value Parameters (ByVal) Parameters in a called procedure which are not passed back to the calling procedure. Reference Parameters (ByRef) Parameters in a called procedure which are passed back to the calling procedure. i.e. Ones which have been changed by the called procedure and these changes are required by the calling procedure. i.e. Private Sub …(ByVal … As …, ByRef… As …) 30/08/2019

AreaTriangle = ½ * TriangleHeight * TriangleBase Input TriangleHeight Can you work out which of the parameters below should be passed by Value and which should be passed by Reference? (See the next slide for the answers!) Sub Procedures: CalcTriangleArea TriangleHeight Main Program TriangleBase AreaTriangle = ½ * TriangleHeight * TriangleBase Input TriangleHeight Input TriangleBase Input RectangleWidth Input RectangleLength CalcTriangleArea CalcRectangleArea CalcHouseArea Output HouseArea TriangleArea TriangleArea RectangleLength CalcAreaRectangle RectangleWidth AreaRectangle = RectangleWidth * RectangleBreadth RectangleArea RectangleArea AreaRectangle CalcHouseArea AreaTriangle HouseArea HouseArea = AreaTriangle + AreaRectangle HouseArea

AreaTriangle = ½ * TriangleHeight * TriangleBase Key: ….. Reference parameter Value parameter Key: ….. Value parameter ….. Reference parameter Sub Procedures: CalcTriangleArea TriangleHeight Main Program TriangleBase AreaTriangle = ½ * TriangleHeight * TriangleBase Input TriangleHeight Input TriangleBase Input RectangleWidth Input RectangleLength CalcTriangleArea CalcRectangleArea CalcHouseArea Output HouseArea TriangleArea TriangleArea RectangleLength CalcAreaRectangle RectangleWidth AreaRectangle = RectangleWidth * RectangleBreadth RectangleArea RectangleArea AreaRectangle CalcHouseArea AreaTriangle HouseArea HouseArea = AreaTriangle + AreaRectangle HouseArea

ByVal by default If you omit ByVal or ByRef then VB will choose ByVal by default. Meaning that by default no variables are passed back to the calling procedure. 30/08/2019

Sub procedures May return one or more items of data or no data at all (as in the previous presentation 10.1 Procedures) to the calling procedure. 30/08/2019

Program 10.2 Value & Reference Parameters Specification: Illustrate the differences between value and reference parameters. 30/08/2019

Program 10.2 Value & Reference Parameters Open the “Average/Mean” Program 3.3 - 3.3 Working with Data Make a copy of the previous program’s whole folder to keep the original program but rename this folder with the same name but add (Procedure Version). We will replace some of the code by two procedures. Procedure ProcessOneNumber Called from the click event of the OK button. Procedure CalcMean Called from the click event of the Show Mean button. 30/08/2019

Program 10.2 Value & Reference Parameters OK button: Two of its tasks were to keep running totals of all the marks and of how many exam marks had been entered. 30/08/2019

Program 10.2 Value & Reference Parameters NewMark Total NumberOfMarks Value reference Procedure ProcessOneNumber Total NumberOfMarks 30/08/2019

Program 10.2 Value & Reference Parameters Find the code for the OK button click event. Travel outside any procedure into the Declarations area. Begin and end a procedure with the following lines leaving a blank line in between using the following lines: Private Sub ProcessOneNumber(ByVal NewMark As Integer, ByRef Total As Integer, ByRef NumberOfMarks As Integer) End Sub Drag the following two lines of code from OK button click event procedure into the procedure above: Total = Total + NewMark ‘Add it to the running total of marks. NumberOfMarks = NumberOfMarks + 1 ‘Increase the number of marks by 1. 30/08/2019

Program 10.2 Value & Reference Parameters Call this procedure from the OK button click event procedure at the place where you moved the lines on the previous slide. Call ProcessOneNumber(NewMark, Total, NumberOfMarks) 30/08/2019

Program 10.2 Value & Reference Parameters Total NumberOfMarks Mean reference Value Procedure CalcMean Mean 30/08/2019

Program 10.2 Value & Reference Parameters Find the code for the Show Mean button click event. Begin and end a procedure with the following lines leaving a blank line in between using the following lines: Private Sub CalcMean(By Val Total As Integer, ByVal NumberOfMarks As Integer, ByRef Mean As Integer) End Sub Drag the following line of code from the Show Mean button click event into the procedure above: Mean = Total / NumberOfMarks 30/08/2019

Program 10.2 Value & Reference Parameters Call this procedure from the Show Mean button click event at the place where you moved the line on the previous slide using: Call CalcMean(Total, NumberOfMarks, Mean) 30/08/2019

Program 10.2 Value & Reference Parameters Run the program and test it. Save and publish the program. 30/08/2019

Commenting on Procedures In presentations 10.1 – 10.3 I will only ask for comments to procedures. Your comments MUST explain: What is the procedure for? Why and when (after and before what) are you calling it? What parameters are being sent to it and why? What parameters are being returned and why?

Extension Programs Open any of the programs you have written previously and move appropriate lines into appropriate procedures. Particularly look at extension programs written in 3.1 Working with Data. e.g. “Discount” Program “Selling Price” Program “Interest” Program etc.... Make a copy of the previous program’s whole folder to keep the original program but rename this folder with the same name but add (Procedure Version). Do this for as many programs as you need to until you are confident in creating procedures. 30/08/2019

Plenary What are parameters? Variables (with stored data) Sent to the called procedure from the calling procedure. Or Sent from the called procedure to the calling procedure after it has finished. What does passing mean in relation to parameters? Passing = sending to or from 30/08/2019

Plenary What are actual, formal, value and reference parameters? How do we declare value and reference parameters? 30/08/2019

Actual and Formal Parameters Actual Parameters Variables that are passed to a procedure when it is called. i.e. Call …(…, …, …) Formal Parameters Variables which must be declared in the procedure which must match in number, position and data type the actual parameters sent to it. i.e. Private Sub …(…, …, …) 30/08/2019

Value and Reference Parameters Formal Parameters can be declared as: Value Parameters (ByVal) Parameters in a called procedure which are not passed back to the calling procedure. Reference Parameters (ByRef) Parameters in a called procedure which are passed back to the calling procedure. i.e. Public Sub …(ByVal … As …, ByRef… As …) 30/08/2019