Visual Basic 2008 9.0 .NET Examples and Ebook
  Home

Procedures and Functions

Vorig Onderwerp

Arrays

|

Object Oriented Programming

Volgend Onderwerp

Introduction to Procedures

Vorig Onderwerp

Sorting Arrays - Insertion Sort

|

Arguments - ByVal and ByRef

Volgend Onderwerp
What are Procedures ?

What are Procedures ?



Next example will convert centimetres to inches and inches to centimetres.


Module Example1
    Sub Main()
        Dim menu As Char
        Dim value As Decimal
        '
        Console.Write("1 centimetres -> inches / 2 - inches -> centimetres : ")
        menu = Console.ReadLine()
        Do Until menu = "1"c OrElse menu = "2"c
            Console.WriteLine("An error occurred, please try again.")
            Console.Write("1 centimetres -> inches / 2 - inches -> centimetres : ")
            menu = Console.ReadLine()
        Loop
        '
        Console.Write("Value : ")
        value = Console.ReadLine()
        Do Until value > 0
            Console.WriteLine("An error occurred, please try again.")
            Console.Write("Value : ")
            value = Console.ReadLine()
        Loop
        '
        If menu = "1"c Then
            Console.WriteLine(value & " centimetres is " & value * 0.3937 & " inches")
        Else
            Console.WriteLine(value & " inches is " & value * 2.54 & " centimetres")
        End If
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 1 centimetre -> inches / 2 - inches -> centimetres : 3
 An error occurred, please try again.
 1 centimetres -> inches / 2 - inches -> centimetres : 4
 An error occurred, please try again.
 1 centimetres -> inches / 2 - inches -> centimetres : 1
 Value : -5
 An error occurred, please try again.
 Value : -10
 An error occurred, please try again.
 Value : 10
 10 centimetres is 3,937 inches

When an invalid menu-option is chosen, or a negative value is entered an error is produces.

Both situations will use the same instruction(s) to produce the error.

To avoid coding the same instruction more than once, "procedures" ( also called "subroutines" or "functions" ) can be used.


What are Procedures ?


When a certain functionality is needed on different occasions in a program ( at different point in code ) a procedure can be used to avoid coding the same instructions ( responsible for implementing that functionality ) more than once.

A procedure can be defined once, and called whenever needed.

The Sub Main() ... End Sub used in the above example is a procedure ( with identifier Main ). This Main procedure is needed when an module is set as startup-object of a project ( an assembly / a program ).

The definition of a procedure starts with Sub and ends with End Sub.
The opening Sub is followed by the identifier ( the name ) of that procedure.

Next example defines a procedure ShowError responsible for printing an error on the console.
Whenever our program needs to produce an error, we can call this procedure.


Module Example2
    Sub Main()
        Dim menu As Char
        Dim value As Decimal
        '
        Console.Write("1 centimetres -> inches / 2 - inches -> centimetres : ")
        menu = Console.ReadLine()
        Do Until menu = "1"c OrElse menu = "2"c
            ShowError()
            Console.Write("1 centimetres -> inches / 2 - inches -> centimetres : ")
            menu = Console.ReadLine()
        Loop
        '
        Console.Write("Value : ")
        value = Console.ReadLine()
        Do Until value > 0
            ShowError()
            Console.Write("Value : ")
            value = Console.ReadLine()
        Loop
        '
        If menu = "1"c Then
            Console.WriteLine(value & " centimetres is " & value * 0.3937 & " inches")
        Else
            Console.WriteLine(value & " inches is " & value * 2.54 & " centimetres")
        End If
        '
        Console.ReadLine()
    End Sub
    Sub ShowError()
        Console.WriteLine("An error occurred, please try again.")
    End Sub
End Module
Download Broncode

Output :

 1 centimetres -> inches / 2 - inches -> centimetres : 3
 An error occurred, please try again.
 1 centimetres -> inches / 2 - inches -> centimetres : 4
 An error occurred, please try again.
 1 centimetres -> inches / 2 - inches -> centimetres : 1
 Value : -5
 An error occurred, please try again.
 Value : -10
 An error occurred, please try again.
 Value : 10
 10 centimetres is 3,937 inches

There is no specific order in which the procedures need to be defined.

A call to a procedure is done by using the identifier of that procedure. The parentheses following the identifier are often optional and will be explained later.

Also chose an identifier that will explain as much as possible what the procedure will do, what the responsibility is of that procedure. Here ShowError is responsible for showing an error.

When a procedure is finished the program continues with the code following the call to this procedure.

When a program has lots of procedures, and these procedure all call each other, it can be difficult to keep track of how - at a certain point at runtime - a certain procedure was called, or who called that certain procedure. A callstack debug tool in your IDE ( programming environment ) can come to rescue.


Visual Studio : Visual Studio has a callstack tool, to trace all calls to procedures.

This tool behaves like a stack ( LIFO or last in, first out ). The latest call to a certain procedure will either call another procedure or finish and be removed from the stack.


Updated On : 2008-01-28

Download Broncode

Published On : 2008-11-06

Introduction to Procedures

Vorig Onderwerp

Sorting Arrays - Insertion Sort

|

Arguments - ByVal and ByRef

Volgend Onderwerp

Procedures and Functions

Vorig Onderwerp

Arrays

|

Object Oriented Programming

Volgend Onderwerp
  Home  
Nederlands
Nederlands

Add to favorites (IE).


No printable version available.