Procedures and Functions!

Hi! Today’s lesson was based mostly on basic functions but for the start of the lesson the class discussed the new terms of Actual and Formal Parameters.

Here is a small section of code that incorporates these two terms:

Call Doubler(Num)
Private Sub Doubler(ByVal N As Single)

The parameter used in the calling statement is properly known as the actual parameter. The actual parameter is what is passed to the procedure.
The parameter listed in the procedure heading is properly known as a formal parameter. The number, order and type of formal parameters must match the actual parameters, but their names can be different.

The rest of the lesson was to do with SD booklet 5 – User defined functions. Mr Stratton said that many of the functions would never appear in the exam but some important functions we might want to take note of were the Ucase function which converts lower-case characters into upper-case characters and the Lcase function which does the opposite. Another important function was the Mid$ function which extracts a sub-string from a string and also featured hugely in our program for the day. I have typed the part of the program for how to create the password below:

‘create the password
name1 = Mid$(name1, 1, 1)
name2 = Mid$(name2, 2, 1)
year = Mid$(year, 3, 2)
colour = Mid$(colour, 2, 2)
street = Mid$(street, 1, 3)
password = name1 + name2 + year + colour + street + shoe_size

Sorry if you get bored reading it all I guess.
Well that all folks!

Design a logo!

We need a new logo for the blog! I would like you to design and create one. It can not use copyright images but can have photos you have taken yourself as well as public domain images.

Attached below is a quick sample I knocked up with powerpoint and irfanview

Program to order computer..

sorry for the late post guys, this relates to the task set on thursday/friday.

Well the task we were given to create should have been finished on friday. The program was complex but there is a solution.

Mr stratton has already posted the solution, but there was another way in which it could have been done, which was instead on using parameter passing, you could just type the program straight in without it,though this would take longer and could then have a lot of problems.

so for the solution look to Mr strattons post previous to this one.

Well guys peace out, live long and prosper….

Solution to 6.5.3 task 1

Ok so this is going to be a long post (I will need to check the blog options and shrink the post sizes). This is a solution to task 1 in 6.5.3 it is based on the design given in the answers. It is NOT the only solution and you might want to comment on ways the program could be improved (think about evaluation). I have included some screen shots.

NOTE the display of the data is optional.

Option Explicit

Private Sub Command1_Click()
'This program represents a solution to 6.5.3 task 1
'The Desgin was given in the answers section
'It is not the only solution
Dim HD_up As Integer 'HD Upgrade required
Dim RAM_up As Integer 'RAM Upgrade required
Dim Mon_up As Integer 'Monitor Upgrade required
Dim Cost1 As Single 'Price
Dim Num As Integer 'Quanity
Dim Basic_Cost As Single 'Cost for Order before deliver and discounts
Dim Dis As Single ' Discount on whole order
Dim Del As Single ' delivery charge per order
Dim VAT As Single 'VAT on Order
Dim Total As Single ' Order Total
Cost1 = 499 'Set the starting cost of a machine
Del = 50 ' £50 delivery per order
Form1.Print "The basic systems costs 499" 'optional
Call Get_Comp_Spec(HD_up, RAM_up, Mon_up)
Call Calc_Cost_Of_One(Cost1, HD_up, RAM_up, Mon_up)
Call Get_Number(Num)
Call Calc_Basic_Cost(Num, Cost1, Basic_Cost)
Call Calc_discount_delivery_VAT(Num, Basic_Cost, Dis, Del, VAT)
Call Calc_total_cost(Basic_Cost, Dis, Del, VAT, Total)
Call Display_total(Total)
End Sub

Private Sub Get_Comp_Spec(ByRef HD_up As Integer, ByRef RAM_up As Integer, ByRef Mon_up As Integer)
'Check what upgrades the user wishes
HD_up = MsgBox("Do you wish to upgrade to 60GB for £30?", vbYesNo)
RAM_up = MsgBox("Do you wish to upgrade to 1GB for £50?", vbYesNo)
Mon_up = MsgBox("Do you wish to upgrade to 21 inch for £199?", vbYesNo)
End Sub

Private Sub Calc_Cost_Of_One(ByRef Cost1 As Single, ByVal HD_up, ByVal RAM_up, ByVal Mon_up)
'calculate the cost of the upgrades
If HD_up = vbYes Then
Cost1 = Cost1 + 30
Form1.Print "Hard drive upgrade £30" 'optional
End If
If RAM_up = vbYes Then
Cost1 = Cost1 + 50
Form1.Print "RAM upgrade £50" 'optional
End If
If Mon_up = vbYes Then
Cost1 = Cost1 + 199
Form1.Print "Monitor upgrade £199" 'optional
End If
End Sub

Private Sub Get_Number(ByRef Num As Integer)
'Get the number of systems ordered
'As an extension task this number could be validated
Num = InputBox("How many machines do you want?")
Form1.Print Num & " machines ordered" 'optional
End Sub

Private Sub Calc_discount_delivery_VAT(ByVal Num, ByVal Basic_Cost, ByRef Dis As Single, ByRef Del As Single, ByRef VAT As Single)
'Calculate Discount
If Num > 10 And Num <= 20 Then
Dis = Basic_Cost * 0.05 '5% Form1.Print "A discount of 5% is £" & Dis 'optional
ElseIf Num > 20 Then
Dis = Basic_Cost * 0.1 '10%
Form1.Print "A discount of 10% is £" & Dis 'optional
Else
Dis = Basic_Cost * 0 '0%
Form1.Print "A discount of 0% is £" & Dis 'optional
End If
'Calculate VAT
Form1.Print "Delivery on the order is £" & Del 'optional
VAT = (Basic_Cost - Dis + Del) * 0.175
Form1.Print "The VAT on the order is £" & VAT 'optional
End Sub

Private Sub Calc_Basic_Cost(ByVal Num, ByVal Cost1, ByRef Basic_Cost As Single)
'Calculate the cost of the order before discount and VAT
Basic_Cost = Num * Cost1 ' Cost for whole order
Form1.Print "The cost of the order before discount,deliver & VAT is £" & Basic_Cost 'optional
End Sub

Private Sub Calc_total_cost(ByVal Basic_Cost, ByVal Dis, ByVal Del, ByVal VAT, ByRef Total As Single)
'Calculate the Total cost of the whole order
Total = Basic_Cost + Del + VAT - Dis
End Sub

Private Sub Display_total(ByVal Total)
'Display the cost for the whole order
Form1.Print "The total for the order was £" & Format(Total, "0.00")
End Sub

Displaying 20 Names and Marks in An Array

Today we were given the task of adapting Fridays program to display 20 names and scores from a listbox as well as working out the average mark as a percentage, in another one of Mr Strattons trademark programming tasks. As usual we were let loose on the computers, where, after 15 minutes of confusion and entering paragraphs of code, Mr Stratton questioned us as to who had actually jotted down a design before implementing it. He eventually posted a possible solution which is shown below:

Private Sub CmdStart_Click()
‘load an Array with data from a list box
‘list box is hidden on the form
‘List Boxes count from 0 so add a heading to the fist item
Dim IntNumber(20) As Integer
Dim StrName(20) As String
Dim intavg As Integer
Call Load_Array(IntNumber(), StrName())
Call Calc_Average(IntNumber(), intavg)
Call Display_Array(IntNumber(), StrName(), intavg)
End Sub

Private Sub Load_Array(ByRef Number() As Integer, ByRef StrName() As String)
Dim Counter As Integer ‘local Variable
For Counter = 1 To 20
Number(Counter) = LstNumbers.List(Counter)
StrName(Counter) = LstNames.List(Counter)
Next
End Sub

Private Sub Calc_Average(ByRef IntNumber() As Integer, ByRef intavg As Integer)
Dim IntSum As Integer
For Counter = 1 To 20
IntSum = IntSum + IntNumber(Counter)
Next
intavg = IntSum / 20
End Sub

Private Sub Display_Array(ByRef Number() As Integer, ByRef StrName() As String, ByVal intavg)
‘Standard procedure as used in previous programs
Dim Counter As Integer ‘local Variable
For Counter = 1 To 20
Form1.Print StrName(Counter) & ” scored ” & Number(Counter) & “%”
Next
Form1.Print “Average = ” & intavg & “%”
End Sub

And thats it folks, I’ll leave Danielle to fill you in with the rest.

VB Program for Displaying the Average of 20 Marks

Below is the program from today’s lesson. It is not completed, I would like you to Change the program to display the average with two decimal places. Please add your solution with a comment below.

Private Sub CmdStart_Click()
'load an Array with data from a list box
'list box is hidden on the form
'List Boxes count from 0 so add a heading to the fist item
Dim IntNumber(20) As Integer
Dim StrName(20) As String
Dim intavg As Integer
Call Load_Array(IntNumber(), StrName())
Call Calc_Average(IntNumber(), intavg)
Call Display_Array(IntNumber(), StrName(), intavg)
End Sub

Private Sub Load_Array(ByRef Number() As Integer, ByRef StrName() As String)
Dim Counter As Integer 'local Variable
For Counter = 1 To 20
Number(Counter) = LstNumbers.List(Counter)
StrName(Counter) = LstNames.List(Counter)
Next
End Sub


Private Sub Calc_Average(ByRef IntNumber() As Integer, ByRef intavg As Integer)
Dim IntSum As Integer
For Counter = 1 To 20
IntSum = IntSum + IntNumber(Counter)
Next
intavg = IntSum / 20
End Sub

Private Sub Display_Array(ByRef Number() As Integer, ByRef StrName() As String, ByVal intavg)
'Standard procedure as used in previous programs
Dim Counter As Integer 'local Variable
For Counter = 1 To 20
Form1.Print StrName(Counter) & " scored " & Number(Counter) & "%"
Next
Form1.Print "Average = " & intavg & "%"
End Sub

Listbox into Array Program

REMEMBER! Save all First Class emails etc if you do not all the information will be lost.

Todays Task :
Create a program that stores 20 numbers in a Listbox then input them into a array before displaying them on the screen.

My Design:

Pseudocode:
main steps

1/ Set up variables
2/ Get and store 20 numbers from listbox
3/Display Numbers
4/End Program

Refinements:
2.1/For 1 to 20
2.2/store number
2.3/Next

Here is Mr Stratton’s Awesome Program From Today
Private Sub CmdStart_Click()
‘load an Array with data from a list box
‘list box is hidden on the form
‘List Boxes count from 0 so add a heading to the fist item
Dim Number(20) As Integer
Call Load_Array(Number())
Call Display_Array(Number())
End Sub

Private Sub Load_Array(ByRef Number() As Integer)
Dim Counter As Integer ‘local Variable
For Counter = 1 To 20
Number(Counter) = LstNumbers.List(Counter)
Next
End Sub

Private Sub Display_Array(ByRef Number() As Integer)
‘Standard procedure as used in previous programs
Dim Counter As Integer ‘local Variable
For Counter = 1 To 20
Form1.Print Number(Counter)
Next
End Sub

REMEMBER! Arrays Are Always Passed ByReferance

and Remember!
Homework:
For Tuesday the Multiple Choice sheet.
The end

Transferring data from a listbox to an array

The programmer would enter the information in a list box then hide this box from the user (Preferences). This allows the program to use the same test data at all times. This is the same as READ DATA in other languages. Shown below is the program from today’s lesson.

Private Sub CmdStart_Click()
'load an Array with data from a list box
'list box is hidden on the form
'List Boxes count from 0 so add a heading to the fist item
Dim Number(20) As Integer
Call Load_Array(Number())
Call Display_Array(Number())
End Sub

Private Sub Load_Array(ByRef Number() As Integer)
Dim Counter As Integer 'local Variable
For Counter = 1 To 20
Number(Counter) = LstNumbers.List(Counter)
Next
End Sub

Private Sub Display_Array(ByRef Number() As Integer)
'Standard procedure as used in previous programs
Dim Counter As Integer 'local Variable
For Counter = 1 To 20
Form1.Print Number(Counter)
Next
End Sub

You wanted the best? You got the best. Ladies and gentlemen, the lesson from 11th of November

In computing today we started off with Local and Global variables. Local Variables exist only within a single subroutine, and  cannot be accessed from elsewhere in the code.

Global Variables are created in the main part of the program that can be seen from any part of the program.

We then moved on to Arrays where we learned that ARRAYS ARE ALWAYS PASSED BY REFERENCE!!!!!!!!!!

Which means that data is passed in to a subroutine where it can be changed and then passed back out to other parts of the program.

We then had to code up a program that took and input from the user and passed that value by reference to other subroutines.

Here is the code for that program

Option Explicit
Private Sub CmdStart_Click()
'Set up variables
Dim multiple As Integer
Dim answers(12) As Integer 'Array of 12 numbers

'Get Multiplier from user
Call GetValue(multiple)
'Calculate Answers
Call CalcAnswer(answers(), multiple)
'Display table
Call DisplayTable(answers(), multiple)
End Sub


Private Sub GetValue(ByRef intvalue As Integer)
intvalue = InputBox("Please enter the value")
End Sub


Private Sub DisplayTable(ByRef answers() As Integer, ByVal multiple)
Dim counter As Integer
Dim Answer As Integer

For counter = 1 To 12
lstOutput.AddItem multiple & " X " & counter & " = " & answers(counter)
Next
End Sub


Private Sub CalcAnswer(ByRef answers() As Integer, ByVal multiple)
Dim counter As Integer

For counter = 1 To 12
answers(counter) = multiple * counter
Next
End Sub

Also

The game.

Coltness High School

Report a Glow concern
Cookie policy  Privacy policy

Glow Blogs uses cookies to enhance your experience on our service. By using this service or closing this message you consent to our use of those cookies. Please read our Cookie Policy.