Home -> Programming -> Subroutines -> Procedures

Procedures

Early programs were created in a monolithic fashion. This means the program started at the top of the 'page' and continued until it reached the end which was invariably at the bottom of the 'page'. However, sometimes the flow of the program had to be redirected. This was initially done using a Goto statement which redirected program execution to the desired line. It did not take long, comparatively speaking until we found that such a programming style was counter productive to both program maintenance and program reliability.

Unfortunately, when BASIC made its debut, it contained this Goto statement. This perpetuated the monolithic spagetti-code programming style for a few years. On the positive side, BASIC also contained a GoSub statement which causes execution of a program to be directed to a section of code called a subroutine which was physically placed after the end of the main program. When a program encountered a GoSub statement, the program would jump to the subroutine and execute the instructions it found there. When the end of the subroutine was reached, the program was redirected to the point just after the GoSub statement and execution of the main program resumed from there. The most obvious advantage to this ability to create and use subroutines is that we can avoid repetition in our code. If we have a section of code that would be otherwise be placed in our programs in two or more places, we create a subroutine and put our code in the subroutine. Then whenever we need that section of code we sould simpley call it.

In Visual BASIC a suboutine is called a procedure. The names are interchangeable. A procedure is a module of code which performs a specific task. The keyword for declaring a procedure in Visual BASIC is Sub and it is the fundamental unit of organizing our code in VB. You must remember that quite a lot of the code for your program has already been written for you. The only code you need to write is the code which makes your program unique. All of this programming will be placed in one or more subs depending on the complexity of your program.

Subroutines therefore have two main advantages. First: they make program and code maintenance easier. When we need to correct or update that section of code, having it in a subroutine means we only have to update it in one place. This makes it easeir to find and easier to maintain or update. Second: they reduce code duplication.

Under the principles of structured programming there is only one way into the procedure, only one way out, and a procedure does one and only one thing. If you need two things done, write two procedures. Using procedures and following the principles of structured programming helps keep the code simple. This makes finding and fixing errors simpler. It also improves the reliability of the program - meaning it doesn't crash as often! Structured programming's intent is to simplify our code and improve its reliability. Procudures are one the main ways to accomplish this goal.

As mentioned above sometimes we find we need the same or very similar section of code in two or more places. By placing this section of code in a procedure, we can write the code once, but use it (call it) any time we need that task performed. This not only makes our code more efficient, but also makes it more reliable. There is only one location for all the code for that particular task. If we have an error in the code we know just where it is located. By avoiding duplication in our code we avoid having to remember, find, and update more than one occurrence of the task. Once we finish the code for the procedure, it doesn't change unless we want to upgrade it.

Using Procedures

Using a procedure is very simple. For example, lets say we have a procedure called Circumference, which calculates the circumference of a circle. We call it using either one of the following two statements. The are perfectly interchangeable.

Circumference

or

Call Circumference

This will cause program execution to jump to the procedure and execute the instructions found there. When the procedure is finished program will continue at the instruction following the call.

Creating a Simple Procedure

The basic structure of a procedure consists of a procedure declaration, a comment on what the procedure does, the code to do the task, and an end statement. It looks like this:

Public/Private Sub ProcedureName
    'Comment goes here

    'Code goes here

End Sub

Explanation

  1. Each procedure starts with a declaration which indicates the procedure's scope. Scope determines what other code modules can see, and hence call or activate this procedure. Private means this procedure can only be called from within this form. Public means that code in other forms can call this procedure.
  2. You must include the keyword Sub so that the program knows this is a procedure declaration rather than a function declaration.
  3. You must provide a name for the procedure and it must comply with all the rules for creating Identifiers. In VB names for variables, procedures, functions etc. are called identifiers. Rules for identifiers are found here.
  4. It is customary to include a brief comment to indicate what task is being performed by the procedure.
  5. The code to accomplish the task of the procedure goes after the procedure declaration, which is the first line, and before the procedure close, which is the last line. All procedures must have a declaration and a close. Programmers will sometimes write just the declaration and close without the code. This is called a stub. If we write code to call a procedure before it is created, we get a syntax error, but if we have written the stub there is no error message displayed. Of course the procedure does nothing until we write the code.
  6. The close for a procedure is always: End Sub.

For example a Procedure that is used in every program you will create is the Exit procedure. It would look something like this:

Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
    'Terminate the program
    Me.Close()
End Sub

Adding Parameters

One of the other things we can do with procedures is to pass information into them. Information passed into a procedure or function is called either a parameter or an argument. For example say we want to calculate the circumference of a circle. The formula is simple but it requires 2 pieces of information, the value of PI and the radius of the circle. Now the value of PI is a constant which we could define anywhere inside our program, but the radius is a value that would usually be passed in to the procedure. This procedure would look like this:

Private Sub Circumference(ByVal radius as Single)
    'Calculate the radius of a circle given the value of its radius
    lblCircumference.Text = 2 * 3.14159 * radius
End Sub

There are two differences between this procedure and the exit procedure. First there is some extra information on the declaration line of the procedure. This is the parameter declaration. Notice it is inside brackets. Actually all procedures end in brackets even if there are no parameters. If the brackets are empty then there are no parameters! We can actually declare as many parameters inside the brackets as we need. We just list them sequentially and separate them by a comma.

A parameter declaration is composed of the following and is contained in brackets.
  1. An opening bracket.
  2. Either ByVal or ByRef. ByVal means we are passing in a value only whereas ByRef means we are passing a memory location, a reference to a variable. The difference is simple. If we want to protect our variable and make sure the procedure cannot alter the variable's value then we use ByVal. The procedure can use the value but does not know where it lives and so cannot alter it. If we want to pass in a variable and have it altered then we use ByRef. You will see an example of this later.
  3. The name of the variable. This is in every sense of the word a declaration of a variable which only exists inside this procedure. It is a local variable, but since it is declared as part of the procedure declaration it allows a value or reference to be passed into the procedure. If it was declared inside the procedure it would be local, but could not be used to pass data into the procedure.
  4. The keyword 'as' so we can indicate what type of data the variable is to be.
  5. The data type we need for our parameter. The only limitation here is that whatever data type we specify limits what data we can pass.
  6. A closing bracket.

The second difference is of course the formula which calculates the circumference. Here we are assigning the result of the calculation to a label on the form which will display the circumference.

When declaring more than one variable there are three rules which must be followed so that the parameters will work properly. The three rules are: For Example:

Call ProcedureName (parameter1, parameter2, parameter3)

Private Sub ProcedureName (ByVal argument1 as Integer, argument2 as Single, argument3 as String)

In this example parameter1 maps to argument1 and the data in parameter1 must be Integer; parameter2 maps to argument2 and the data in parameter2 must be Single; parameter3 maps to argument3 and the data in parameter3 must be String.