Showing posts with label Return Values. Show all posts
Showing posts with label Return Values. Show all posts

Wednesday, October 27, 2010

Constructor

The constructor of a class is implemented as a subroutine. The constructor may or may not require parameters. A subroutine cannot return a value.


Code eg 1: Constructor with no paramters

Public Sub New()
' Code does something...
End Sub

Code eg 2: Constructor with paramters
Public Sub New(NumberOfPoints As Integer, UserName As String)
' Code does something with parameters...
End Sub

Programming Behavior

Each behavior of a class is implemented as a procedure. A procedure may be a subroutine or a function.

Procedures may or not require parameters to execute.


Subroutine
When the behavior simply needs to execute some code, we use a subroutine.

Code eg 1: Subroutine with no parameters
Public Sub NameOfBehavior()
    'Code does something
End Sub
Code eg 2: Subroutine with parameters

Public Sub Login(UserName: String, Password: String)
    'Code does something with parameters...
End Sub


Function
When the behavior is required to return a value to calling procedure, we use a function.

Code eg 1: Function with no parameters
Public Function NameOfFunction() As String
    'Code does something
    Return "Sample return String"
End Sub
Code eg 2: Function with parameters

Public Function getLastName(FirstName: String) As String
    'Code does something with parameters...
    Return LastName
End Sub

Parameters in Subroutines & Functions

Sometimes a behavior will require further information to accomplish a task. When an input is required for a behavior to execute, this input is passed to the procedure as one or a number of parameters (sometimes called arguments).

Code eg: Subprocedure using parameters.

Public Sub Login(Username As String, Password As String)
    ' Code does something
End Function

Code eg: Function using parameters.

Public Function Calculate(NumberOne As Integer, NumberTwo As Integer) As Integer
    ' Code does something
   Return AValue
End Function