VB .Net

Procedures and Functions

·        Sample Program. Download and add it to a new project.

·        Creating and Using Sub Procedures

·        Creating and Using Function Procedures (programmer-defined)

Sub Procedures:

Structured program design requires that problem be broken into small problems to be solved one at a time.  Breaking down a problem into small procedures allows easier problem solving and repeated use of the codes. The Visual Basic has two devices, sub procedures (Procedures) and Function procedures (Functions), that are used to break problems into manageable chunks.  To distinguish them from event procedures, Sub and Function procedures are referred to as general procedures.  General procedures also eliminate repetitive code, can be reused in other programs, and allow a team of programmer to work on a single programs.

A Sub procedure is a part of a program that performs one or more related tasks, has its own name, and is written as a separate part of the program.

The Syntax

Private Sub ProcedureName( parameters )

            statement(s)

End Sub

A Sub procedure is invoked with a statement as:

ProcedureName( parameters )

The basic goal of naming general procedures are the same as naming variables.  The name chosen for a Sub procedure should describe the task it performs.  Sub procedure can be either typed directly into the code window.

Consider the following program that calculates the sum of two numbers.

Private Sub cmdAdd_Click( )
    Dim intNum1 As Integer
    Dim intNum2 As Integer
    Dim intSum As Integer

    intNum1 = 2
    intNum2 = 3

    intSum = intNum1 + intNum2

    lblSum.Text = Str(intSum)
End Sub

The program is in an event procedure, and it has nothing new.

Sub procedures allow you to write the program in such a way that are first focus on the tasks and later on how to accomplish each task.

The following program uses a Sub procedure to accomplish the same task of the preceding program.

Private Sub cmdAdd_Click( )
    Dim intNum1 As Single
    Dim intNum2 As Single
    Dim intSum As Integer

    GetTwoIntegerNumbers (intNum1, intNum2)

    AddTheTwoNumbers (intNum1, intNum2, intSum)

    DisplayTheSum (intSum)
   
End Sub


Private Sub GetTwoIntegerNumbers (ByRef intNum1 As Integer, ByRef intNum2 As Integer)
    intNum1 = 2
    intNum2 = 3
End Sub


Private Sub AddTheTwoNumbers (ByVal intNum1 As Integer, ByVal intNum2 As Integer, ByRef intSum as Integer)
    intSum = intNum1 + intNum2
End Sub


Private Sub DisplayTheSum(ByVal intSum as Integer)
    lblSum.Text = Str(intSum)
End Sub

The following revision causes the program to display the calculation several sums.

Private Sub cmdAdd_Click( )
    Dim intSum As Integer
         
    AddTwoNumbers(1, 2, intSum)
    AddTwoNumbers(2, 3, intSum)
    AddTwoNumbers(3, 4, intSum)

End Sub


Private Sub AddTwoNumbers(ByVal intNum1 As Integer, intNum2 As Integer, ByRef intSum As Integer)

    intSum = intNum1 + intNum2
   
End Sub

 


Function Procedure

In addition to built-in functions, you can define functions of your own.  User-defined functions work the way built-in functions work.  None or more input provided, a function returns one value.  Functions have a single output that can be numeric or string.  Functions can be used in expressions in exactly same way as built-in function.  Programs refer to them as if they were constant, variables, or expressions. 

A function can be typed directly into the code window with:

Private Function name_of_function (parameters) As return_value_data_type

         codes here......

End Funciton

 

Functions are defined by function blocks of the form. 

The Syntax

Private Function FunctionName (parameter1 As data_type, parameter2 As data_type, …) As return_ata_type

            Statement(s)

            Return value_that_the_function_returns

End Function

The parameters inside the function block that are not ByRef parameters have local scope.  Function names should be suggestive of the role performed and must conform to the rules for naming variables.  The return_ata_type at the end of the function heading specifies the data type of the return value. 

A Function is invoked with a statement of the form. It is basically, the same syntex as you use built-in function.

variable1 = FunctionName (paramenter1, parameter2,...)

 

Examples:


'Display the sum of two numbers
Private Sub cmdAdd_Click()
   Dim total As Single
   ExplainPurpose ( )                       'prints the purpose of the program.
   total = AddNums(2, 3)                   'use the AddNums function to get the total of 2 and 3.
   PrintNums(2, 3, total)
End Sub

'Explain the task performed by the program
Private Sub ExplainPurpose( )
   MsgBox ("This program displays a sentence identifying two numbers and their sum")
End Sub

'Display numbers and their sum.
Private Sub PrintNums(ByVal num1 As Single, ByVal num2 As Single, _
            ByVal theTotal As Single)
   MsgBox ( "The sum of " & num1 & " and " & num2 & " is " & theTotal )
End Sub

'Adds two numbers and returns the total.
Public Function AddNums(numA As Single, numB As Single) As Single
   Dim theTotalValue As Single
   theTotalValue = numA + numB
   Return theTotalValue
End Function

Remarks: Notice that cmdAdd_Click, the main procedure, became very simple and easy to read. All the details are allocated to individual procedures and functions.

The main difference of a function from a procedure is that the a function returns one value while a procedure may retrun none or many.