Wednesday, 13 November 2013

VB Lesson 10: Standard Subroutines & Functions

I genuinely have no excuse Lalin, except maybe that I helped you last week with both open evenings ;), but seriously, I've just been lazy. But I'm writing them up now, so yay :).

Visual Basic Lesson 10:

So onto subroutines and functions... Wooh! Subroutines and functions are really useful pieces of coding, but before we talk about writing our own, we need to look at the standard ones which are offered by VB. On top of that we also need to define what exactly a subroutine is and what exactly a function is.

Firstly let us look at functions. Functions, like variables, still have to have a name, if they didn't then it would be impossible to distinguish between them. This all comes down to self-documenting code when writing your own, because a function name tells the programmer what the function does - to a certain extent anyway. Functions also have parameters. This sounds fairly fancy, but essentially the parameters are variables within brackets.

A standard example of a parameter:

(Variable)
There will be a more practical example of a parameter when it comes to an actual example function.

Another key feature of a function is that it must always return a value! Every single time.  So because it has to return a value you must always have something on the left with an equals sign, with something else on the right.

For example:

Current = Now()

A further example of a function is:

Dim current As Date

Current = Now()
M = Month(Current)
Print Hour(Current), Minute (Current)

Two useful built in functions are the midfunction and the substring function. 

Okay, now onto subroutines. Like functions, subroutines have to have a name and they can have parameters. But the difference is that subroutines may or may not return a value and the returned value doesn't have to be used as an assignment.

Here is an example of a program designed in class:


This program is designed to use the substring function in order to reverse a name which the user has inputted, in order to display it backwards.

And that's essentially built in subroutines and functions. Next time we will look at writing  our own subroutines.

Bye!

Sunday, 3 November 2013

Test Answer Rewrites!

This isn't a blog explaining anything. It is just the rewrites of the answers from the test which I got wrong.

Question 6. Validation and verification check are carried out on employee data being entered into a computer system.
(a) Using an example of acceptable data describe a suitable validation check for each of the items of data below. In each case give an example of invalid data that would be detected by the check.
A different validation check must be used in each case.                                                [4]
         Employee salary.

A suitable validation check for an employee salary would be a type check, which would ensure the correct data type, in this case real data, is being used for the data stored. For example "£20,000" would be accepted, but "twenty thousand pounds" would be rejected as it is the text data type, not real data.

Question 9. (a) Below is an algorithm to produce the multiplication table for a given number.

Algorithm Multiplication Table.
Multiplier is integer                       {input by user}
Product is integer                         {used to store current answer}i is integer                                    {loop control variable}startmainprog    input Multiplier        for i = 1 to 12                  set Product = i* Multiplier                  output Product    end forend mainprog             
 (i) Draw circle on the above algorithm to clearly indicate an example of annotation.[1]

for i = 1 to 12
                  set Product = i* Multiplier
                  output Product
    end for    

(b) Below is a segment of an algorithm that determines if an item is present in a sorted array.

input SearchValueset Found = False        {initialise variables}set i = 0set SizeArray = 9
repeat
         set i = i + 1         if SearchVariable = SearchArray[i] then Found = True
until (Found = True) OR (i = SizeArray) OR (SearchValue < SearchArray[i])

 Complete the table below to show how each variable changes when the algorithm is performed on the test data given.
Search Value = 41 

i SearchArray[i] Found
1        14           False
2        19           False
3        23           False
4        28           False
5        31           False
6        39           False
7        43           False
          47           

Question 12. (a) State what the output will be for a customer who has bought:

                          (iii) twenty five bottles of olive oil of total value 45                                    [1]

Give free bottle of Greek olive oil.

Question 13. Below is an algorithm intended to calculate the area of a circle

Algorithm CalculateArea

Pi = 3.142

Area is integer Area is Real

Radius is real

Startmainprog

       output "Type in the radius"

       input Radius

       Area = Pi * Radius * Radius

       Output "The area is" Area

endmainprog

(b) The line "Area is integer" may cause a problem. Describe the problem that this line may cause and amend the algorithm to remove the problem.

The problem that this line may cause is that an area may not be an integer or whole number, it might be a decimal number, therefore a piece of real data.


You may cross out any words of lines in the algorithm and write your own words or lines. There is no need to completely rewrite the algorithm.