0% found this document useful (0 votes)
57 views59 pages

Lesson5 - Procedures and Modules

This document discusses modules, sub procedures, and functions in Visual Basic.NET. It introduces modular design concepts and explains how to write sub procedures. It also covers the differences between calling by value and calling by reference. The learning outcomes are to declare and write sub programs, pass arguments to sub programs, and write sub procedures that use call-by-value and call-by-reference.

Uploaded by

Yovin Lekamge
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)
57 views59 pages

Lesson5 - Procedures and Modules

This document discusses modules, sub procedures, and functions in Visual Basic.NET. It introduces modular design concepts and explains how to write sub procedures. It also covers the differences between calling by value and calling by reference. The learning outcomes are to declare and write sub programs, pass arguments to sub programs, and write sub procedures that use call-by-value and call-by-reference.

Uploaded by

Yovin Lekamge
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/ 59

CREATING MODULES

AND WORKING WITH


SUB PROCEDURES
AND FUNCTIONS
Visual Basic.NET
Lesson 5

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020


1
TOPIC & STRUCTURE OF THE LESSON

• Introduction to Modular Design Concepts


• Write Sub Procedures
• Compare between Call by Value and Call by
Reference

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 2


LEARNING OUTCOMES
At the end of this lecture you should be able
to :
1. Declare a sub program
2. Write a sub program and pass arguments to it
3. Write a Call-By-Value and Call By Reference sub
procedure

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 3


 In all the previous lessons, you have used
event procedures such as
 Button1_Click
 Form1_Load etc.
To manage events and organize the flow of your
programs.

 In VB programming, all executable


statements must be placed inside some
procedure; only general declarations and
instructions to the compiler can be placed
outside a procedure’s scope.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 4


MODULAR DESIGN CONCEPTS
Modularity refers to breaking a large
problem down into smaller self-contained
modules.
This allows the programmer to isolate
problems within the program by looking at
specific areas.
Without Breaking problems down into
smaller modules will increase program
maintenance cost.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 5


IN THIS CHAPTER,
 You’ll continue to organize your programs
according to structured programming
techniques by developing a hierarchical
structure within your application and by
breaking computing tasks into logical units.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 6


PROCEDURES OVERVIEW
 If you design your application correctly, and split
the processes and tasks logically, you can reuse
the same code across the application you are
working on or even across other applications.
 This should be the goal of every developer.
 There is no sense in having a block of code
behind a button, and then having the same
exact code behind another button.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 7


ADVANTAGES OF PROCEDURES
 The code in a procedure is reusable. After a
procedure is created and tested, it can be
called from different places in an
application.
 The application is easier to debug and
maintain, because you can easily trace the
source of an error in a procedure instead of
checking the entire application for errors.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 8


DIFFERENT MODULAR TECHNIQUES
Visual Basic provides us with the ability to
represent modules through the use of the
following:-
• Modules
• Sub Procedures
General Procedures
• Function Procedures
• Event Procedures

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 9


WORKING WITH
MODULES

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020


10
WORKING WITH MODULES
 As you write longer programs, you are likely to
have several forms and event procedures that
use some of the same variables and routines.

 As we did all this time, variables are local to an


event procedure.
 They can be read or changed only within the event
procedure in which they were created.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 11


WORKING WITH MODULES
CONTD..
 If you declare variables at the top of a form’s
program code, it gives the variables a
greater scope, but limits only to that form.
 However, if you create multiple forms in
your project, the variables declared at the
top of a form are valid only in the form in
which they were declared.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 12


WORKING WITH MODULES
CONTD..
 To share variables and procedures among all
the forms and event procedures in a project,
you declare them in one or more modules
included the project.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 13


WHAT IS A MODULE?
 A Module a special file that has a .vb file
name extension and contains variable
declarations and procedures that can be used
anywhere in the program.
 Like forms, modules are listed separately in
Solution Explorer.
 Unlike forms, modules contain only code and
don’t have a user interface.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 14


CREATING A MODULE
 To Create a New module in a program,
you click the Add New Item button on
the standard tool bar or click the Add
New Item command on the project
menu.
 A dialog box opens from there select
Module template and specify the name of
the module.
 The first module of your program is
named Module1.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 15


A Module is
where global
variables and
sub procedures
can be declared

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 16


PROPERTIES OF MODULES
 Because a module contains just code, it has
only few properties.
 By using its most significant property, file
name you can rename the module.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 17


HOW TO REMOVE A MODULE
FROM A PROJECT
 Right click the module in solution explorer,
and click Exclude from Project menu.
 Exclude from Project command will not
doesn’t delete the module from the hard
disk.
 But it removes the link between the
specified module and the current project.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 18


PUBLIC VARIABLES

A Public variable is a variable whose scope


or lifespan is throughout the whole program.
This means that all sub procedures (or
functions) can make use of it.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 19


WORKING WITH PUBLIC VARIABLES
 A public variable has a global Scope, which
can be accessed from any place of your
project.
 Declaring a global or public variable in a
module is simple.
 You type the keyword “Public” followed by
the variable name and a type declaration.
 After declare the variable you can read it,
change it, or display it in any procedure of
your program.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 20


HOW TO DECLARE A PUBLIC
VARIABLE
 Syntax
Public VariableName As DataType

 For example
Public RunningTotal As Integer

 Note that the Public keyword has been


substituted for the Dim keyword which
you used to Declare a local variable.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 21


PUBLIC VARIABLES VS. FORM
VARIABLES
 Public variables maintain their values in all
the forms and modules in your project.
 Form variables maintain their values only in
that particular form which the variables are
declared.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 22


CREATING
PROCEDURES

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020


23
CREATING GENERAL
PROCEDURES
 Procedures provide a way to group a set of
related statements to perform a task.
 Visual Basic.NET include two primary types
of procedures.

1. Sub procedures
2. Function procedures

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 24


WORKING WITH
SUB PROCEDURES

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020


25
SUB PROCEDURES
What are Sub Procedures?

A Sub Procedure is a part of a program


that performs one or more related tasks,
has its own name, and written as a
separate part of a program.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 26


SUB PROCEDURES
 S.Ps are called by name from event
procedures or other procedures.
 They can receive arguments and also pass
back the modified values in an argument
list.
 But S.Ps don’t return values associated
with their particular Sub Procedures name.
 S.Ps are typically used to receive or
process input, display output, print output
or set properties.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 27


SUB PROCEDURE SYNTAX
Sub ProcedureName( [arguments] )
procedure Statement(s)
End Sub

 ProcedureName: The name of sub procedure


that you are creating.
 Arguments: A list of optional arguments,
separated by commas if there is more than one
to be used in the S.P.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 28


INVOKING SUB PROCEDURE

 To call/invoke a S.P. in a program, you specify


the name of procedure, and then list the
arguments required by the Sub Procedure.

ProcedureName(Argument1, Argument2…..)

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 29


WRITING A SUB PROCEDURE - EXAMPLE

Private Sub Button1_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles Button1.Click
Call add(2, 1)
End Sub
Arguments

Private Sub add(ByVal n1 As Integer, ByVal n2 As Integer)


Dim total As Integer
total = n1 + n2
MsgBox(total)
End Sub parameters

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 30


BYVAL KEYWORD
 Visual Studio adds the ByVal keyword by
default to each argument.
 ByVal indicates that a copy of the data is
passed to the function through this
argument.
 But any changes to the arguments won’t we
returned to the calling routing since it is a
sub procedure.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 31


NO ARGUMENTS – NO RETURN VALUE
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
No_arg_No_return()
End Sub
No arguments
Public Sub No_arg_No_return()
Dim x, y, z As Integer
x = 20
y = 30
z=x*y
Label1.Text = "TOTAL = " & z
End Sub

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 32


OUTPUT

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 33


PASSING ARGUMENTS BY VALUE
AND BY REFERENCE
 A we discussed earlier, Visual Studio adds the
ByVal keyword by default to each argument.
 ByVal indicates that variables should be
passed to a procedure by Value (default).
 Any changes made to a variable passed in by
value aren’t passed back to the calling
procedure.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 34


CALL BY REFERENCE
 Using the ByRef keyword, indicates that
variables should be passed to a procedure by
reference, meaning that, any changes made
to the variable in the procedure are passed
back to the calling routing.
 Call ByRef Passes the original value and ByVal
passes a copy of the variable to the S.P.
 Passing by reference can have significant
advanteges, as long as you’re careful not to
change a variable unintentionally in a
procedure.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 35


CALL BY REFERENCE - PROCEDURE

Private sub a()


dim a as integer
a=5
call square(a)
print a
end sub

private sub square( ByRef num as integer)


num = num * num
end sub

Ans a = 25
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 36
CALL BY VALUE
Private sub a()
dim a as integer
a=5
call square(a)
print a
end sub

private sub square (byval num as integer)


num = num * num
end sub

Ans a = 5

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 37


EXIT SUB

• EXIT SUB will alter the flow of control to


your program
• Executing EXIT SUB in a sub procedure
causes an immediate exit from that
procedure.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 38


WORKING WITH
FUNCTIONS

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020


39
TOPIC & STRUCTURE OF THE
LESSON
• Introduction
• Function Procedures
• Exit sub and exit function
• Generating (pseudo) random numbers

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 40


LEARNING OUTCOMES

At the end of this lecture you should be able


to :
1. Write a Function Procedure
2. Use Exit Sub and Exit Function statements
3. Generate random numbers

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 41


KEY TERMS YOU MUST BE ABLE TO USE
 If you have mastered this topic, you
should be able to use the
 following terms correctly in your
assignments and exams:

1. Function
2. Exit
3. RND

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 42


FUNCTION

A function is similar to a procedure


and has the task of allowing modules
to be built. It has the ability to return
a value as the answer.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 43


FUNCTION PROCEDURES
 Function Procedures are called by name from event
procedures or other procedures.

 Often used for calculations, function procedures can


receive arguments and always return a value in the
function name.

 Arguments are the data used make functions work, and


they must be included between parenthesis and
separate by commas.

 Basically, using a Function procedure is exactly like


using a built-in function or method such as Int, Rnd, or
FromFile

 Function in Modules are public by default. As a result,


you can use them in any event procedure within the
project.
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 44
FUNCTION FORMAT

Function FunctionName(var1 as type1,var2 As


type2,….) As ReturnDatatype
statement(s)
FunctionName = return statement
End Function

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 45


FUNCTION FORMAT

Private Function FunctionName(var1 as type1,var2


as type2,….) as datatype
statement(s) parameter list
FunctionName = expression
End Function
argument list
Called by:
X = FunctionName(arg1, arg2, …)
Parameters and arguments MUST AGREE in type and
number.

Slide 6 (of 30)


EXAMPLE
Private Sub Button1_Click_1(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Label1.Text = ""
Label1.Text = FtoC(Val(TextBox1.Text))
End Sub

Function FtoC(ByVal t As Integer) As Integer


FtoC = (5 / 9) * (t - 32)
End Function

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 47


EXIT FUNCTION
 Executing Exit Function causes an
immediate exit from the function
 Control is returned to the caller and the
next statement in sequence after the call
is executed

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 48


EXIT FUNCTION EXAMPLE
Private sub cmddivide_click()
Dim numerator , denominator as integer
Dim result as string
Numerator = CInt(txtnum.text)
Denominator = CInt(txtden.text)
Result = divide(numerator, denominator)
If result = “” then
MessageBox.Show ( “Divide by Zero attempted”)
Else
MessageBox.Show (result)
End if
End sub

Slide 20 (of 30)


EXIT FUNCTION EXAMPLE
Private function divide(n as integer, d as integer) as
string
Dim answer as integer
If d = 0 then
exit function
Else
answer = n / d
divide = “Integer division yields” & ans
End if
End Function

Slide 21 (of 30)


RETURN KEYWORD

 You can make use of the keyword return to


receive the values back from the called
function to the calling function.

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 51


EXAMPLE
Random Number Generation

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020


52
RANDOM NUMBER GENERATION
 The built-in VB function, RND, returns a random
number in the range: 0 <= random number < 1
 To generate a random number between some
other range: Low … High, use the following
formula:
 rand = Low + Int((High – Low + 1) * Rnd)

Example:
Dieface = 1 + Int(6 * rnd()) to generate random
numbers from 1 to 6

Slide 22 (of 30)


RANDOM NUMBER GENERATION

 RND function returns a random number in a


range
 a = 1 + Int(6 * rnd()) to generate random
numbers from 1 to 6

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 54


SAMPLE PROGRAM
Dim x As Integer
Dim a As Integer
Dim output As String

Private Sub Button1_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles Button1.Click
Randomize()
For x = 1 To 10
a = 1 + Int(Rnd() * 10)
output &= a & vbCrLf
Next

MessageBox.Show(output)

End Sub vbCrLf = Visual Basic Carriage Return Line Feed.


This is similer to chr(10) & chr(13)
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 55
EXERCISE

 Write a program that will generate a random


number from 5 to 100 when a command button
is pressed.

Slide 23 (of 30)


Summary of Main Teaching Points

 Function Procedures are often used to solve an


equation

 Returns a single value as the answer

 The arguments in the function invocation must match


the parameter list of the function in both type and
number

Slide 28 (of 30)


Next Session

Arrays

Slide 30 (of 30)


Question and Answer Session

Q&A

APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 11/14/2020 1- 59

You might also like