10 3 Procedures Function Procedures 12282021 1 Learning




























- Slides: 28

10. 3 Procedures Function Procedures 12/28/2021 1

Learning Objectives Describe the difference between functions and sub procedures. Explain how to write and call functions. 12/28/2021 2

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. n Remember that: Sub procedures: n 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. 3

Built-in & User-defined Functions Built-in Functions l l 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. 12/28/2021 4

Functions: Main Program Input Triangle. Height Input Triangle. Base Input Rectangle. Width Input Rectangle. Length Calc. Triangle. Area Calc. Rectangle. Area Calc. House. Area Output House. Area Parameters Triangle. Height Triangle. Base Triangle. Area Calc. Triangle. Area. Triangle = ½ * Triangle. Height * Triangle. Base Calc. Area. Rectangle. Length Rectangle. Width Area. Rectangle = Rectangle. Width * Rectangle. Area Rectangle. Breadth Calc. House. Area. Triangle House. Area = Area. Triangle + House. Area. Rectangle 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.

Writing a Function Similar to Sub procedures. Differences: n n 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 By. Val n n 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) 12/28/2021 Continued on the next slide. 6

Writing a Function n 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) n When the function is called you treat it as a value (i. e. the value it returns). Store it in a variable: n Variable. Name = Function. Name(Variables to be passed) Display it directly: n 12/28/2021 Control. Text = Function. Name(Variables to be passed) 7

Writing a Function i. e. n Private Function …(By. Val … As …, By Val… As …) Dim (Variable name to be returned) As (Data Type) … Return (Variable name to be returned) n 12/28/2021 End Function 8

Program 10. 3 Calculating Interest Specification: n n 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. 12/28/2021 9

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. n i. e. 10% of € 1000 In year 2 you would get 10% of (€ 1000 + € 100 = € 1100) n 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. 12/28/2021 10

Program 10. 3 Calculating Interest Create a new project named ‘Calculating Interest’. txt. Amount txt. Interest. Rate txt. Years lbl. Interest but. Calc. Interest

Program 10. 3 Calculating Interest but. Calc. Interest code: n n n Dim Interest As Decimal Dim Amount As Decimal Dim Interest. Rate As Single Dim Years As Integer Amount = txt. Amount. Text Interest. Rate = txt. Interest. Rate. Text Years = txt. Years. Text ‘Call the Calc. Interest function by passing it the relevant ‘parameters and assigning it to the variable Interest = Calc. Interest(Amount, Interest. Rate, Years) lbl. Interest. Text = “€” & Interest 12/28/2021 12

Program 10. 3 Calculating Interest Create a function: n n n Private Function Calc. Interest(By. Val Amount _ As Decimal, By. Val Interest. Rate As Single, _ By. Val 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 n n Interest = Interest + ((Amount + Interest) * _ Interest. Rate / 100) Next Year Return Interest ‘Return the variable Interest. n End Function

Program 10. 3 Calculating Interest Run the program and test it. 12/28/2021 14

Program 10. 3 Calculating Interest Write a “Boolean” function to test if the data entered in the text boxes is numeric. n Private Function Numeric. FN Dim Numeric As Boolean = True If Is. Numeric (txt. Amount. Text) = False Or Is. Numeric (txt. Interest. Rate. Text) = False Or Is. Numeric (txt. Years. Text) = False Then n Numeric = False End If Return Numeric n End Function 12/28/2021 15

Program 10. 3 Calculating Interest Test this function before the Dim lines in the but. Calc. Interest code: n If Numeric. FN = False Then Msg. Box (“Please only enter numbers!”) Exit Sub n End If 12/28/2021 16

Commenting on Functions In presentations 10. 1 – 10. 3 I will only ask for comments to procedures/functions. Your comments MUST explain: n n 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?

Extension “Average” Program 1 Open Program 3. 4 Average you used last lesson. Make the sub-procedure Calc. Mean into a function. n This is appropriate because Calc. Mean returns only one variable i. e. Mean. Write a Boolean function to test for numeric data. 12/28/2021 18

Extension “Factorial Iterative Function” Program 1 Change the “Factorial” Program - 5. 1 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). 12/28/2021 19

Extension Programs 2 Open any of the programs you have written previously and move appropriate lines into appropriate functions. n Particularly look at the original programs written in 3. 1 Working with Data. n e. g. “Discount” Program “Selling Price” Program “Interest” Program etc. . n 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. 12/28/2021 20

Extension “Factorial Recursive Function” Program 3 Change the “Factorial” Program (Iterative Version) in presentation 5. 1 Loops so that it uses a recursive function. n Hint: FUNCTION calc(n) IF n = 1 THEN calc ← 1 n ELSE calc ← n * calc(n-1) n ENDIF RETURN calc n 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). 12/28/2021 21

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

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

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

When to Use Recursion? Neat, cleaner and more elegant answer compared to the iterative version. n 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. n 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 easier to write so less chance of error though. http: //www. cs. umd. edu/class/fall 2002/cmsc 214/Tutorial/recursion 2. html 12/28/2021 25

Plenary What is the difference between functions and sub procedures? n Function Procedures Always returns one item of data to the calling procedure. n Sub procedures: May return one or more items of data or no data at all to the calling procedure. 12/28/2021 26

Plenary How do we write functions? n n Private Function …(By. Val … As …, By. Val… As …) Dim (Variable name to be returned) As (Data Type) n … n Return (Variable name to be returned) n End Function 12/28/2021 27

Plenary How do we call functions? n Assign a function’s return value to a variable. (Variable name) = (Function Name)(Parameters) n Display a function’s return value e. g. to the text property of a control. (Control Name). Text = (Function Name)(Parameters) 12/28/2021 28