Modules, Functions and Procedures

Introduction to Modules and Procedures and function

Introduction
A module is a file, also called a source file (as in C/C++, Pascal, and other languages) that belongs to a program and in which you write code. For example, while we were using forms so far, we couldn't write code on a form. We had to access the Code Editor.
 Practical Learning: Introducing Modules
  1. Start Visual Basic and, in the opening dialog box, double-click Standard EXE
  2. To save the project, on the main menu, click File -> Save Project
  3. Locate your MS Visual Basic folder created in the first lesson and display it in the Save In combo box
  4. In the Save File As dialog box, click the Create New Folder button Create New Folder
  5. Type Modules1 and press Enter twice to display the new folder in the Save In combo box
  6. Replace the name of the file in the File Name edit box with Main and press Enter
  7. Type Modules1 to replace the name of the project and press Enter. If are asked whether you want to add the project to SourceSafe, click No
  8. Display the form and design it as follows:
     
    Geometric Calculations
    ControlNameCaptionAdditional Properties
    LabelSide:
    TextBoxtxtSide0.00Alignment: 1-Right Justify 
    LabelPerimeter:
    TextBoxtxtPerimeter0.00Alignment: 1-Right Justify
    ButtoncmdCalcPerimeterCalculate
    LabelArea:
    TextBoxtxtArea0.00Alignment: 1-Right Justify
    ButtoncmdCalcAreaCalculate
  9. Save all

Creating a Module
There are two main ways you obtain a module for your program.

  • When you create a form, a module is automatically created and associated to it. 
  • You can access the module of a form by right-clicking it and click View Code. 
  • Besides the modules created for your forms, you can create your own modules any time. 
  • To create a new module, on the main menu, you can click Project -> Add Module
  • In the Add Module dialog box, you can click Open. 
  • Once a new module has been added to your project, it is treated as a file. 
  • As such, you should save it to give it a friendlier name. 
  • The name of a module can be used to indicate the type of code included in that module.
Once a module is ready,

  • you can type any piece of valid code in it. 
  • Such a module is automatically made available to other parts of your program. 
  • Just as you can write code in a module attached to a form, you can also write code in an independent module (unlike some other languages like C/C++ or Pascal, you usually don't need to reference one module from another; once a module has been created, you can use its contents as you wich).
Any existing module of your project is represented in the Project window with a button and a name in the Modules node. Therefore, to open a module, in Project window, you can double-click its node.
 Practical Learning: Creating a Module
  1. To access the form's module, double-click anywhere in the form and notice that a source file opens
  2. To create a new module, on the Standard toolbar, click the arrow of the Add Form button and click Module

    Adding a New Module
  3. In the Add Module dialog box, make sure the Module icon is selected and click Open

    The Add Module dialog box
  4. To save the new module, on the Standard toolbar, click the Save button Save
  5. Change the name of the file to modGeometry and press Enter
  6. To change the name of the module in Visual Basic, in the Project window, click Module1. In the Properties window, click (Name). Type Routines and press Enter
We use procedures and functions to create modular programs. Visual Basic statements are grouped in a block enclosed by SubFunction and matching End statements. The difference between the two is that functions return values, procedures do not.
A procedure and function is a piece of code in a larger program. They perform a specific task. The advantages of using procedures and functions are:
  • Reducing duplication of code
  • Decomposing complex problems into simpler pieces
  • Improving clarity of the code
  • Reuse of code
  • Information hiding

Procedures

A procedure is a block of Visual Basic statements inside SubEnd Sub statements. Procedures do not return values.
Option Strict On


Module Example

    Sub Main()
       
        SimpleProcedure()
        
    End Sub
    
    Sub SimpleProcedure()
        Console.WriteLine("Simple procedure")
    End Sub
    
End Module
This example shows basic usage of procedures. In our program, we have two procedures. The Main()procedure and the user defined SimpleProcedure(). As we already know, the Main() procedure is the entry point of a Visual Basic program.
SimpleProcedure()
Each procedure has a name. Inside the Main() procedure, we call our user defined SimpleProcedure()procedure.
Sub SimpleProcedure()
    Console.WriteLine("Simple procedure")
End Sub
Procedures are defined outside the Main() procedure. Procedure name follows the Sub statement. When we call a procedure inside the Visual Basic program, the control is given to that procedure. Statements inside the block of the procedure are executed.
Procedures can take optional parameters.
Option Strict On


Module Example

    Sub Main()

        Dim x As Integer = 55
        Dim y As Integer = 32
       
        Addition(x, y)
        
    End Sub
    
    Sub Addition(ByVal k As Integer, ByVal l As Integer)
        Console.WriteLine(k+l)
    End Sub
    
End Module
In the above example, we pass some values to the Addition() procedure.
Addition(x, y)
Here we call the Addition() procedure and pass two parameters to it. These parameters are two Integer values.
Sub Addition(ByVal k As Integer, ByVal l As Integer)
    Console.WriteLine(k+l)
End Sub
We define a procedure signature. A procedure signature is a way of describing the parameters and parameter types with which a legal call to the function can be made. It contains the name of the procedure, its parameters and their type, and in case of functions also the return value. The ByValkeyword specifies how we pass the values to the procedure. In our case, the procedure obtains two numerical values, 55 and 32. These numbers are added and the result is printed to the console.

Functions

A function is a block of Visual Basic statements inside FunctionEnd Function statements. Functionsreturn values.
There are two basic types of functions. Built-in functions and user defined ones. The built-infunctions are part of the Visual Basic language. There are various mathematical, string or conversion functions.
Option Strict On


Module Example

    Sub Main()
       
        Console.WriteLine(Math.Abs(-23))
        Console.WriteLine(Math.Round(34.56))
        Console.WriteLine("ZetCode has {0} characters", _
            Len("ZetCode"))
        
    End Sub
    
End Module
In the preceding example, we use two math functions and one string function. Built-in functionshelp programmers do some common tasks.
In the following example, we have a user defined function.
Option Strict On


Module Example

    Dim x As Integer = 55
    Dim y As Integer = 32
    
    Dim result As Integer

    Sub Main()

        result = Addition(x, y)
        Console.WriteLine(Addition(x, y))
        
    End Sub
    
    Function Addition(ByVal k As Integer, _
                       ByVal l As Integer) As Integer
        Return k+l
    End Function
    
End Module
Two values are passed to the function. We add these two values and return the result to the Main()function.
result = Addition(x, y)
Addition function is called. The function returns a result and this result is assigned to the result variable.
Function Addition(ByVal k As Integer, _
                    ByVal l As Integer) As Integer
    Return k+l
End Function
This is the Addition function signature and its body. It also includes a return data type, for the returned value. In our case is is an Integer. Values are returned to the caller with the Returnkeyword.