Presentation is loading. Please wait.

Presentation is loading. Please wait.

10.2 Procedures Passing Parameters 30/08/2019.

Similar presentations


Presentation on theme: "10.2 Procedures Passing Parameters 30/08/2019."— Presentation transcript:

1 10.2 Procedures Passing Parameters 30/08/2019

2 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

3 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.

4 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.

5 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.

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

7 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.

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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

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

18 Program 10.2 Value & Reference Parameters
Open the “Average/Mean” Program 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

19 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

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

21 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

22 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

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

24 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

25 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

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

27 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?

28 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

29 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

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

31 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

32 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


Download ppt "10.2 Procedures Passing Parameters 30/08/2019."

Similar presentations


Ads by Google