This documentation is archived and is not being maintained.

Making a Computer Do Something: Writing Your First Procedure

Visual Studio 2008

In this lesson, you will learn how to create a procedure, a self-contained block of code that can be run from other blocks of code. You will then learn how to create parameters for your procedures.

A procedure is just a block of code that tells the program to perform an action. Although you may not have realized it, you have already used procedures in the previous lessons. For example, the MsgBox function is a built-in procedure that performs the action of displaying a dialog box.

While Visual Basic has many built-in procedures for performing common actions, there will always be cases when you want your program to perform an action that a built-in procedure can't handle. For example, the MsgBox function cannot display a dialog box that has a picture. You have to write your own procedure to accomplish this task.

A procedure is a self-contained block of code that can be run from other blocks of code. Generally speaking, procedures each contain the code that is required to accomplish one task. For example, you might have a procedure called PlaySound, which contains the code that is required to play a wave file. While you could write the same code to play a sound every time your program had to make a noise, it makes more sense to create a single procedure that you can call from anywhere in your program.

A procedure is run, or executed, by calling it in code. For example, to run the PlaySound procedure, you just add a line of code that has the name of your procedure, as shown here.


That's all there is to it! When the program reaches that line, it will jump to the PlaySound procedure and execute the code that is contained there. The program will then jump back to the next line that follows the call to PlaySound.

You can call as many procedures as you like. Procedures are run in the order that they are called. For example, you might also have a procedure called DisplayResults; to execute it after executing the PlaySounds procedure, call the procedures as shown here.



There are two kinds of procedures: functions and subroutines (sometimes referred to as subs). A function returns a value to the procedure that called it, whereas a sub just executes code. Subs are called when a line of code that contains the name of the sub is added to the program, as in the following example.


Functions are different, because functions not only execute code, they also return a value. For example, imagine a function called GetDayOfWeek that returns an Integer indicating the day of the week. You call this function by first declaring a variable in which to store the return value, and then assigning the returned value to the variable for later use, as shown here.

Dim Today As Integer

Today = GetDayOfWeek

In this example, the value returned by the function is copied to the variable named Today and stored for later use.

You write procedures by first writing a procedure declaration. A procedure declaration does several things. It states whether the procedure is a function or a sub, it names the procedure, and it details any parameters the procedure might have. (Parameters will be discussed in detail later in this lesson.) The following is an example of a simple procedure declaration.

Sub MyFirstSub()
End Sub

The Sub keyword tells the program that this procedure is a sub and will not return a value. The name of the sub (MyFirstSub) comes next, and the empty parentheses indicate that there are no parameters for this procedure. Finally, the End Sub keyword indicates the end of the subroutine. All code that is to be executed by this sub goes between these two lines.

Declaring functions is similar, but with the added step that the return type (such as Integer, String, etc.) must be specified. For example, a function that returned an Integer might look like this.

Function MyFirstFunction() As Integer 
End Function

The As Integer keywords indicate that this function will return an Integer value. To return a value from a function, use the Return keyword, as shown in the following example.

Function GetTheNumberOne() As Integer 
    Return 1
End Function

This procedure will return the number 1.

To create procedures

  1. On the File menu, click New Project.

  2. In the New Project dialog box, in the Templates pane, click Windows Application.

  3. In the Name box, type MyFirstProcedure and then click OK.

    A new Windows Forms project opens.

  4. Double-click the form to open the Code Editor.

  5. In the Code Editor, locate the line that reads End Class. This is the end of the code section that makes up the form. Immediately before this line, add the following procedure:

    Function GetTime() As String 
      Return CStr(Now)
    End Function

    This function uses the built-in Now procedure to receive the current time, and then uses the CStr function to convert the value returned by Now into a human-readable String. Finally, that String value is returned as the result of the function.

  6. Above the function you added in the previous step, add the following Sub.

    Sub DisplayTime()
    End Sub

    This sub calls the function GetTime and displays the result returned by it in a message box.

  7. Finally, add a line to the Form1_Load event handler that calls the DisplayTime sub, as shown here.

  8. Press F5 to run the program.

    When the program starts, the Form1_Load event procedure is executed. This procedure calls the DisplayTime sub, so program execution jumps to the DisplayTime sub procedure. That sub in turn calls the GetTime function, so program execution then jumps to the GetTime function. This function returns a String representing the time to the DisplayTime sub procedure, which then displays that string in a message box. After the sub is finished executing, the program continues normally and displays the form.

At times you will have to provide additional information to your procedures. For example, in the PlaySound procedure, you may want to play one of several different sounds. You can provide the information about which sound to play by using parameters.

Parameters are much like variables. They have a type and a name, and they store information just like variables. They can be used just like variables in a procedure. The two main differences between parameters and variables are as follows:

  • Parameters are declared in the procedure declaration, not in individual lines of code.

  • Parameters are usable only in the procedure they are declared in.

Parameters are declared in the procedure declaration in the parentheses that follow the procedure name. The As keyword is used to declare the type, and each parameter is generally preceded by the ByVal keyword. This keyword will be added automatically by Visual Basic if you do not add it, and it has a fairly advanced function that is beyond the scope of this lesson to explain.

An example of a sub with parameters is shown here.

Sub PlaySound(ByVal SoundFile As String, ByVal Volume As Integer)
  My.Computer.Audio.Play(SoundFile, Volume)
End Sub

You would then call the sub with the values for the parameters as shown here.

PlaySound("Startup.wav", 1)

You can also declare parameters for functions in the same way you would for subs.

To create a function that has parameters

  1. On the File menu, click New Project.

  2. In the New Project dialog box, in the Templates pane, click Windows Application.

  3. In the Name box, type parameters and then click OK.

    A new Windows Forms project opens.

  4. From the Toolbox, drag two Textbox controls onto the form.

  5. From the Toolbox, drag a Button control onto the form.

  6. Double-click the Button to open the Code Editor.

  7. Immediately after the End Sub line of the Button1_Click event handler, add the following procedure:

    Function AddTwoNumbers(ByVal N1 As Integer, ByVal N2 As Integer) _
      As Integer 
      Return N1 + N2
    End Function
  8. In the Button1_Click procedure, add the following code:

    Dim aNumber As Integer = CInt(Textbox1.Text)
    Dim bNumber As Integer = CInt(Textbox2.Text)
    MsgBox(AddTwoNumbers(aNumber, bNumber))

    This code declares two integers and converts the text in the two text boxes to integer values. It then passes those values to the AddTwoNumbers function and displays the value returned in a message box.

  9. Press F5 to run the program.

  10. Type a number value in each text box and click the button. The two numbers are added, and the result is displayed in a message box.

In this lesson, you learned the difference between functions and subs, and how to create each of these two procedures. You have also learned how to call procedures and how to create procedures that have parameters.

In the next lesson, you will learn how to use the For...Next statement to repeat actions.

Next Lesson: Making a Program Repeat Actions: Looping with the For...Next Loop