0% found this document useful (0 votes)
18 views18 pages

FUNCTIONS_AND_SUBROUTINES_final1

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

FUNCTIONS_AND_SUBROUTINES_final1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

FUNCTIONS AND

SUBROUTINES
FUNCTIONS
•What are functions?
• Code enclosed by a Function and an End Function.
• A series of Visual Basic statements enclosed by the
Function and the End Function.
• performs computation and analysis to return some
value.
• Functions return values. example, a function can
compute the area of the circle, which will return the
value (area)
SYNTAX OF A FUNCTION
• [accesibility]
• Function Function_name
• [parameters]
• [As] return_type]
statements
• End Function
EXPLAINING THE
SYNTAX
•[accesibility]- specifies which pieces of code can be accessed
from the code.[Lecture 6 on Events –Reserved Words] e.g Public,
Private etc.
• Name of the Function – should be a valid VB identifier beginning
with a letter or an underscore. eg.CubeVolume - see notes on
[rules for naming variables].

• Parameters – They declare , specifying things like the passing


mechanism, variablename, datatype . eg.[ByVal num1 As
Integer..]ByVal and ByRef are yet to be explained.
• Return type – specifies the datatype of the variable the function
returns. E.g [parameters] As Integer.
SUBROUTINES….C
ONT
• Subroutines can accept inputs (as
parameters), but will not return values.
• Subroutines are not used to compute or
return values to the user; instead, they are
used to modularize scripts to increase
efficiency, reduce code redundancy and
improve readability of the scripts.
SUBROUTINES
• Is a chunk of code enclosed by the Sub and End Sub
• It performs a task for the code that invokes it
• It is different from a function in that it does not
return a value
Syntax
[modifiers] Sub SubName [(ParameterList)]
[statements]
End Sub
CREATING
SUBROUTINES
•Create an interface, put a button and a textbox.
• Double click the button to the code window
• Dim first As Integer
• First=Val(TextBox1.Text)
• First=0 Then
• MessageBox.Show(“Enter Integer”)
Run the program
The code running is in the button
CREATING A
SUBROUTINE
• Put curser outside the button and on a new line just after the End
Sub but before the End class
Type
• Private Sub WholeNumbers()
• Dim first As Integer
• first=Val(TextBox1.Text)
• If first=0 Then
• MessageBox.Show(“Enter Integer”)
Remove the code from the button
The code does not run because there is no code in the button
We have put the code in the Subroutine
CALLING THE
•SUBROUTINE
We can call the Subroutine whenever we want
to use it by just referring to its name
• Now go to design view and double click the
button to go top the button code
Type
• Call WholeNumbers()
• Run the program
• The program runs because we have called the
subroutine
CALLING THE
SUBROUTINE
•CONTINUED
Go to design view and add another button
• Double click button2 and go to button2 code window
Type
Call WholeNumbers()
• Run the program
• The program runs
The point is when you have created a subroutine, you
can use it whenever you like, by just referring to it by
name.
USING PARAMETERS
• Parameters are what we want to hand to our
Subroutine.
They define how and what we can pass from the
TextBox to the Subroutine. [modifiers] Sub SubName
[(ParameterList)]
[statements]
ACTIVITY
• Add two textboxes and a button to the form.
• Change the text property of Button3 to Get Answer.
• Double click Get Answer button to the code window
USING PARAMETERS
CONTINUED
NOW TYPE
Dim first As Integer
Dim second As Integer
First =Val(TextBox2.Text)
Second=Val(TextBox3.Text)
Call AddNumbers()
USING PARAMETERS CONTINUED
• Create a Subroutine AddNumbers just after End Sub but before End
Class.
TYPE
Private Sub AddNumbers()
Dim answer As Integer
answer = first + second
MessageBox.Show(“The Total is” & answer
End Sub
there are two wiggly lines under first and Second because
AddNumbers Sub knows nothing about these two variables.
We've only declared one variable inside the Subroutine - answer.
To get rid of the wiggly lines, we can set up something called a
Parameter. Well, two parameters.
USING PARAMETERS
CONTINUED
• Change the Private Sub AddNumbers() line to this:
• Private Sub AddNumbers( first As Integer, second As Integer )
• change your Calling line to this:
Call AddNumbers( first, second )
(If the second inside your Sub has changed to Second(). Delete the
round brackets.)
Run your programme and check that it is working properly
• .
USING PARAMETERS CONTINUED
The parameters in between the parentheses are our two
variables.
They don't have to have the same names.
Whatever you call your variables in the AddNumbers Sub
does not have to be the same names as the calling line.
The variable names can be entirely different. But the values
in the variables get passed in the order you set them up.

In our case the value in the variable first will get passed to
the first variable in our AddNumbers Sub; the value in the
variable second will get passed to the next variable we set
up in our AddNumbers Sub.
HOMEWORK

• Create a Sub to check a Textbox for a valid


email address, or adapt the one you
already have. Pass whatever is entered in
the Textbox to a variable called "email".
Pass the value from this variable to your
Sub by using a Parameter. When a button is
clicked, a message box should pop up
telling the user if the email address was
wrong.
BYVAL AND BYREF IN
VB .NET
• When you pass arguments over to Subs and Function you can
do so either By Value or By Reference.
ByVal
• means that you are passing a copy of a variable to your
Subroutine
• You can make changes to the copy and the original will not be
altered.
ByRef
• is short for By Reference. This means that you are not
handing over a copy of the original variable but pointing to
BYVAL AND BYREF IN VB .NET CONTINUED

ACTIVITY
• Add a new button the form you created in the
previous section. Double click the button and add
the following code:
• Dim Number1 As Integer
• Number1 = 10
Call IncrementVariable(Number1)
• MessageBox.Show(Number1)

You might also like