CSI 101 Elements of Computing Spring 2009 Lecture #10 – Functions and Subroutines Monday, March 16th
2 Procedural Programming Method of organizing computer program Breaking up code into smaller segments Makes it more efficient Allows multiple programmers to produce Facilitates reuse Tie segments together logically Code statements in segment perform a particular service or function
3 How does Procedural Programming work? Think of it like an assembly line Each part of assembly line works on a specific task or piece of the final product Dedicated workers focus their efforts just on their part of the process Foreman oversees entire process and makes sure it runs smoothly
4 Types of procedural segments 3 types in Visual Basic Event procedure Invoke when a particular action occurs on a screen form Subroutine procedure Invoked via a CALL statement Function procedure Invoked by referring to its name Example: result = Foo()
5 Parameters Input to procedure Data type must match general type of procedure Doesn’t have to be exact match For instance, integral values could pass a Long into an Integer if value does not exceed Integer’s maximum If more than one parameter, corresponds in order
6 Parameter Examples Foo has 3 inputs: Integer, Integer, String Dim A as Integer, B as Integer, C as Integer Dim S1 as String, S2 as String Dim X As Long, Y as Single, Z as Double CALL Foo(A,B,S1) works CALL Foo(S1,A,B) fails – WHY? S1 is 1 st parameter, would correspond to Integer B is 3 rd parameter, would correspond to String
7 Parameter Examples, cont Foo has 3 inputs: Integer, Integer, String Dim A as Integer, B as Integer, C as Integer Dim S1 as String, S2 as String Dim X As Long, Y as Single, Z as Double CALL Foo(A,X,S1) Works if X's value is within 2 billion CALL Foo(A,Y,S2) works – WHY? Removes any fractional value from Y
8 Defining Subroutines SUB statement begins it Like BEGIN from pseudocode, it must name the subroutine END SUB ends it Unlike END in pseudocode, does not need name Cannot define another subroutine within a subroutine, so the END SUB would refer to the previous SUB statement
9 Subroutine arguments Arguments are the subroutine’s corresponding parameters Parameters are considered what the calling process uses, while arguments are defined by the subroutine Arguments have variable names and data types just like parameters Uses same structure as DIM without DIM keyword
10 Defining Subroutine example Let’s define our previous FOO: Sub Foo(InVal1 As Integer, InVal2 As Integer, InStr As String) : End Sub Note each argument is given a unique name and has a defined data type
11 Parameter passing 2 ways: Pass by reference Called routine does not make a copy, but works in original register Thus, any change to argument in called routine is reflected in original procedure’s parameter value This is Virtual Basic’s default Pass by value Called routine makes a copy of data value when called Any change to called routine’s copy is not reflected in the original procedure’s variable
12 Pass by Reference Example Foo(Int1, Int2, String1) If Int1 > Int2, String1 set to “Higher” If Int1 < Int2, String1 set to “Lower” If Int1 = Int2, String1 set to “Equal” CALL Foo(A, B, S1) A and B retain value, but S1 changes
13 Pass by Value Need to specify ByVal in subroutine definition Note that this is controlled by subroutine, not by calling routine Thus, could change Foo by: Sub Foo2(Int1 As Integer, Int2 As Integer, ByVal Str1 As String) Note ByVal keyword goes before argument name
14 Pass by Value Example Foo2(Int1, Int2, String1) If Int1 > Int2, String1 set to “Higher” If Int1 < Int2, String1 set to “Lower” If Int1 = Int2, String1 set to “Equal” CALL Foo2(A, B, S1) No variables have their values changed when Foo2 ends
15 Subroutine return values Pass by reference is how subroutines return a value to its calling procedure Must use an argument, so calling procedure must have variable of correct data type to use as argument
16 Functions These are special processing segments that are invoked by using their name They have a single return value, and it is placed in a variable that is NOT a parameter Computer creates a variable with the same name as the function. By default, that is the variable used to return the function's return value Visual Basic has many predefined functions, but programmers are free to create their own as well
17 Form of a Function Begins with FUNCTION keyword Concludes with END FUNCTION Arguments defined as in subroutine Includes definition of return value Example: Function Foo3(Val1 As Integer, Val2 As Integer) As Integer : End Function
18 Return keyword Simultaneously sets return value and ends the function Return A + B Can be placed anywhere before END FUNCTION Often used with IF statement to end function on particular condition IF index > 255 Then 'Exceed max value Return 0 'Value for error Else 'Continue processing
19 Function Example Let’s create a function as an example Our function finds the minimum value of three numbers Call it MIN We'll restrict our values to integers Return value is one of them, so it’s also an integer
20 MIN Function Function Min(A As Integer, B As Integer, C As Integer) As Integer If A<B Then 'A smaller If A<C Then Min=A 'Note we can set Min 'Min is the variable created to hold the return value Else Min=C Endif Elseif B<C Then Min=B Else Min=C EndIf End Function 'Note no RETURN statement RETURN not necessary if variable named for function contains the return value
21 Invoking MIN Dim A,B,C,M as Integer 'At some point, we get values for A, B, and C M = Min(A,B,C) 'M now holds the smallest value