Home

Introduction to Procedures

 

Introduction

A procedure is a set-aside assignment the compiler must take care of to complement a program. A program developer writes the procedure and a user (the user is either you or another programmer) would only see the result. There are two categories of procedures you will use in your programs: those that have already been created thus made available to you, and those you will create yourself.

In the Visual Basic language, like most other languages, there are two types of procedures: functions and sub routines.

Introduction to Sub-Procedures

A sub procedure is an assignment that is carried but does not give back a result. To create a sub procedure, start by typing the Sub keyword followed by a name (like everything else, a procedure must have a name). The name of a procedure is always followed by parentheses. At the end of the sub procedure, you must type End Sub. Therefore, the primary syntax of a sub procedure is:

Sub ProcedureName()

End Sub

The name of a procedure should follow the same rules we learned to name the variables. In addition:

  • If the procedure performs an action that can be represented with a verb, you can use that verb to name it. Here are examples: show, display
  • To make the name of a procedure stand, you should start it in uppercase. Examples are Show, Play, Dispose, Close
  • You should use explicit names that identify the purpose of the procedure. If a procedure would be used as a result of another procedure or a control's event, reflect it on the name of the sub procedure. Examples would be: afterupdate, longbefore.
  • If the name of a procedure is a combination of words, you should start each word in uppercase. Examples are: AfterUpdate, SayItLoud

The section between the Sub and the End Sub lines is referred to as the body of the procedure. Here is an example:

Sub Assign()

End Sub

The body of the procedure is used to define what, and how, the assignment should be carried. For example, if you need to use a variable, you can declare it and specify the kind of variable you need. There is no restriction on the type of variables that can be declared in a procedure. Here is an example in which a string variable is declared in the body of a sub routine:

Sub Assign()
    Dim strFullName As String
End Sub

In the same way, you can declare as many variables as you need inside of a procedure. The actions you perform inside of a procedure depend on what you are trying to accomplish. For example, a procedure can simply be used to create a string. The above procedure can be changed as follows:

Sub Assign()
    Dim strFullName As String
    strFullName = "Paul Bertrand Yamaguchi"
End Sub

Practical Learning: Introducing Procedures

  1. Start Microsoft Visual Basic
  2. To create a new application, on the main menu, click File -> New -> Project or File -> New Project
  3. In the Templates list, click Console Application
  4. In the Name text box, type Squared
  5. In the Solution Name text box, type Geometry1
  6. Click OK
  7. In the Solution Explorer, under the Square node, right-click Module1.vb and click Rename
  8. Type Square.vb and press Enter
  9. To create a procedure, change the document as follows:
      
    Public Module Square
    
        Sub ProcessSquare()
            Dim dblSide As Double
            Dim dblPerimeter As Double
    
            dblSide = InputBox("Enter Side: ")
    
            dblPerimeter = dblSide * 4
    
            MsgBox("=-= Square Characteristics=-=" & vbCrLf & _
                   "Side:      " & dblSide & vbCrLf & _
                   "Perimeter: " & dblPerimeter)
        End Sub
    
        Sub Main()
    
        End Sub
    
    End Module
  10. Save the document

Calling a Sub Procedure

Once you have a procedure, whether you created it or it is part of the Visual Basic language, you can use it. Using a procedure is also referred to as calling it. Before calling a procedure, you should first locate the section of code in which you want to use it. To call a simple procedure, type its name followed by parentheses in the section where you want to use it. Here is an example:

Module Exercise

    Sub Assign()
        Dim strFullName As String
        strFullName = "Paul Bertrand Yamaguchi"

        MsgBox(strFullName)
    End Sub

    Friend Sub Main()
        Assign()
    End Sub

End Module

Besides using the name of a procedure to call it, you can also precede it with the Call keyword. Here is an example:

Module Exercise

    Sub Assign()
        Dim strFullName As String
        strFullName = "Paul Bertrand Yamaguchi"

        MsgBox(strFullName)
    End Sub

    Friend Sub Main()
        Call Assign()
    End Sub

End Module
 
 

Practical Learning: Calling a Sub Procedure

  1. To call the procedure, type its name in the Main() procedure as follows:
     
    Module Central
    
        Sub ProcessSquare()
            Dim dblSide As Double
            Dim dblPerimeter As Double
    
            dblSide = InputBox("Enter Side: ")
    
            dblPerimeter = dblSide * 4
    
            MsgBox("=-= Square Characteristics=-=" & vbCrLf & _
                   "Side: " & dblSide & vbCrLf & _
                   "Perimeter: " & dblPerimeter)
        End Sub
    
        Sub Main()
            ProcessSquare()
        End Sub
    
    End Module
  2. Execute the application and enter the side of the square as 48.14
     
    Square
  3. Close the DOS window and return to your programming environment

Procedures and Access Modifiers

Like a variable, a procedure can use access modifiers. A procedure can be made a private procedure, a friendly procedure, or a public procedure, using the Private, the Friend, or the Public keywords respectively:

  • Private: A procedure marked as private can be called only by members of the same module
  • Friend: A friendly procedure can be called by members of the modules of the same application
  • Public: A public procedure can be called from its program and other programs

Practical Learning: Access Modifying a Procedure

  1. To control the access to a procedure, change the file as follows:
     
    Public Module Square
    
        Private Sub ProcessSquare()
            Dim dblSide As Double
            Dim dblPerimeter As Double
    
            dblSide = InputBox("Enter Side: ")
    
            dblPerimeter = dblSide * 4
    
            MsgBox("=-= Square Characteristics =-=" & vbCrLf & _
                   "Side: " & dblSide & vbCrLf & _
                   "Perimeter: " & dblPerimeter)
        End Sub
    
        Public Sub Main()
            ProcessSquare()
        End Sub
    
    End Module
    
  2. Execute the application and enter the side of the square as 105.15
  3. Close the DOS window and return to your programming environment
 
 

Home Copyright © 2008-2009 FunctionX Next