0% found this document useful (0 votes)
5 views3 pages

A Level Computer Science Procedures & Functions

worksheet

Uploaded by

max davies
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

A Level Computer Science Procedures & Functions

worksheet

Uploaded by

max davies
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

A Level Computer Science

Procedures and functions


Program 1: Add code for a procedure that will add the first two numbers together and store them in a variable called
Sum. Remember to identify the parameters that will be passed too.
Sub Main()
FirstNo, SecondNo, Sum As Integer

FirstNo = INPUT(“Enter first number”)


SecondNo = INPUT(“Enter second number”)
Call AddNumbers( )
End Sub
Sub AddNumbers( )
Sum = FirstNo + SecondNo
End Sub

Program 2: Add code for a procedure that will set a variable called Highest to the highest value from NumOne and
NumTwo. Remember to identify the parameters that will be passed too.

Sub Main()
NumOne, NumTwo, Highest
NumOne= INPUT(“Enter first number”)
NumTwo = INPUT(“Enter second number”)
Call FindHighest( )
End Sub

Sub FindHighest ( )
If NumOne > NumTwo then
Highest = NumOne
Else
Highest = NumTwo
End
End Sub
A Level Computer Science
Program 3: Add code for a procedure that will set a variable called Valid to true length of DriverNo is equal to 16
characters. Remember to identify the parameters that will be passed too.

Sub Main()
DriverNo As String
Valid as Boolean
DriverNo= INPUT(“Enter your driver number”)
Call CheckValidNo( )
End Sub
Sub CheckValidNo ( )
If [Link] = 16 then
Valid = True
Else
Valid = False
End if
End Sub

Notice with each of the procedures, you had to pass in the variables needed to do the calculations or processing and
the variable to store the result of the calculations or processing. If a procedure is producing a single variable as a
result of processing, you should really use a special type of procedure called a function.

The difference between a procedures and functions is: “a function will always return a value.”

Below is a program that asks the user to enter a mark and shows the grade this equates to, but this time using a
function:
Sub Main()

EnteredMark=INPUT(“Enter your mark”)

Grade = CalculateGrade(EnteredMark) ‘Grade stores whatever is returned from the


function CalculateGrade()

End Sub

Function CalculateGrade(ByRef EnteredMark As Integer)

If EnteredMark < 40
Return “U” ‘This will return “U” and store it in the variable Grade
ElseIf EnteredMark < 50
Return “E”
……
End if
A Level Computer Science
Create function calls for program 1, 2 and 3 from the previous page:
Sub Main()
FirstNo, SecondNo, Sum As Integer
FirstNo = INPUT(“Enter first number”)
SecondNo = INPUT(“Enter second number”)
Sum = AddNumbers(FirstNo, SecondNo)
End Sub

Function AddNumbers(FirstNo, SecondNo)


Return FirstNo + SecondNo
End Sub

TASK: Now write a program with functions to Subtract, Multiply and Divide

Sub Main()
NumOne, NumTwo, Highest As Integer
NumOne= INPUT(“Enter first number”)
NumTwo = INPUT(“Enter second number”)
Highest = FindHighest (NumOne, NumTwo)
End Sub

Function FindHighest (NumOne, NumTwo)


If NumOne > NumTwo Then
Return NumOne
Else
Return NumTwo
End If
End Function

Sub Main()
DriverNo As String
Valid As Boolean
DriverNo= INPUT(“Enter your driver number”)
Valid = CheckValid(DriverNo)
End Sub

Function CheckValidNo (DriverNo)


If [Link] = 16
Return True
Else
Return False
End If
End Function

You might also like