Download presentation
Presentation is loading. Please wait.
1
PSU CS 106 Computing Fundamentals II VB Subprograms & Functions HM 4/29/2008
2
2 © Dr. Herbert G. Mayer Agenda Subroutine Syntax Call Statement Value Parameters Reference Parameter Function Syntax Order of Parameter Evaluation Recursion
3
3 © Dr. Herbert G. Mayer Subroutines Large VB programs can be modularized into smaller, logical entities: –subs and functions Subs (AKA Subroutines) as well as functions are defined with name and open a new, nested scope Both have parameters, are called, and scope can be constrained: –public or private –Default is private Subs invoked in a call statement Functions called by use in expressions –return a value to place of call Actual parameters must match defined formal parameters –one-to-one –by position –and by their respective types
4
4 © Dr. Herbert G. Mayer Sub Syntax Generic Syntax Rules: Scope Sub Name ( Formals ) Body End Sub Scopeoptional. If used, specify Private or Public Public extends visibility Nameany user-defined name, is callable by that name Formals0 or more formal parameters, comma-separated, if > 1 Each formalreference or value parameter, each with defined type Bodyoptional declarations, and optional statements A real example: Sub swap_if( ByRef a As Integer, ByRef b As Integer ) Dim Temp As Integer = a ‘ scope limited to: inside Sub If a < b Then a = b b = temp End If End Sub ‘ swap_if
5
5 © Dr. Herbert G. Mayer Call Statement Sub known as procedure, subroutine, proc in other languages Sub is called in call statement Use call statement any place where a statement is allowed Call transfers control to body of the named Sub –After binding the actual to the formal parameters Binding is done by position, left to right Body of callée is executed –until end is reached –or until a Return Statement is executed Return Statement is implied at end of Sub body Body may contain any number of local declarations Body may contain any number of statements, including 0 When Sub end is reached, or Return Statement executed, control transfers back to place after the call statement
6
6 © Dr. Herbert G. Mayer Parameter Passing At place of call, 0 or more actual parameters are provided, as defined in formal declaration Number of formal parameters is defined in the Sub Declaration Association of actual parameters to formal parameters is by position, from left to right Types must be pair-wise compatible If not equal, implied type conversion must ensure compatibility; else error If more than 1 parameter, each is separated from predecessor by comma
7
7 © Dr. Herbert G. Mayer Value Parameter Value Parameter specified via ByVal keyword in formal sub declaration Actual may be any type-compatible expression Actual parameter provides initial value, which is bound to the formal Formal may be assigned inside Sub during call But after return, the actual is unmodified, even if assignments to formal have taken place during call
8
8 © Dr. Herbert G. Mayer Value Parameter Local bound to Value Parameter unchanged after call Sub Demo_V( ByVal a As Integer ) Assert( a = 109, “Must be 109 in Demo_V, b4 is: ”, a ) a += 1 Assert( a = 110, “Must be 110 in Demo_V, aft is: ”, a ) End Sub ‘ Demo_V Sub Calling( ) Dim local As Integer = 109 Assert( local = 109, “Must be 109 in Calling b4”, local ) Demo_V( local ) Assert( local = 109, “Must be 109 in Calling aft”, local ) End Sub ‘ Calling
9
9 © Dr. Herbert G. Mayer Reference Parameter Reference Parameter specified via ByRef keyword in formal sub declaration Actual may be any type-compatible object, including a subscripted element or a full array Actual parameter may provide initial value, which then initializes the formal Formal may be assigned inside Sub during the call After return, actual parameter holds last value assigned to the formal during the call That is the major difference between Value and Reference Parameter passing!
10
10 © Dr. Herbert G. Mayer Reference Parameter Local bound to Reference Parameter changed after call Sub Demo_R( ByRef a As Integer ) Assert( a = 109, “Must be 109 in Demo_R, b4 is: ”, a ) a += 1 Assert( a = 110, “Must be 110 in Demo_R, aft is: ”, a ) End Sub ‘ Demo_R Sub Calling( ) Dim local As Integer = 109 Assert( local = 109, “Must be 109 in Calling b4”, local ) Demo_R( local ) Assert( local = 110, “Must be 110 in Calling aft”, local ) End Sub ‘ Calling
11
11 © Dr. Herbert G. Mayer Functions VB functions similar to Subroutines: –Function has name, by which it is called in expression –Function opens a new scope, like Sub –Function may have formal parameters, matched at call –Function returns a value –Return Type is specified in function declaration Function scope constrained as public or private Function invoked in expressions Function returns value to place of call Actual parameters must match defined formal parameters one-to-one by position and in type
12
12 © Dr. Herbert G. Mayer Function Syntax Generic Syntax Rules: Scope Function Name ( Formals ) As type Body Return expression ‘ anywhere in the body End Function Scopeoptional scope specified. If used: Private or Public Public extends visibility Nameany user-defined name, called by that name Typemust be any VB type; function returns value of that type Bodyoptional declarations, statement list with Return Statement A real example: Function max( ByVal a() As Integer, ByRef inx As Integer ) As Integer Dim max_val as integer = a(0) Dim i as integer inx = 0 For i = 1 To UBound ( a )‘ note built-in function UBound() If a(i) > max_val Then max_val = a(i)‘ found a new max inx = i‘ remember index of max End If Next Return max_val‘ return max, index passed by ref End Function
13
13 © Dr. Herbert G. Mayer Order of Parameter Evaluation For Subs and function calls, actual parameters are evaluated before starting call Before call each actual is bound to its corresponding formal parameter by position The types are equal, or else are converted, else are incompatible and cause an error Actual parameters are evaluated from left to right Note that this may have side-effects for subsequent actual parameter Actual parameter evaluation may even have side- effects for global objects, visible inside callée
14
14 © Dr. Herbert G. Mayer Recursion Def: An algorithm is recursive, if it is partly defined by simpler versions of itself Different from infinite regress, which is recursion gone awry! Sample of recursive algorithm, the factorial function of integer argument n, named fact(n) –fact(0)= 1 –fact(1)= 1 –for all n > 1: –fact(n)= fact(n-1) * n VBasic Sub may also be recursive, directly or indirectly Direct recursion: call to a() is issued from inside Sub for the same function a() Indirect recursion: call to a() is issued from another sub b(), that is called from a(), before a() returns
15
15 © Dr. Herbert G. Mayer Recursion Sample Function fibo( ByVal n As Integer) As Integer ‘ unsigned If n < 2 Then‘ base case, assumed unsigned Return n‘ return argument value Else Return fibo( n – 1 ) + fibo( n – 2 )‘ recurse! End If End Function ‘ fibo... Msgbox( “ Fibo( 5 ) = ” & fibo( 5 ) )
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.