220 likes | 333 Views
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.
E N D
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.
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.
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.
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 ByVal (by value); VB will fill this in for you if you don’t type it. • The other way of declaring a parameter,ByRef (by reference) is rarely used in VB; we won’t use it in 373.
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.
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.
Calling a Procedure • The sub below calls the DisplayScore sub several times. • The comments explain the various ways that a sub can be called.
Optional Parameters • Parameters can be declared Optional. • Optional parameters must come after all required parameters. • They are declared using the keyword Optional before ByVal. • 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 “ = DefaultValue” after the data type. • The following slide shows an example:
Optional Parameters • The optional parameter is Name. • The default value is “Sir/Madam”. • Here are two lines of code which call this sub:
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 SubsFunctionsVideoExample.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.
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:
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:
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.
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!
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.
Bad Function Example 1 • This function is bad because it assigns a value to Label1.Text. • It won’t work if there is no Label1. • Changing Label1’s Text property is an unanticipated side effect.
Bad Function Example 2 • The line “MessageToSend = s” makes this a bad function.
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 BandMember, etc.)
Returning the Value • VB.NET provides two ways to return a value from a function: • Use the function name, as in VBA: • Use Return. The book recommends this method.
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:
Functions Video • For a detailed explanation of how functions and subroutines are called and parameters are passed, • Watch the SubsFunctions video available in Ctools. • You’ll also learn a lot about using the powerful debugging tools in VB.