Download presentation
Presentation is loading. Please wait.
Published bySusanto Lesmono Modified over 5 years ago
1
10.3 Procedures Function Procedures 07/06/2019
2
Learning Objectives Describe the difference between functions and sub procedures. Explain how to write and call functions. 07/06/2019
3
Function Procedures Known as just functions for short (sub procedures are known as just procedures for short). Always returns one item of data to the calling procedure. Remember that: Sub procedures: May return one or more items of data or no data at all to the calling procedure. If only one value is to be returned then a Function is more suitable, if two or more values (or no values) need to be returned then a procedure is more suitable.
4
Built-in & User-defined Functions
Built-in Functions Supplied by VB, some of which you have used previously e.g. Format, Mid, Today User-defined Functions Written by the programmer. You will learn to write these here. 07/06/2019
5
AreaTriangle = ½ * TriangleHeight * TriangleBase
Functions: CalcTriangleArea Main Program TriangleHeight AreaTriangle = ½ * TriangleHeight * TriangleBase Input TriangleHeight Input TriangleBase Input RectangleWidth Input RectangleLength CalcTriangleArea CalcRectangleArea CalcHouseArea Output HouseArea TriangleBase TriangleArea CalcAreaRectangle RectangleLength AreaRectangle = RectangleWidth * RectangleBreadth RectangleWidth RectangleArea AreaRectangle CalcHouseArea AreaTriangle HouseArea = AreaTriangle + AreaRectangle Parameters HouseArea Note that the variable to be returned no longer has to be sent to Function procedures (as they do to Sub Procedures – see 10.2 Procedures), so all formal parameters are value parameters. A function just returns one answer and it is left up to the main program to “know” what that answer means.
6
Writing a Function Similar to Sub procedures. Differences:
Its job is to return only one item of data. So all formal parameters are value parameters (see the last slide). i.e. Declared with ByVal Starts with Private Function instead of Sub. The first line of the function (after the Private Function…) has to declare the variable which will be returned: Dim (Variable Name to be returned) As (Data Type) 07/06/2019 Continued on the next slide.
7
Writing a Function The last line of the function (before the End Function) will return the one item of data to the calling procedure: Return (Variable name to be returned) When the function is called you treat it as a value (i.e. the value it returns). Store it in a variable: VariableName = FunctionName(Variables to be passed) Display it directly: Control.Text = FunctionName(Variables to be passed) 07/06/2019
8
Writing a Function i.e. Private Function …(ByVal … As …, By Val… As …)
Dim (Variable name to be returned) As (Data Type) … Return (Variable name to be returned) End Function 07/06/2019
9
Program 10.3 Calculating Interest
Specification: Write a function to calculate the amount of interest earned on an investment. The amount invested, the interest rate and the number of years the investment lasts, are to be entered by the user. 07/06/2019
10
Program 10.3 Calculating Interest
If you invested €1000 over 2 years at an interest rate of 10% per year then the interest paid after one year would be €100. i.e. 10% of €1000 In year 2 you would get 10% of (€ €100 = €1100) i.e. €110 So the total interest paid would be €210 after 2 years. This program will calculate the interest paid for a given number of years. 07/06/2019
11
Program 10.3 Calculating Interest
Create a new project named ‘Calculating Interest’. txtAmount txtInterestRate txtYears lblInterest butCalcInterest
12
Program 10.3 Calculating Interest
butCalcInterest code: Dim Interest As Decimal Dim Amount As Decimal Dim InterestRate As Single Dim Years As Integer Amount = txtAmount.Text InterestRate = txtInterestRate.Text Years = txtYears.Text ‘Call the CalcInterest function by passing it the relevant ‘parameters and assigning it to the variable Interest. Interest = CalcInterest(Amount, InterestRate, Years) lblInterest.Text = “€” & Interest 07/06/2019
13
Program 10.3 Calculating Interest
Create a function: Private Function CalcInterest(ByVal Amount _ As Decimal, ByVal InterestRate As Single, _ ByVal Years As Integer) Dim Interest As Decimal ‘Variable to be returned. ‘For the loop to calculate year on year up to the number of ‘years the user entered. Dim Year As Integer For Year = 1 To Years Interest = Interest + ((Amount + Interest) * _ InterestRate / 100) Next Year Return Interest ‘Return the variable Interest. End Function
14
Program 10.3 Calculating Interest
Run the program and test it. 07/06/2019
15
Program 10.3 Calculating Interest
Write a “Boolean” function to test if the data entered in the text boxes is numeric. Private Function NumericFN Dim Numeric As Boolean = True If IsNumeric (txtAmount.Text) = False Or IsNumeric (txtInterestRate.Text) = False Or IsNumeric (txtYears.Text) = False Then Numeric = False End If Return Numeric End Function 07/06/2019
16
Program 10.3 Calculating Interest
Test this function before the Dim lines in the butCalcInterest code: If NumericFN = False Then MsgBox (“Please only enter numbers!”) Exit Sub End If 07/06/2019
17
Commenting on Functions
In presentations 10.1 – 10.3 I will only ask for comments to procedures/functions. Your comments MUST explain: What is the procedure/function for? Why and when (after and before what) are you calling it? What parameters are being sent to it and why? What parameter/s is/are being returned and why?
18
Extension “Average” Program 1
Open Program 3.4 Average you used last lesson. Make the sub-procedure CalcMean into a function. This is appropriate because CalcMean returns only one variable i.e. Mean. Write a Boolean function to test for numeric data. 07/06/2019
19
Extension “Factorial Iterative Function” Program 1
Change the “Factorial” Program Loops so that it uses a function. This is called an iterative function. 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 (Iterative Function Version). 07/06/2019
20
Extension Programs 2 Open any of the programs you have written previously and move appropriate lines into appropriate functions. Particularly look at the original 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 (Function Version). Do this for as many programs as you need to until you are confident in creating functions. 07/06/2019
21
Extension “Factorial Recursive Function” Program 3
Change the “Factorial” Program (Iterative Version) in presentation 5.1 Loops so that it uses a recursive function. Hint: FUNCTION calc(n) IF n = 1 THEN calc ← 1 ELSE calc ← n * calc(n-1) ENDIF RETURN calc 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 (Recursive Function Version). 07/06/2019
22
Extension “Adding Numbers from 1 to a chosen number” Program 4
Change the “Adding Numbers from 1 to a chosen number” Program (Iterative Version) in presentation 5.1 Loops so that it uses a recursive function. 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 (Recursive Function Version).
23
Extension “Adding Numbers between two chosen numbers” Program 5
Change the “Adding Numbers between two chosen numbers” Program (Iterative Version) in presentation 5.1 Loops so that it uses a recursive function. 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 (Recursive Function Version).
24
Extension “Adding Numbers from one chosen number to 6 then add 1” Program 6
Change the “Adding Numbers from one chosen number to 6 then add 1” Program (Iterative Version) in presentation 5.1 Loops so that it uses a recursive function. 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 (Recursive Function Version).
25
When to Use Recursion? Neat, cleaner and more elegant answer compared to the iterative version. Writing recursive functions can give you greater confidence that you are coding correctly. Often, writing code that ought to be recursive without recursion (i.e., as loops) produces messy code. Recursion often produces solutions that are very compact (requires few lines of typed code). So easier to debug than code with many lines. Recursive solutions can be VERY inefficient though. In general, recursion should be used when you know the number of recursive calls isn't excessive which depends on how much memory you have. Maximum: ~ 1000 recursive calls. A large number of function calls could cause stack overflow. Iteration can be simpler to understand and easier to write so less chance of error though. 07/06/2019
26
Plenary What is the difference between functions and sub procedures?
Function Procedures Always returns one item of data to the calling procedure. Sub procedures: May return one or more items of data or no data at all to the calling procedure. 07/06/2019
27
Plenary How do we write functions?
Private Function …(ByVal … As …, ByVal… As …) Dim (Variable name to be returned) As (Data Type) … Return (Variable name to be returned) End Function 07/06/2019
28
Plenary How do we call functions?
Assign a function’s return value to a variable. (Variable name) = (Function Name)(Parameters) Display a function’s return value e.g. to the text property of a control. (Control Name).Text = (Function Name)(Parameters) 07/06/2019
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.