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.
Here's the formal syntax for functions you use the Function statement:
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:
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:
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.
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:
If you use Exit Function without setting a return value, the function returns the default value .
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
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
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
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
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
shabbir
like this
