Procedures Subs and Functions Procedures Before OOP subroutines
Procedures Subs and Functions
Procedures • Before OOP, subroutines were the primary high-level way to organize a program. • In OOP, this role has been taken over by the Class. • Nevertheless, at some point the organization stops and the work begins. • The work in any program is done by the procedures. 2
Subs and Functions • Visual Basic uses two types of procedures: Subroutines (Subs) and Functions. • We could add properties as well, but a property is simply one sub and one function operating under the same name. • The words “procedure” and “method” both mean a Sub or a function. 3
Sub or Function? • As far as VB is concerned, the only difference between a Sub and a Function is that a Function returns a value, but a Sub does not. • However, good programming (as required in this course) demands a stricter distinction: – A Function is the calculator: it performs only one role— returning a value (usually based on the input parameters). It should perform no other tasks that cause changes in the working of the program. – A Subroutine is the worker that gets things done. It may make use of functions or other subs in order to get its job done. A sub may cause any number of changes in the working of the program. 4
Parameters • Both subs and functions can take parameters, also known as arguments. • In the sub below, the graphics object “g” is the only parameter. • Parameters are by default By. Val (by value); VB will fill this in for you if you don’t type it. • The other way of declaring a parameter, By. Ref (by reference) is rarely used in VB; we won’t use it in 373. 5
Parameters • Subs and functions can take multiple parameters of different types. • Multiple parameters should be separated by commas. • The parameters work just like local variables in the sub; whatever value is passed to a parameter is used wherever that parameter’s name is used in the procedure. 6
Parameter Example • This sub has five parameters: two Strings, two Integers, and one Boolean. • When the sub is called, whatever value is passed as the first parameter will become the value of hometeam, the second value will become visitors, etc. 7
Calling a Procedure • The sub below calls the Display. Score sub several times. • The comments explain the various ways that a sub can be called. 8
Optional Parameters • Parameters can be declared Optional. • Optional parameters must come after all required parameters. • They are declared using the keyword Optional before By. Val. • They must have a default value which will be used if the calling code doesn’t include a value for this parameter. • This is indicated by typing “ = Default. Value” after the data type. • The following slide shows an example: 9
Optional Parameters • The optional parameter is Name. • The default value is “Sir/Madam”. • Here are two lines of code which call this sub: 10
Wait for the Movie • If you are having trouble understanding how subs, functions, and parameters work, I have created another exciting* video which demonstrates them in action and in greater detail. • The video is in Resources/Videos under the name “Subs, Functions, Debugging Video”. It should be playable on CAEN computers. It requires the Flash player. • The VB program demonstrated in the video is also available there: it is called Subs. Functions. Video. Example. zip. • If you think you understand subs and functions but want to see how the debugging tools work, you can jump to six minutes and ten seconds into the video. * Yeah right. 11
Parameter Arrays • Sometimes you will want to have a subroutine or function which takes an indefinite number of parameters. To do this, use a parameter array. The function below demonstrates: 12
Parameter Arrays • When you have a procedure which includes a parameter array, you can call it just by passing a comma-separated list of values, like this: �You can also pass it an array of the appropriate type, like this: 13
Functions • A function is like a sub—it is a procedure that can take parameters. • The differences are: – A function returns a value; – A function’s only function (so to speak) is to return a value. 14
Functions—One purpose only! • Suppose that you had written a square root function like this: • If those three subroutines do what they promise, you’re going to be unhappily surprised if you ever use this function! 15
What functions should and shouldn’t do • Functions should ONLY figure out the return value. • They should NEVER change the value of variables or properties, and they shouldn’t call subroutines. NO SIDE EFFECTS! • They can call other functions and use variables defined in the class. 16
Bad Function Example 1 • This function is bad because it assigns a value to Label 1. Text. • It won’t work if there is no Label 1. • Changing Label 1’s Text property is an unanticipated side effect. 17
Bad Function Example 2 • The line “Message. To. Send = s” makes this a bad function. 18
Function Return Types • Since the purpose of a function is to return a value, all functions should have data types. • VB requires this if Option Strict and Option Explicit are both on (as required for this class). • The data type can be – any built-in value type (Integer, Double, String, Boolean) – Any built-in reference type (Form, Label, etc. ) – Any data type defined in the program: a class type, an enumeration type, a structure type, or an interface type. • The return type is given after the close parenthesis of the parameter list, using “As” (As Integer, As Band. Member, etc. ) 19
Returning the Value • VB. NET provides two ways to return a value from a function: 1. Use the function name, as in VBA: 2. Use Return. The book recommends this method. 20
Calling Functions • Since a function returns a value of a particular datatype, it can be used in expressions just like variables of that data type: 21
Functions Video • For a detailed explanation of how functions and subroutines are called and parameters are passed, • Watch the Subs. Functions video available in Ctools. • You’ll also learn a lot about using the powerful debugging tools in VB. 22
- Slides: 22