VB.NET Sub-procedure and Function

Subroutine

A subroutine is a code construct.
A code construct is a pattern of coding. Examples;- For Loops, Do Whiles Loops, Sub ... End Sub, Function .... End Function
A subroutine is a self contained section of code, that;-
Has Inputs: Possible
Has Output: Never

Module mainModule
    Sub Main()
        MsgBox("The Main procedure is starting the application.")
        ' Insert call to appropriate starting place in your code.
        MsgBox("The application is terminating.")
    End Sub
End Module


Functions
A function is a self contained section of code, that;-
Has Inputs: Generally Yes
Has Output: Always


1
Function SquareOfNumber(ByVal x As Decimal) As Decimal
2
 Return x * x

3
End Function

ByVal This a copy of the object passed in. You can do what you like to this and it won't change the original object passed in as the argument.
ByRef This a REFerence to the actual object. What you do to this affects the original object passed in as the argument.

ByRef argument allows the programmer to get information out of a Subroutine, like a Function. 

Example 1 (Trivial): Increase the value of two Doubles by 2.
1
Public Sub IncreaseBothBy2(ByRef A As Double,ByRef B As Double)
2
A+=2

3
B+=2
4
End Sub

Example 2: Clear the text of any textbox.

1
Sub ClearTextbox(ByRef Tb As Textbox)
2
Tb.Text=""

3
End Sub





No comments:

Post a Comment