A Function is a procedure which returns a single value. Visual Basic has many built-in functions
lblAmount.Text= FormatCurrency(sngAmount,0)
intAgeAmount = CInt(txtAge.Text)
If IsNumeric(txtAmount.Text)
We (programmers) can write our own programmer defined functions.
In Visual Basic a function looks like
Public Function
FunctionName([arguments]) [As
Datatype]
:
:
Return [Some value]
End Function
You may pass values to Function procedures the same way as you pass values to Sub procedures. The As DataType clause indicates the data type of the value that will be returned from the function. The As DataType clause is required when Option Strict is On.
The Return statement specifies the value (variable or constant) that will be returned to the calling procedure.
For example
Public Function
Square(ByVal intNum
As Integer)
As Long
‘return data type
Return intNum ^ 2
End Function
The statement that calls the preceding function might look like
lngAnswer = Square(intNum)
A common use of functions in programs it to return error codes.
:
If ValidValue(txtInput.Text)
Then
:
:
Public Function ValidValue(ByVal
strInput As String)
As Boolean
Dim
decInput
As Decimal
If IsNumeric(strInput)
Then
decInput =
Convert.ToDecimal(strInput)
If decInput < 0
Or decInput > 1000
Then
Return False
Else
Return True
End If
Else
Return False
End If
End Function