Creating Functions in VB.net
By: Steven Holzner
Unlike Sub procedures,, functions can return values. You use the Function statement to create a function:
[ <attrlist> ] [{ Overloads | Overrides | Overridable | NotOverridable |
MustOverride | Shadows | Shared }]
[{ Public | Protected | Friend | Protected Friend | Private }] Function
name[(arglist)] [ As type ]
[ statements ]
[ Exit Function ]
[ statements ]
End Function
When you use ByVal (the default in VB .NET), you pass a copy of a variable to a procedure; when you use ByRef, you pass a reference to the variable, and if you make changes to that reference, the original variable is changed
The various parts of this statement are the same as for Sub procedures (see the previous topic) except for the As type clause, which specifies the type of the return value from the function; here's how to set the type item:
- type-This is optional unless Option Strict is On. Data type of the value returned by the Function procedure 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.
If you use Exit Function without assigning a value to name, the function returns the default value appropriate to argtype. This is 0 for Byte, Char, Decimal, Double, Integer, Long, Short, and Single; Nothing for Object, String, and all arrays; False for Boolean; and #1/1/0001 12:00 AM# for Date.
The Return statement simultaneously assigns the return value and exits the function; any number of Return statements can appear anywhere in the procedure. (You also can mix Exit Function and Return statements.) Here's an example function-Addem-, which adds two integer values passed to it:
Module Module1
Sub Main()
Dim intValue As Integer = 2
System.Console.WriteLine("{0}+{1}={2}", _
intValue, intValue, Addem(intValue, intValue))
End Sub
Function Addem(ByVal int1 As Integer, ByVal int2 As Integer) As Long
Return int1 + int2
End Function
End Module
Archived Comments
1. Billyweimb
View Tutorial By: Billyweimb at 2017-07-17 14:22:07
2. Billyweimb
View Tutorial By: Billyweimb at 2017-05-17 17:57:10
3. very good site for qustion answer. I just spellbound...... I am totaly impressed........ i like th
View Tutorial By: Anindya Mitra at 2012-04-22 16:28:42
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net
Exception Filtering in the Catch Block in VB.net
Using Multiple Catch Statements in VB.net
Throwing an Exception in VB.net
Throwing a Custom Exception in VB.net
Changes in Controls from VB6 to VB.net
Unstructured Exception Handling in VB.net