1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Functions in Visual Basic

Discussion in 'Visual Basic [VB]' started by Sanskruti, Mar 30, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    You can also create functions in Visual Basic . They are just like Sub procedures except that they can return a value. You declare a function in much the same way as a Sub procedure, except that you use the Function keyword instead of Sub.

    Function Syntax



    Here's the formal syntax for functions you use the Function statement:
    Code:
    [ <attrlist> ] [{ Overloads | Overrides | Overridable | 
    NotOverridable | MustOverride | Shadows | Shared }]
    [{ Public | Protected | Friend | Protected Friend | 
    Private }] Function name[(arglist)] [ As type ]
    [ Implements interface.definedname ]
      [ statements ]
      [ Exit Function ]
      [ statements ]
    End Function
    Let's look at an example. In this case, we'll create a function named Sum that calculates the sum of two integers and returns that sum; this project is named Functions in the code for the book.

    To create this new function, you use the Function keyword:

    Code:
    Module Module1
    
      Sub Main()
    
      End Sub
    
      Function Sum(ByVal a As Integer, ByVal b As Integer) As Long
    
      End Function
    
    End Module
    You return a value from a function with the Return statement, as here, where the code is returning the sum of the two arguments passed to the function:

    Code:
    Module Module1
    
      Sub Main()
    
      End Sub
    
      Function Sum(ByVal a As Integer, ByVal b As Integer) As Long
        Return a + b
      End Function
    
    End Module
    Now when you call Sum with two integers, like Sum(2, 3), Visual Basic will treat that function call as an expression and replace it with the value returned by the function, which is 5 here.

    Returning Data from a Function



    Code:
    Module Module1
    
      Sub Main()
        Dim intX As Integer = 2
        Dim intY As Integer = 3
        Console.WriteLine(intX & " + " & _
          intY & " = " & Sum(intX, intY))
        Console.WriteLine("Press Enter to continue...")
        Console.ReadLine()
      End Sub
    
      Function Sum(ByVal a As Integer, ByVal b As Integer) As Long
        Return a + b
      End Function
    
    End Module
    When you run this code, you see this result:

    2 + 3 = 5
    Press Enter to continue...

    The various parts of this statement are the same as for Sub procedures except for the As type clause, which specifies the type of the return value from the function. This clause indicates the data type of the value returned by the function. That type can be Boolean, Byte, Char, Date, Decimal, Double, Integer, Long, Object, Short, Single, or String, or the name of an enumeration, structure, class, or interface.

    The Return statement, if there is one, sets the return value and exits the function; any number of Return statements can appear anywhere in the function, but as soon as one of them is executed, you return from the function to the calling code. You can also use the Exit Function statement to exit the function at any time. If you use Exit Function, how can you return a value from a function? You just assign that value to the function name itself, like this:
    Code:
    Function Sum(ByVal a As Integer, ByVal b As Integer) As Long
      Sum = a + b
      Exit Function
        .
        .
        .
    End Function
    If you use Exit Function without setting a return value, the function returns the default value .
     
    shabbir likes this.
  2. Steel9561

    Steel9561 New Member

    Joined:
    Apr 26, 2008
    Messages:
    14
    Likes Received:
    1
    Trophy Points:
    0
    I think that the fact that functions always return a value is one of the most important factors of functions. Functions really help you do complex calculations and just return the final outcome to the calling procedure or function :)

    Luis Lazo
     
    Last edited by a moderator: May 1, 2008
    Prince_Royce likes this.

Share This Page