|
|
 |
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| In next example variable sum is used to retrieve output from the routine MakeSum. Argument sum works with a reference ( ByRef ) to variable sum from the Main routine. Assigning a value to argument sum in MakeSum will now change the value of sum in Main. |
| Module Example1
Sub Main()
Dim value1 As Integer = 5
Dim value2 As Integer = 10
Dim sum As Integer
MakeSum(value1, value2, sum)
Console.WriteLine(sum)
Console.ReadLine()
End Sub
Sub MakeSum(ByVal value1 As Integer, ByVal value2 As Integer, _
ByRef sum As Integer)
sum = value1 + value2
End Sub
End Module Download Broncode |
| A more elegant way to produce output from a routine, can be done by using a function. |
| Module Example2
Sub Main()
Dim value1 As Integer = 5
Dim value2 As Integer = 10
Dim sum As Integer = GetSum(value1, value2)
Console.WriteLine(sum)
Console.ReadLine()
End Sub
Function GetSum(ByVal value1 As Integer, _
ByVal value2 As Integer) As Integer
Return (value1 + value2)
End Function
End Module Download Broncode |
Definition and Call of a Function
| A call to a function is an expression that returns a value.
A function can be defined with Function ... End Function : |
| Function <identifier>(<arguments>) [<type-specifier>]
...
End Function
|
| The type of the expression the call to the function forms is an expression of that type defined in the type specifier of the definition of the function.
A call to GetSum(...) is an Integer expression.
The function defines which value is returned. This can be done by a Return statement. Return in GetSum is followed by an expression which represents a value that fits the Integer format.
From now on the term "method" is used for procedures ( Sub ) and functions ( Function ).
A procedure can be seen as a function that doesn't return anything. Or a functionmethod that returns a "void".
Procedures are used to perform a simple action, use functions when the routine needs to return a result.
Try choosing an identifier that explains as clearly as possible which value is returned by the function. Often used prefixes in identifiers of functions are "Is", "Has" and "Get". "Is" and "Has" functions usually return a Boolean value. |
Up
Setting the Return Value
| The return value of a function can also be set by assigning that value to the function name ( identifier ).
When a function is called, room for a value of the returntype is reserved on the activation record for that function. The Return statument will fill that space and exit the function. A Return statement is an exit point for that function. Assigning the returnvalue to the name of the function will only fill the reserved returnholder, but will not exit the function. |
| Module Example3
Sub Main()
Console.WriteLine(Test1())
Console.WriteLine(Test2())
Console.ReadLine()
End Sub
Function Test1() As String
Console.WriteLine("Test1 : code before Return statement")
Return "Test1"
Console.WriteLine("Test1 : code after Return statement")
End Function
Function Test2() As String
Console.WriteLine("Test2 : code before assigning return value")
Test2 = "Test2"
Console.WriteLine("Test2 : code after assigning return value")
End Function
End Module Download Broncode |
| Output : Test1 : code before Return statement
Test2 : code before assigning return value
Test2 : code after assigning return value |
| As you can see all statements following the Return statement will not execute (1).
Next example defines three different GetInfo...-functions that will produces the same resultvalue. All functions will set the returnvalue in a different way. |
| Module Example4
Sub Main()
For value As Integer = -1 To 1
Console.WriteLine("Value " & value & " : ")
Console.WriteLine(GetInfo1(value))
Console.WriteLine(GetInfo2(value))
Console.WriteLine(GetInfo3(value))
Console.WriteLine()
Next
Console.ReadLine()
End Sub
Function GetInfo1(ByVal value As Integer) As String
Select Case value
Case Is = 0
Return "Value is zero."
Case Is > 0
Return "Value is positive."
Case Is < 0
Return "Value is negative."
End Select
End Function
Function GetInfo2(ByVal value As Integer) As String
Dim info2 As String
Select Case value
Case Is = 0
info2 = "Value is zero."
Case Is > 0
info2 = "Value is positive."
Case Is < 0
info2 = "Value is negative."
End Select
Return info2
End Function
Function GetInfo3(ByVal value As Integer) As String
Select Case value
Case Is = 0
GetInfo3 = "Value is zero."
Case Is > 0
GetInfo3 = "Value is positive."
Case Is < 0
GetInfo3 = "Value is negative."
End Select
End Function
End Module Download Broncode |
| Output : Value -1 :
Value is negative.
Value is negative.
Value is negative.
Value 0 :
Value is zero.
Value is zero.
Value is zero.
Value 1 :
Value is positive.
Value is positive.
Value is positive. |
| GetInfo1 defines three exitpoints ( three Return statements ) that will end the execution of that function. The use of more than one exitpoint can make a functiondefinition harder to read or maintain.
GetInfo2 uses an extra variable to hold the returnvalue.
As GetInfo3 demonstrates is the use of an extra variable to hold the returnvalue unnecessary ( and memory consuming ). These String functions will when in execution have a reserved place for a String value.
The Return statement in GetInfo3 (1) is unnecessary, and can be left out. |
| Module Example5
Sub Main()
Dim dividend As Integer = 24
Dim divisor As Integer = 6
Dim quotient As Single
If Not IsZero(divisor) Then quotient = GetQuotient(dividend, divisor)
Console.WriteLine(quotient)
Console.ReadLine()
End Sub
Function IsZero(ByVal value As Integer) As Boolean
If value = 0 Then IsZero = True
End Function
Function GetQuotient(ByVal dividend As Integer, _
ByVal divisor As Integer) As Double
GetQuotient = dividend / divisor
End Function
End Module Download Broncode |
| Function IsZero ( also called "query" ) answers affirmative or negative to the question whether or not value is equal to zero. The most suitable returntype is therefore Boolean.
Prefix "Is" is typical for a functions returning a Boolean value.
No returnvalue is explicitally set when value is not zero. The default value of type Boolean ( False ) will then be returned. |
Up
Exercises
| Task Exercise 1 :
Create a function GetResult that uses three argumentvalues.
The first and third argumentvalue need to be Integer values, the second argument needs to be a Char value.
The function should support three arithmetical operations. The second argument must be "+"c, "-"c or "*"c.
Integer can be used as returntype.
Solution Exercise 1 : |
| Module Exercise1Solution
Sub Main()
Console.WriteLine(GetResult(5, "+"c, 4))
Console.WriteLine(GetResult(5, "-"c, 4))
Console.WriteLine(GetResult(5, "*"c, 4))
Console.ReadLine()
End Sub
Function GetResult(ByVal value1 As Integer, _
ByVal operatorSymbol As Char, _
ByVal value2 As Integer) As Integer
Select Case operatorSymbol
Case "+"c
GetResult = value1 + value2
Case "-"c
GetResult = value1 - value2
Case "*"c
GetResult = value1 * value2
End Select
End Function
End Module Download Broncode |
| Create three functions.
Function IsLeapYear that answers affirmative or negative on the question whether or not a certain year is a leapyear or not. A leapyear can be divided by 4 and not by 100, or can be divided by 400. 1900 for instance was not a leapyear, 2000 was.
Function GetDaysFebruary that returns the number of days for February in a certain year.
Function GetDays that returns the number of days for a certain month of a certain year. |
| Module Exercise2Solution
Sub Main()
Console.WriteLine(IsLeapYear(1900))
Console.WriteLine(IsLeapYear(1996))
Console.WriteLine(IsLeapYear(2000))
Console.WriteLine()
Console.WriteLine(GetDaysFebruary(1900))
Console.WriteLine(GetDaysFebruary(1996))
Console.WriteLine(GetDaysFebruary(2000))
Console.WriteLine()
Console.WriteLine(GetDays(1900, 2))
Console.WriteLine(GetDays(1996, 2))
Console.WriteLine(GetDays(2000, 2))
Console.WriteLine()
Console.WriteLine(GetDays(1900, 3))
Console.WriteLine(GetDays(1996, 3))
Console.WriteLine(GetDays(2000, 3))
Console.ReadLine()
End Sub
Function IsLeapYear(ByVal year As Integer) As Boolean
If year Mod 4 = 0 AndAlso year Mod 100 <> 0 OrElse _
year Mod 400 = 0 Then IsLeapYear = True
End Function
Function GetDaysFebruary(ByVal year As Integer) As Integer
GetDaysFebruary = 28
If IsLeapYear(year) Then GetDaysFebruary = 29
End Function
Function GetDays(ByVal year As Integer, ByVal month As Integer) As Integer
If month = 2 Then
GetDays = GetDaysFebruary(year)
Else
Dim days As Integer() = _
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
GetDays = days(month - 1)
End If
End Function
End Module Download Broncode |
| Output : False
True
True
28
29
29
28
29
29
31
31
31 |
This version ( published on 2008-06-24 ) is printed from http://www.studyvb.com, visit the website for more recent information.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|