Subroutines - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

Subroutines

Description:

Usually on the right hand side of an assignment statement. Functions ... Assign a value to Function Name. Using a Return statement with the value to return ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 41
Provided by: debrac7
Category:

less

Transcript and Presenter's Notes

Title: Subroutines


1
Subroutines Functions
  • ITE 285

2
Why Use Procedures
  • Re-usable code
  • Handles the LOGIC of our program
  • Logical Modulate Design of Solution
  • Easy for programmer to understand later
  • Easier to test debug
  • Each procedure should accomplish one
    task/sub-task
  • Break down each task into sub-tasks

3
Types of Procedures
  • _______________
  • Executes automatically when an event is fired
  • Written in form file in specific controls section
    of code window
  • _______________
  • _______________

4
Types of Procedures
  • Event Procedure
  • Executes automatically when an event is fired
  • Written in form file in specific controls section
    of code window
  • _______________
  • _______________

5
Types of Procedures
  • Event Procedure
  • Executes automatically when an event is fired
  • Written in form file in specific controls section
    of code window
  • User-Defined Procedures
  • _______________

6
Types of Procedures
  • Event Procedure
  • Executes automatically when an event is fired
  • Written in form file in specific controls section
    of code window
  • User-Defined Procedures
  • External Procedures

7
User-Defined Procedures
  • Must be called by other code
  • Keyword __________
  • Declared in ___________________ section of form
    file or in a Code Module
  • 2 Types
  • __________________
  • Does not return a value
  • Can be an event procedure
  • __________________
  • Does return a value
  • Cannot be an event procedure

8
User-Defined Procedures
  • Must be called by other code
  • Keyword CALL
  • Declared in ___________________ section of form
    file or in a Code Module
  • 2 Types
  • __________________
  • Does not return a value
  • Can be an event procedure
  • __________________
  • Does return a value
  • Cannot be an event procedure

9
User-Defined Procedures
  • Must be called by other code
  • Keyword CALL
  • Declared in General Declarations section of form
    file or in a Code Module
  • 2 Types
  • __________________
  • Does not return a value
  • Can be an event procedure
  • __________________
  • Does return a value
  • Cannot be an event procedure

10
User-Defined Procedures
  • Must be called by other code
  • Keyword CALL
  • Declared in General Declarations section of form
    file or in a Code Module
  • 2 Types
  • Sub-Routine
  • Does not return a value
  • Can be an event procedure
  • __________________
  • Does return a value
  • Cannot be an event procedure

11
User-Defined Procedures
  • Must be called by other code
  • Keyword CALL
  • Declared in General Declarations section of form
    file or in a Code Module
  • 2 Types
  • Sub-Routine
  • Does not return a value
  • Can be an event procedure
  • Function
  • Does return a value
  • Cannot be an event procedure

12
External Procedures
  • Declared in separately compiled modules (code
    libraries)
  • Can be called by your code
  • Keyword CALL
  • CANNOT be used in this class

13
User Defined Sub-Routines
  • Declared by creating a method ___________ and
    end.
  • AccessControlKeyword Sub Name (ParameterName as
    DataType)
  • Subroutine Code
  • End Sub

14
User Defined Sub-Routines
  • Declared by creating a method Header and end.
  • AccessControlKeyword Sub Name (ParameterName as
    DataType)
  • Subroutine Code
  • End Sub

15
User Defined Sub-Routines
  • Example
  • Public Sub myFirstSub (ByVal intMyNum as Integer)
  • txtGrade.text (intMyNumber 20)
  • EndSub

16
User Defined Sub-Routines
  • Must be called from another line of code
  • _______________ of sub-routine is generally
    accepted as the sub-routine call
  • Exception External Sub-routines (must use CALL)
  • Sample Code

17
User Defined Sub-Routines
  • Must be called from another line of code
  • Name of sub-routine is generally accepted as the
    sub-routine call
  • Exception External Sub-routines (must use CALL)
  • Sample Code

18
Code Module
  • To add a Code Module
  • Project Menu
  • Add Module
  • Module Option
  • Give it a meaningful name
  • Remember Variables and/or Procedures declared
    as DIM are form level will not be accessible
    within a code module (and vise-versa) would
    need to be Public

19
Functions
  • Will return a value to the calling code
  • Must have an associated ________________
  • Declared at the end of the function header
  • Return a value
  • Assign a value to _______________
  • Using a _______________ statement with the value
    to return
  • A function call is only __________ of a code
    statement
  • Must be part of an expression (to handle
    returning value)
  • Usually on the right hand side of an assignment
    statement

20
Functions
  • Will return a value to the calling code
  • Must have an associated Data Type
  • Declared at the end of the function header
  • Return a value
  • Assign a value to _______________
  • Using a _______________ statement with the value
    to return
  • A function call is only __________ of a code
    statement
  • Must be part of an expression (to handle
    returning value)
  • Usually on the right hand side of an assignment
    statement

21
Functions
  • Will return a value to the calling code
  • Must have an associated Data Type
  • Declared at the end of the function header
  • Return a value
  • Assign a value to Function Name
  • Using a _______________ statement with the value
    to return
  • A function call is only __________ of a code
    statement
  • Must be part of an expression (to handle
    returning value)
  • Usually on the right hand side of an assignment
    statement

22
Functions
  • Will return a value to the calling code
  • Must have an associated Data Type
  • Declared at the end of the function header
  • Return a value
  • Assign a value to Function Name
  • Using a Return statement with the value to return
  • A function call is only __________ of a code
    statement
  • Must be part of an expression (to handle
    returning value)
  • Usually on the right hand side of an assignment
    statement

23
Functions
  • Will return a value to the calling code
  • Must have an associated Data Type
  • Declared at the end of the function header
  • Return a value
  • Assign a value to Function Name
  • Using a Return statement with the value to return
  • A function call is only part of a code statement
  • Must be part of an expression (to handle
    returning value)
  • Usually on the right hand side of an assignment
    statement

24
Functions
  • Syntax
  • AccessControlKeyword Function Name (ParameterName
    as DataType) as ReturnType
  • Function Code
  • Set Value to Return
  • End Function

25
Functions
  • Example
  • Private Function getName ( ) as String
  • Dim strInputName as String
  • strInputName InputBox (Enter First Name )
  • getName strInputName or Return
    strInputName
  • End Function
  • Sample Code

26
Parameter Passing
  • Parameters can be sent to subroutines and/or
    functions
  • ________________ list is defined in parenthesis
    within the procedure heading
  • ________________ list is designated in the
    procedure call
  • These must match in
  • _______________
  • _______________
  • _______________

27
Parameter Passing
  • Parameters can be sent to subroutines and/or
    functions
  • Formal Parameter list is defined in parenthesis
    within the procedure heading
  • ________________ list is designated in the
    procedure call
  • These must match in
  • _______________
  • _______________
  • _______________

28
Parameter Passing
  • Parameters can be sent to subroutines and/or
    functions
  • Formal Parameter list is defined in parenthesis
    within the procedure heading
  • Actual Parameter list is designated in the
    procedure call
  • These must match in
  • _______________
  • _______________
  • _______________

29
Parameter Passing
  • Parameters can be sent to subroutines and/or
    functions
  • Formal Parameter list is defined in parenthesis
    within the procedure heading
  • Actual Parameter list is designated in the
    procedure call
  • These must match in
  • Number
  • Data Type
  • Order

30
Parameter Passing
  • Defining a Parameter (in the procedure heading)
  • Pass by Reference or Pass by Value (more shortly)
  • Default is by Value
  • Identifier Name
  • Associated data type
  • Public Sub myFirstSub (ByVal intMyNum as Integer)
  • txtGrade.text (intMyNumber 20)
  • EndSub

31
Parameter Passing
  • When _______________ this procedure
  • You must send data (values or variables) to match
    formal parameter list
  • myFirstSub (25)
  • Actual parameter list and formal parameter list
    must match exactly
  • Visual Basic does allow for Procedure
    _______________
  • Multiple versions of a procedure with different
    parameter lists

32
Parameter Passing
  • When calling this procedure
  • You must send data (values or variables) to match
    formal parameter list
  • myFirstSub (25)
  • Actual parameter list and formal parameter list
    must match exactly
  • Visual Basic does allow for Procedure
    _______________
  • Multiple versions of a procedure with different
    parameter lists

33
Parameter Passing
  • When calling this procedure
  • You must send data (values or variables) to match
    formal parameter list
  • myFirstSub (25)
  • Actual parameter list and formal parameter list
    must match exactly
  • Visual Basic does allow for Procedure Overloading
  • Multiple versions of a procedure with different
    parameter lists

34
Pass by Value
  • Declared with ByVal keyword
  • Procedure receives a __________ of the value that
    was sent to the procedure
  • A local _______________ will be declared and
    initialized with that value
  • Changes made to the value will effect only the
    __________ copy of the variable
  • Changes WILL NOT carry back to the calling code

35
Pass by Value
  • Declared with ByVal keyword
  • Procedure receives a copy of the value that was
    sent to the procedure
  • A local _______________ will be declared and
    initialized with that value
  • Changes made to the value will effect only the
    __________ copy of the variable
  • Changes WILL NOT carry back to the calling code

36
Pass by Value
  • Declared with ByVal keyword
  • Procedure receives a copy of the value that was
    sent to the procedure
  • A local variable will be declared and initialized
    with that value
  • Changes made to the value will effect only the
    __________ copy of the variable
  • Changes WILL NOT carry back to the calling code

37
Pass by Value
  • Declared with ByVal keyword
  • Procedure receives a copy of the value that was
    sent to the procedure
  • A local variable will be declared and initialized
    with that value
  • Changes made to the value will effect only the
    local copy of the variable
  • Changes WILL NOT carry back to the calling code

38
Pass by Reference
  • Declared with ByRef keyword
  • Procedures receive the _______________ of the
    variable sent as a parameter
  • Local variable is created with the same memory
    location of the original parameter
  • Changes made to the value of the local variable
    ______ effect the value of the original
    parameter
  • Changes are carried back to the calling code.

39
Pass by Reference
  • Declared with ByRef keyword
  • Procedures receive the memory address of the
    variable sent as a parameter
  • Local variable is created with the same memory
    location of the original parameter
  • Changes made to the value of the local variable
    ______ effect the value of the original
    parameter
  • Changes are carried back to the calling code.

40
Pass by Reference
  • Declared with ByRef keyword
  • Procedures receive the memory address of the
    variable sent as a parameter
  • Local variable is created with the same memory
    location of the original parameter
  • Changes made to the value of the local variable
    WILL effect the value of the original parameter
  • Changes are carried back to the calling code.
Write a Comment
User Comments (0)
About PowerShow.com