22
Procedures Subs and Functions

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

Embed Size (px)

Citation preview

Page 1: 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

Procedures

Subs and Functions

Page 2: 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

2

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.

Page 3: 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

3

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.

Page 4: 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

4

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.

Page 5: 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

5

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.

Page 6: 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

6

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.

Page 7: 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

7

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.

Page 8: 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

8

Calling a Procedure• The sub below calls the DisplayScore sub several

times.• The comments explain the various ways that a sub

can be called.

Page 9: 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

9

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:

Page 10: 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

10

Optional Parameters

• The optional parameter is Name.• The default value is “Sir/Madam”.• Here are two lines of code which call this sub:

Page 11: 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

11

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.

Page 12: 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

12

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:

Page 13: 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

13

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:

Page 14: 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

14

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.

Page 15: 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

15

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!

Page 16: 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

16

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.

Page 17: 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

17

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.

Page 18: 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

18

Bad Function Example 2

• The line “MessageToSend = s” makes this a bad function.

Page 19: 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

19

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.)

Page 20: 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

20

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.

Page 21: 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

21

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:

Page 22: 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

22

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.