1 / 22

Procedures

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.

taya
Download Presentation

Procedures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Procedures Subs and Functions

  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.

  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.

  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.

  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.

  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.

  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.

  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.

  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:

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

  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.

  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:

  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:

  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.

  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!

  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.

  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.

  18. Bad Function Example 2 • The line “MessageToSend = s” makes this a bad function.

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

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

  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:

  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.

More Related