VB Functions and Subroutines - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

VB Functions and Subroutines

Description:

VB Functions and Subroutines. VB makes a distinction between functions and subroutines ... Class level variables can be accessed directly and need not be passed as ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 14
Provided by: davidb195
Category:

less

Transcript and Presenter's Notes

Title: VB Functions and Subroutines


1
VB Functions and Subroutines
  • VB makes a distinction between functions and
    subroutines
  • Functions return a single value, subroutines do
    not return any value
  • Functions may be used within an expression,
    subroutines cannot
  • Either may have zero to many parameters

2
Subroutines
  • FormatSub name( ByVal ByRef var1 as Type1
    , ) statement(s)End Sub
  • ByVal passes a copy of a value to the subroutine
    - the value is unchanged when the subroutine
    finishes

3
Subroutines (Cont'd)
  • ByRef passes a reference to a value to the
    subroutine - the value can be changed by the
    subroutine
  • Class level variables can be accessed directly
    and need not be passed as parameters - ex event
    procedures can directly access all elements on a
    form

4
Example Subroutine
  • Takes an error message as a value parameter and
    prints itSub printError( ByVal err As String )
    MsgBox( "Error " err )End Sub

5
Example Subroutine
  • Takes an error message as a value parameter and a
    string as a reference parameter and changes the
    latterSub setError( ByVal err As String, ByRef
    str As String ) str "Error " errEnd Sub

6
Example Subroutine
  • Sets a label to a calculated valuePrivate Sub
    btnArea_Click( ) Handles btnArea.Click
    lblArea.Text CStr( pi txtRadius2 )End
    Sub
  • The label and text box are class-level variables

7
Example Subroutine
  • Sets an area value by referenceSub setArea(
    ByVal dblRadius As Double, ByRef dblArea As
    Double ) dblArea pi dblRadius2End Sub

8
Example Subroutine Calls
  • Calls printErrorprintError( "Value lt 0" )
  • Calls setErrorsetError( strError )(argument
    must be a variable)
  • Calls setAreasetArea( dblRadius, dblArea )

9
Functions
  • FormatFunction name ( ByVal var1 as Type1 ,
    ) As DataType statement(s) Return
    expressionEnd Function
  • expression must be of DataType
  • parameters are local to the function

10
Functions (Cont'd)
  • Functions can have parameters passed by
    reference, but this is bad programming practice
    as function should return a single value rather
    than change values

11
Example Function
  • Takes an error message as a value parameter and
    returns a stringFunction returnError( ByVal err
    As String ) As String Return "Error "
    errEnd Function

12
Example Function
  • Returns an area given a radiusFunction
    circleArea( ByVal dblR As Double ) As Double
    Return pi dblR2End Function

13
Example Function Calls
  • Calls returnErrorstrNewError returnError(
    "Value lt 0" )
  • Calls circleAreadblArea circleArea( dblRadius
    )
Write a Comment
User Comments (0)
About PowerShow.com