Visual Basic VB: Procedures - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Visual Basic VB: Procedures

Description:

Why Using Procedures. Both Subs and Functions useful to provide reusable chunks of code ... Storage reserved on entry and destroyed at exit from procedure ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 18
Provided by: rickd74
Category:

less

Transcript and Presenter's Notes

Title: Visual Basic VB: Procedures


1
Visual Basic (VB)Procedures
2
Procedures in VB
  • VB provides two types of procedure
  • Sub Procedures.
  • Function Procedures.

3
Sub Procedures
  • Weve already met Sub procedures when we were
    handling events that were happening to form
    objects.
  • Private Sub Button1_Click()
  • stuff(a)
  • End Sub
  • However, you can write Subs that dont handle
    events.
  • Sub stuff(ByVal i As Integer)
  • Debug.WriteLine(Str(i))
  • End Sub

Wheres the Dim? Wheres i declared?
4
Cont. Sub Procedures
  • Sub Definition
  • Private Sub Method1(parameter list)
  • body
  • End Sub
  • Sub call
  • Method1(argument list)

5
Function Procedures
  • Function Procedures are different though
  • They return a value
  • Can be called anywhere an expression is used
  • For example, on the right hand side of an
    assignment statement
  • Weve seen some built-in ones before, eg CStr

6
Cont. Function Procedures 1
Calling a Function Dim a As Integer Dim b As
Integer Dim c As Integer c add(a, b)
Function Definition Function add(ByVal i As
Integer, ByVal j As Integer) As Integer
add i j End Function
If a1 and b2, what does c become?
7
Cont. Function Procedures 2
  • Function definition
  • Private Function Method1(parameter list)
    Return type
  • body
  • End Function
  • Function call
  • type x Method1(argument list)

8
Sub V Function Procedures
  • Functions
  • Return a value before exiting the Function.
  • This value must match the Function return type.
  • Subs
  • Do not return a value.
  • Therefore, cannot be used in assignment
    statements.

9
Why Using Procedures
  • Both Subs and Functions useful to provide
    reusable chunks of code
  • Can be called in different contexts
  • Or even reused in different applications
  • Reuse is important to RAD.
  • If you ever write essentially the same piece of
    code twice, think of using a procedure.

10
ByVal versus ByRef
  • In VB, arguments in procedures can be passed by
    value or by reference.
  • When passing by value, copies of the arguments
    are passed.
  • When passing by reference, the address of
    arguments are passed.

11
Swap Example Without Using a Procedure
  • Dim a As Integer 1
  • Dim b As Integer 2
  • Dim temp As Integer
  • temp a
  • a b
  • b temp

12
Swap Example using a Procedure (call by Value)
  • Dim a As Integer 1
  • Dim b As Integer 2
  • Private Sub swap(ByVal x As Integer, ByVal y As
    Integer)
  • Dim temp As Integer
  • temp x
  • x y
  • y temp
  • End Sub
  • .
  • .
  • Swap(a, b) a ? , b ?

13
Swap Example using a Procedure (call by
Reference)
  • Dim a As Integer 1
  • Dim b As Integer 2
  • Private Sub swap(ByRef x As Integer, ByRef y As
    Integer)
  • Dim temp As Integer
  • temp x
  • x y
  • y temp
  • End Sub
  • .
  • .
  • Swap(a, b) a ? , b ?

14
The scope of a variable
  • defines which parts of the code are aware of
    its existence.
  • When we declare a variable within a procedure,
    only the code in that procedure can access or
    change its value
  • The variable has a scope that is local to that
    procedure.
  • Storage reserved on entry and destroyed at exit
    from procedure
  • But sometimes we need to use a variable with a
    broader scope
  • eg, available to all the procedures within the
    same module, or even to all the procedures in the
    application.
  • VB allows you to specify the scope of a variable
    when you declare it.
  • But how?

15
Dim, Public and Private
  • Dim
  • Declares variables and allocates them storage
    space.
  • Variables at the module level available to all
    procedures within the module.
  • At the procedure level, available only within the
    procedure.
  • Public
  • Used at the module level to declare public
    variables and allocate storage space.
  • Variables available to all procedures in all
    modules.
  • Private
  • Used at the module level to declare private
    variables and allocate storage space.
  • Private variables are available only to the
    module in which they are declared.
  • There are other related keywords, eg Protected,
    Friend, Protected Friend, Static that you may
    come across and can explore for yourself.

16
Procedure Visibility
  • As well as being able to specify the visibility
    of variables, we can do the same with procedures
  • Public Sub/Function.
  • Indicates that the Function or Sub procedure is
    accessible to all other procedures in all
    modules.
  • Private Sub/Function.
  • Indicates that the Function or Sub procedure is
    accessible only to other procedures in the module
    where it is declared.

17
Things to Do
  • In the meantime, your coursework should be well
    underway!
  • Remember, you should be working outside of
    timetabled classes.
  • Also remember, developing away from the labs can
    be problematic when we assess your work.
  • Also, read and do the exercises in chapters 5, 6,
    10 and 11 of Bell and Parrs Visual Basic .NET
    for students in the next week.
  • Or Sections 1.3, 7.1, 7.2, 7.3, 7.4 and
    exercises of Introduction to Programming Using
    Visual Basic" by Gary Bronson
  • You might also want to try the exercises given on
    the second tutorial sheet available from the
    module Web pages.
Write a Comment
User Comments (0)
About PowerShow.com