Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Visual Basic [VB] (http://www.go4expert.com/articles/visual-basic-tutorials/)
-   -   Functions in Visual Basic (http://www.go4expert.com/articles/functions-visual-basic-t3688/)

Sanskruti 30Mar2007 19:30

Functions in Visual Basic
 
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: VB

[ <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: VB

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: VB

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: VB

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: VB

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 .

Steel9561 1May2008 03:03

Re: Functions in Visual Basic
 
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


All times are GMT +5.5. The time now is 16:16.