Showing posts with label Function. Show all posts
Showing posts with label Function. Show all posts

Tuesday, April 26, 2011

FormatCurrency Function

To format a decimal value as currency for output:

lblLabel.Text = FormatCurrency(decPrice)

Also see: Format currency in GridView

Wednesday, October 27, 2010

Mutators: Set

Classes may contain mutator behaviors for some or all of their states. This enables other objects to change the value of a state. A mutator behavior is usually implemented as a subroutine, unless there is a requirement for a return message - such as a confirmation that the change took place, in which case you would use a function (which could return a value, such as True or False).

A setter subroutine requires a parameter which represents the new value of the state.

For example, the Player class now contains a number of mutator behaviors. Following convention, the names of these behaviors begin with the word Set.

In a class diagram, the format for a setter behavior(subroutine) looks like this:

Visibility SubroutineName(Parameters: DataType)

...as can be seen in the Player class example:

+SetName(NewName: String)

The implementation (code) for a mutator, reads the parameter that is passed to it and updates the value of the state with the value of the parameter. In other words, the SetName() function will read the String parameter passed to it and then change the value of the Name state.

An mutator behavior has visibility of Public.

Accessors: Get

Classes may contain accessor behaviors for some or all of their states. This enables other objects to read the value of a state. An accessor behavior is nearly always implemented as a function, as a function returns a value to the calling procedure.

For example, the Player class now contains a number of accessor behaviors. Following convention, the names of these behaviors begin with the word Get.

In a class diagram, the format for a behavior that returns a value(function) looks like this:
Visibility FunctionName(ParametersIfAny):ReturnDataType

...as can be seen in the Player class example:
+GetName():String

The implementation (code) for an accessor, reads and returns the value of the state being accessed. In other words, the GetName() function will return a String (containing the value of the Name state) to the calling procedure.

An accessor behavior has visibility of Public.

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

Functions

A function returns a value to the procedure that called it.

Code eg 1: Function.

Public Function CalculateScore() as Integer
        ' Declare variables
        Dim TotalDamage As Integer = 555
    Dim TotalAgility As Integer = 6
        ' Create the expression
        Dim TotalPoints As Integer = TotalDamage * TotalAgility
    Return
TotalPoints
End Function

A function returns a value to the calling procedure. In the above case, the function will return the value of the variable called TotalPoints. The As Integer part of the function header, indicate that the function will return a value that is of the Integer type.

Code eg 2: Function.

Public Function getFirstName() as String
    Return strFirstName
End Function

This new example shows a function that will return a String value.

Subroutines

A subroutine simply executes code when called by another procedure.

Code eg: Subroutine.

Public Sub CalculateScore()
        ' Declare variables
        Dim TotalDamage As Integer = 555
        Dim TotalAgility As Integer = 6
        ' Create the expression
        Dim TotalPoints As Integer = TotalDamage * TotalAgility
End Sub

A subroutine does not return a value to the calling procedure.

If you need to return a value - use a function.