Home

Introduction to Procedures

 

Introduction to Procedures

 

Procedures

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 VBasic, like most other languages, there are two types of procedures: functions and sub routines (many other languages like Pascal make this distinction; some other languages like C++ and C# don't explicitly differentiate both categories).

Introduction to Sub-Procedures

A sub procedure is an assignment that is carried but doesn't 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 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, omitting the prefix. 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 Assignment()

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 Assignment()
    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 Assignment()
    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. Set the Name to Square1 and click OK
  5. To use a few string variables, change the program as follows:
      
    
            
  6. Execute the application and provide the requested values. Here is an example:
     
  7. Return to your programming environment

Calling a Sub Procedure

Once you have a procedure, whether you created it or it is part of VBasic, 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. Here is an example:

Module Module1

    Sub SitDown()
        MsgBox("This program expects that you are sitting down!")
    End Sub

    Sub Main()
        SitDown()
    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 Module1

    Sub SitDown()
        MsgBox("This program expects that you are sitting down!")
    End Sub
    Sub Main()
        Call SitDown()
    End Sub

End Module

This would produce:

This program expects that you are sitting down!

Practical Learning: Calling a Sub Procedure

  1. To call the procedure, type its name in the Main() procedure
     
    Module Module1
    
        Sub SquarePerimeter()
            Dim dblSide As Double
            Dim dblPerimeter As Double
    
            Console.Write("Enter Side: ")
            dblSide = Console.ReadLine()
    
            dblPerimeter = dblSide * 4
    
            MsgBox("Square Characteristics")
            MsgBox("Side:         {0:F}", dblSide)
            MsgBox("Perimeter: {0:F}", dblPerimeter)
        End Sub
    
        Sub Main()
            SquarePerimeter()
        End Sub
    
    End Module
  2. Compile the application by typing vbc Exercise.vb and press Enter
  3. Execute the application by typing Exercise
     
  4. Close the form and return to MSVB
 
 

Previous Copyright © 2004-2007 FunctionX, Inc. Next