On completion of this section you will be familiar
with
·
Difference between functions and
procedures
·
The need for them in a programme.
·
How to pass arguments to a function or
procedure.
·
How to get values back from functions.
If we look back at Listing 2‑1 we had a very
simple programme that was very easy to follow since the individual lines of the
input, processing and output followed each other in a very logical sequence
from top to bottom. Of course this programme was so simple that it was of no
use in real life. In order to make it more useful we added an If..Else
construct in order to calculate the tax. This addition is shown inListing 4‑1. After that we
added a Select..Case construct in order to calculate the
superannuation, as shown in Listing 5‑2. Finally, in order
to validate the hours we added the While
loop shown in Listing 6‑6. For completion a
similar loop should be added for validating the rate and the superannuation
code.
Recall that the If.. Else construct was added to handle the complexity of
the tax calculation, the Select Case
to handle the even more complex superannuation contribution and the While loop to handle the validation of
the data going into the variable sngHours. In order to restore the
original simplicity of the programme we shall create separate procedures for
handling the calculation of the gross, the tax, the superannuation and the nett. In the main programme we simply place calls to those
procedures
As well as making our programme easier to read,
functions and procedures have another benefit. Suppose that there was a group
of lines of a programme that were required to be used in different parts of the
programme, then, we can simply write out the lines of code separately for each
part of the programme where they are required. This has two drawbacks:
Firstly the more often we retype the lines of code the
more likely we are to make a mistake in one of them. This would cause
inconsistency in the application.
If the routine needs to be altered then we have to
make the same alteration wherever the routine occurs. This has the inherent
danger that we may miss out one of the instances of the routine.
The other alternative is to write the group of lines
together only once and then to call that group whenever we need to use them.
This has the following advantages:
·
It saves both saving typing time and
storage space.
·
As we write them only once the danger of
error is minimised.
·
If the routine needs to be changed then
the change has to be done only once
Our first task here is to look at what functions and
procedures are and what are the similarities and the difference between them.
As we said earlier functions and procedures are pieces
of code that are called from other programs in order to perform calculations or
else to run pieces of code that are required more than once. The main
difference between functions and procedures is that functions perform
calculations or other processing and send the result of the calculation or the
processing back to the main programme. The procedures dont perform any
calculations and dont send any results back to the main programme.
The code shown below is simply Listing 6‑6 broken into
methods. Below the code we will examine its new construction and how it
operates.
Listing 7‑1
|
1 |
Sub Pay2() |
|
2 |
Dim sngHours As Single |
|
3 |
Dim curRate As Currency |
|
4 |
Dim curGross As Currency |
|
5 |
Dim curTax As Currency |
|
6 |
Dim curNett
As Currency |
|
7 |
Dim curSuper As Currency |
|
8 |
Dim intSuperCode
As Integer |
|
9 |
sngHours =
InputBox("Enter Hours worked") |
|
10 |
curRate =
InputBox("Enter Rate") |
|
11 |
intSuperCode
= InputBox("Enter superannuation code") |
|
12 |
curGross = calculateGross(sngHours, curRate) |
|
13 |
curTax = calculateTax(curGross) |
|
14 |
curSuper = calculateSuper(intSuperCode,
curGross) |
|
15 |
curNett
= calculateNett(curGross, curTax, curSuper) |
|
16 |
showdata
sngHours, curRate, curGross, curTax, curSuper, curNett |
|
17 |
End Sub |
|
18 |
|
|
19 |
Function calculateGross(hours As Single,
rate As Currency) As Currency |
|
20 |
Dim gross As Currency |
|
21 |
gross = hours * rate |
|
22 |
calculateGross
= gross |
|
23 |
End Function |
|
24 |
|
|
25 |
Function calculateTax(gross As Currency) As
Currency |
|
26 |
Dim tax As Currency |
|
27 |
If gross <= 500 Then |
|
28 |
tax = gross * 0.25 |
|
29 |
Else |
|
30 |
tax = 125 + (gross - 500)
* 0.33 |
|
31 |
End If |
|
32 |
calculateTax
= tax |
|
33 |
End Function |
|
34 |
|
|
35 |
Function calculateSuper(superCode
As Integer, gross As Currency) As Currency |
|
36 |
Dim super As Currency |
|
37 |
Select Case superCode |
|
38 |
Case 0 |
|
39 |
super = 0 |
|
40 |
Case 1, 2, 3 |
|
41 |
super = gross * 0.05 |
|
42 |
Case 4 |
|
43 |
super = gross * 0.07 |
|
44 |
Case 5, 6 |
|
45 |
super = gross * 0.1 |
|
46 |
Case 7, 8, 9 |
|
47 |
super = gross * 0.2 |
|
48 |
Case Else |
|
49 |
Debug.Print
"Faulty data entered for super code" |
|
50 |
End |
|
51 |
End Select |
|
52 |
calculateSuper
= super |
|
53 |
End Function |
|
54 |
|
|
55 |
Function calculateNett(gross As Currency,
tax As Currency, super As Currency) As Currency |
|
56 |
Dim nett
As Currency |
|
57 |
nett
= gross - tax - super |
|
58 |
calculateNett
= nett |
|
59 |
End Function |
|
60 |
Sub showdata(hours As Single, rate As
Currency, gross As Currency, tax As Currency, super As Currency, nett As Currency) |
|
61 |
Debug.Print "Hours
", hours |
|
62 |
Debug.Print
"Rate"; ", rate" |
|
63 |
Debug.Print "Gross
", gross |
|
64 |
Debug.Print "Tax",
tax |
|
65 |
Debug.Print
"Super", super |
|
66 |
Debug.Print "Nett", nett |
|
67 |
End Sub |
Here Sub Pay
extends from line 1 to line 17. At first glance it is remarkably similar to
Listing 1‑1, but once we examine it closely we begin to find some
differences. The first difference is at line 12. This line is
curGross = calculateGross(sngHours,
curate)
This is equivalent to saying pass control to the function calculateGross and pass a copy of the data in the variables sngHours and curRate to it If the user entered 20 and 30 for hours and rate
then those two values will be passed to calculateGross.
The function calculateGross
itself spans lines 19 23. Let us first examine its declaration line:
Function calculateGross(hours As
Single, rate As Currency) As Currency
The first word Function
indicates that it is a piece of code that will perform a calculation and send
the result of that calculation back to the programme that called it. The second
word calculateGross is the name
of the function and whenever we wish to call that function we use its name as
we did at line 12.
After the name we have what looks like two variables
in brackets (hours as Single, rate as Currency). Those are the arguments of the function. If a function
is to perform a calculation it has to be given the raw data needed. The arguments is how
this data reaches the function. Recall that in line 12 when we called calculateGross we also included the variables sngHours
and curRate
between the brackets. When the function activates, the value of sngHours
will be copied into the argument hours and the value of curRate
will be copied into the argument rate. Because of this copying calculateGross now has the raw data it needs to perform
its calculations. These calculations occur in the body of the function.
At line 20 a local variable gross is declared. At
line 21 the values of the arguments hours and rate are multiplied and
the result stored in gross. This value must now be sent
back to the main programme. This is done at line 22, although to a beginner it
is difficult to know what is happening. The command calculateGross =
gross means do the following
1. finish the function
calculateGross and return control back to line of code
that called it i.e. line 12
2. take back also the
value of the variable gross
3. store this value in
the variable to the left of the call to calculateGross
The function calculateGross was
called at line 12. The variable to the left of the call is curGross and thus the
value calculated inside the body of the function calculateGross
is stored in this variable.
The same logic applies to the calls to the functions calculateTax, calculateSuper
and calculateNett at lines 13, 14
and 15.
The call to showData at line 16 is different
however. The differences are:
1. There is no
variable to the left of the call.
2. The arguments are
not enclosed in brackets
Let us now look at the definition of showData
itself which spans lines 60 to 67. At line 60 we notice that the first word is Sub and not Function. This means that this
will not perform any calculations and will not be returning any value back to
the main programme. It is for this reason that there is no variable to the left
of it when it is called at line 16. The values of the variables in Sub Pay are passed to its arguments in
the same way as values were passed to the functions. Here, however, no
calculations are performed on them. Instead they are printed to the screen
using Debug.Print
Now looking once more at Sub Pay we have restored the original simplicity, and the line by
line logic is easy to follow. Thus in the input section the values for hours,
rate and super code are received at lines 9, 10 and 11. In the processing
section the gross is calculated at line 12, tax at line 13, superannuation at line
14 and nett at line 15. The output section has only
one line 16 where a single call to showdata is used
to display the result of the calculation.
Functions and procedures are used both to avoid
repetition of code within a programme and also in order to make the logic of
the programme easier to follow. Data is passed by the calling routine to a
method using arguments. The data passed and the arguments must match each other
in type. The difference between functions and procedures is that functions
perform a calculation and return the result of it back to the main programme,
whereas procedures dont return any values back. The functions must be declared
with the keyword Function while the procedures are to be declared with the
keyword Sub.
Copy Listing 7‑1 into your
computer, and then, compile and run it.
Part 1
1. How does the use of
functions and procedures lead to having better written programmes?
2. What does an
argument mean as far as functions and procedures are concerned?
3. What co-relation
must exist between the arguments and the data passed to them?
4. Describe in detail
the similarities and differences between functions and procedures
Part 2
In Listing 7‑1 determine which lines of Pay2 are
the Input, Process and Output sections. Also determine the same section in calculateGross
or any other of the calculate functions.
Does the
procedure showData have all three sections. If not
which ones does it have?
Modify the
code you have created for Assignment Part 5 as follows:
1. Put the calculation
of the subtotal, GST, discount and total into three separate functions called calculateSubtotal, calculateGST,
calculateDiscount, calculateTotal.
2. The function calculateSubtotal will have two arguments, the unit price
and the amount sold.
3. The function calculateGST will have only one argument: the subtotal
4. The function caculateDiscount will have two arguments: the subtotal
and the discount code
5. The function calculateTotal will have three arguments: the subtotal, the
GSt and the discount
6. There must also be
a procedure for printing the data called showData. This will print out the
values of subtotal, GST, discount and total.
Although a major reorganization of the programme has
occurred here there has been no change in the actual processing. The test data for this part will be exactly
the same as that for Part 5.