VB.NET refers to the two types of
method as Sub
and Function
.
In the Java language they take up the approach of a void
and a
method with a type such as String
. This article
will look at both of these types of method. The part that gives
the method a name and parameters is called the method
signature. Below is a simple diagram that explains the
method structure.
Mutator methods vs accessor methods
A mutator method is used to change the way a class 'looks'. It can be used to change a variable within a class that is otherwise not exposed. This can be used for the sake of making the class look tidier or for the sake of security. For instance, if a variable that is stored contains a password, it may be a good idea to make it privately declared and to have a method that can override it, only when the user inserts the current password.
An accessor method is designed to access a private variable and give it back. For instance, there may be a variable used to store a piece of text, and a method that can access this variable. When it is accessed this way, the value is not changed.
Function
A mathematical function can be seen as taking an input, applying something to it and then outputting some other data. It can be seen as a map:
A ⇒ B
An accessor method is a method which is used to access a
piece of data. This may return something such as a variable that
is stored privately or it may return some data based on a
function. An accessor method is always declared using the Function
keyword.
Function SampleFunction() As String 'Code goes here End Function
A function also requires a return call, in VB.NET it is simply represented by Return
.
A return call is used to make the function give back data to the
original call, which in the case of the basic map of a function
example would be A. Here is an example function that returns the
number 10 doubled.
Function DoubleUp() As Integer Return 10 * 2 End Function
Sub
The VB.NET Sub
is short for subroutine, a part of a program that
performs a certain task. A subroutine in VB.NET will not return
anything. Whilst most mutator methods are subroutines, they need
not have to be, a function may also be used and return true or
false based on whether it accomplished the task or not. Sub
is
the equivalent of the Java void
keyword.
Subroutines are generally used to perform a task and not return anything to the user, but it technically can. This will be demonstrated later in this article. An example subroutine is defined below:
Sub SampleMethod() 'Code goes here End Sub
A subroutine may not directly return anything but user dialogs etc. can still be used for interaction with the user. The following subroutine is used to double a number and inform the user of it's value.
Sub DoubleUp() MsgBox(10 * 2) End Sub
The MsgBox
is simply used to display the result to the
user and will be covered in a later article.
Parameters
A parameter is a piece of information (a variable in programming terms) that is given (passed) to a method. Almost all programming languages that exist have some form of parameter passing.
A parameter can be any value, variable or some function that returns a value. A method is given a signature which allows it to specify its parameters along side its name. In the previous samples there are brackets after the method name which is where the parameters are to go. A method does not require parameters to function however. Below is an example of a method with parameters:
Function DoubleUp(x As Integer) Return (x * 2) End Function
In the above example, the parameter x is given as an integer. The method is a function which return x doubled.
ByVal and ByRef
ByVal
is short for 'by value' and ByRef
is short for 'by reference'. The latter is one way how a subroutine can return a value.
Java is a language that only supports the former and can pass
only by value. A parameter in VB.NET can be passed into a method
either by value or by reference.
A parameter passed in by value will mean it is simply the
value that is given, so if a variable is passed in and the
method directly accesses it (such as doubles it), the original
variable will not change. If it is passed by reference, the
original variable reference (its location in the stack or
memory) is changed. Below are two examples of functions and what it
does to each for ByRef
and ByVal
:
Function DoubleUp(ByVal x As Integer) x = x * 2 Return x End Function
- A = 5
- Answer = DoubleUp(A)
- A = 5
- Answer = 10
Function DoubleUp(ByRef x As Integer) x = x * 2 Return x End Function
- A = 5
- Answer = DoubleUp(ByRef A)
- A = 10
- Answer = 10
The following example of a module in a Console Application demonstrates a subroutine which acts as an accessor method:
Module MainModule Sub Main() Dim value As Integer = 5 GetValue(value) MsgBox(value) End Sub Sub GetValue(ByRef x As Integer) x = x * 2 End Sub End Module
A message will appear with 10 in it. And that's all there is to changing variables with parameters and subroutines.
Calling a method
A method can be called through a very simple syntax. The following is a console application that calls a method:
Module MainModule Sub Main() Dim value As Integer = 5 Console.Write(GetValue(value)) MsgBox(value) End Sub Sub GetValue(ByVal x As Integer) Return x * 2 End Sub End Module
Recursion
Recursive methods are methods that call themselves. This is called recursion.
A method can call itself indefinitely or it can call itself until a condition has been met.
Whilst the following sample does use if statements which are discussed later in the tutorial, it does give a good example as to how to use recursion:
Module MainModule Sub Main() Countdown(10) End Sub Sub Countdown(ByVal n As Integer) Console.WriteLine(n) If n > 0 Then Countdown(n - 1) End If End Sub End Module
- Create a program that displays the first ten numbers of the Fibonacci sequence (1, 2, 3, 5, 8, 13, 21, 34, 55, 89). This program should use a recursive function to generate this. The program will also need to use an if statement to do this.