Home

Introduction to Functions

 

Introduction

Like a sub procedure, a function is used to perform an assignment. The main difference between a sub procedure and a function is that, after carrying its assignment, a function gives back a result. We also say that a function "returns a value". To distinguish both, there is a different syntax you use for a function.

Function Creation

To create a function, you use the Function keyword followed by a name and parentheses. Unlike a sub procedure, because a function returns a value, you should/must specify the type of value the function will produce. To give this information, on the right side of the closing parentheses, you can type the As keyword, followed by a data type. To indicate where a function stops, type End Function. Based on this, the minimum syntax used to create a function is:

Function FunctionName() As DataType
    
End Function

The name of a function follows the same rules and suggestions we reviewed for sub procedures. The DataType factor indicates the type of value that the function will return. If the function will produce a word or a group of words, you can create it as String. If the function will check something and determine whether it produce a true or a false value, you can create it as Boolean. The other data types are also valid in the contexts we reviewed them. Here is an example:


Module Module1

    Function GetFullName() As String
        
    End Function

End Module

As done with the variables, you can also use a type character as the return type of a function and omit the As DataType expression. The type character is typed on the right side of the function name and before the opening parenthesis. An example would be GetFullname$(). As with the variables, you must use the appropriate character for the function:
 

Character The function must return
$ a String type
% a Byte, Short, Int16, or In32
& an Int64 or a Long
! a Single type
# a Double
@ a Long integer

Here is an example:


Module Module1

    Function GetFullName$()
        
    End Function

End Module

As mentioned already, the section between the Function and the End Function lines is the body of the function. It is used to describe what the function does. As done on a sub procedure, one of the actions you can perform in a function is to declare a (local) variable and use it as you see fit. Here is an example:

Function CallMe() As String
    Dim Salute As String
    Salute = "You can call me Al"
End Function

After performing an assignment in a function, to indicate the value it returns, somewhere after the assignment and before the End Function line, you can type the name of the function, followed by the = sign, followed by the value the function returns. Here is an example in which a function returns a name:


Module Module1

    Function GetFullName$()
        Dim FirstName, LastName As String

        Console.Write("Enter First Name: ")
        FirstName = Console.ReadLine()
        Console.Write("Enter Last Name: ")
        LastName = Console.ReadLine()

        GetFullName = LastName & ", " & FirstName
    End Function

End Module

Alternatively, instead of using the name of the function to indicate the value it returns, you can type Return, followed by the value or the expression that the function returns. Based on this, the above function could also be created as follows:


Module Module1

    Function GetFullName$()
        Dim FirstName, LastName As String

        Console.Write("Enter First Name: ")
        FirstName = Console.ReadLine()
        Console.Write("Enter Last Name: ")
        LastName = Console.ReadLine()

        Return LastName & ", " & FirstName
    End Function

End Module

You can also use some local variables in the function to perform an assignment and then assign their result to the name of the function.

 
 

Practical Learning: Creating a Function

  1. To create a function, type the following in the file:
     
    
    Module Module1
    
        Sub SquarePerimeter()
            Dim dblSide As Double
            Dim dblPerimeter As Double
    
            Console.Write("Enter Side: ")
            dblSide = Console.ReadLine()
    
            dblPerimeter = dblSide * 4
    
            Console.WriteLine("Square Characteristics")
            Console.WriteLine("Side:      {0:F}", dblSide)
            Console.WriteLine("Perimeter: {0:F}", dblPerimeter)
        End Sub
    
        Function RectPerimeter() As Double
            Dim dblLength As Double
            Dim dblWidth As Double
    
            Console.Write("Enter Rectangle Length: ")
            dblLength = Console.ReadLine()
            Console.Write("Enter Rectangle Width:  ")
            dblWidth = Console.ReadLine()
    
            RectPerimeter = (dblLength + dblWidth) * 2
        End Function
    
        Sub Main()
            
        End Sub
    
    End Module
  2. Save all
 

Calling a Function

As done for the sub procedure, in order to use a function in your program, you must call it. Like a sub procedure, to call a function, you can simply type its name in the desired section of the program. Here is an example:

Sub Main()
    CallMe
End Sub

Since the primary purpose of a function is to return a value, to better take advantage of such a value, you can assign the name of a function to a property or a variable in the section where you are calling the function. Here is an example:


Module Module1

    Function GetFullName$()
        Dim FirstName, LastName As String

        Console.Write("Enter First Name: ")
        FirstName = Console.ReadLine()
        Console.Write("Enter Last Name: ")
        LastName = Console.ReadLine()

        GetFullName = LastName & ", " & FirstName
    End Function

    Sub Main()
        Dim FullName As String

        FullName = GetFullName()

        Console.WriteLine("Full Name: {0}", FullName)
        Console.WriteLine()
    End Sub

End Module

Here is an example of running this program:

Enter First Name: Herman
Enter Last Name: Argoni
Full Name: Argoni, Herman
 
 

Practical Learning: Calling a Function

  1. To call the RectPerimeter() function in Main(), declare a Double variable named Perimeter, assign it the name of the function, then use WriteLine to display the value of the variable:
     
    
    Module Module1
    
        Sub SquarePerimeter()
            Dim dblSide As Double
            Dim dblPerimeter As Double
    
            Console.Write("Enter Side: ")
            dblSide = Console.ReadLine()
    
            dblPerimeter = dblSide * 4
    
            Console.WriteLine("Square Characteristics")
            Console.WriteLine("Side:      {0:F}", dblSide)
            Console.WriteLine("Perimeter: {0:F}", dblPerimeter)
        End Sub
    
        Function RectPerimeter() As Double
            Dim dblLength As Double
            Dim dblWidth As Double
    
            Console.Write("Enter Rectangle Length: ")
            dblLength = Console.ReadLine()
            Console.Write("Enter Rectangle Width:  ")
            dblWidth = Console.ReadLine()
    
            RectPerimeter = (dblLength + dblWidth) * 2
        End Function
    
        Sub Main()
            Dim Perimeter As Double
    
            Perimeter = RectPerimeter()
    
            Console.WriteLine("Rectangle Perimeter: {0:F}", Perimeter)
        End Sub
    
    End Module
  2. Save the file
  3. Compile the application and execute it. Here is an example:
     
    C:\VBasic\Procedures1>vbc Exercise.vb
    Microsoft (R) Visual Basic .NET Compiler version 7.10.3052.4
    for Microsoft (R) .NET Framework version 1.1.4322.573
    Copyright (C) Microsoft Corporation 1987-2002. All rights reserved.
    
    
    C:\VBasic\Procedures1>Exercise
    Enter Side: 36.88
    Square Characteristics
    Side:      36.88
    Perimeter: 147.52
  4. Return to Notepad
 

The Main() Function

Main() is the entry point of a program as we have used it so far. Particularly, Main() can be used as a sub procedure or a function. So far, we have used Main() only as a sub procedure. To use Main() as a function, type the Function keyword required for each function, followed by Main(), followed by As Integer. When declared like this, the Main() function must return an integer. The most regularly return value is 0, which indicates that the function ended successfully.

Practical Learning: Calling a Function

  1. To use Main() as a function, change it as follows:
     
    
    Module Module1
    
        Sub SquarePerimeter()
            . . . No Change
        End Sub
    
        Function RectPerimeter() As Double
            . . . No Change
        End Function
    
        Function Main() As Integer
            Dim Perimeter As Double
    
            Perimeter = RectPerimeter()
    
            Console.WriteLine("Rectangle Perimeter: {0:F}", Perimeter)
    
    	Return 0
        End Function
    
    End Module
  2. Save the file
  3. Compile the application and execute it 
  4. Return to Notepad
 

Previous Copyright © 2004-2007 FunctionX, Inc. Next