1 / 17

UTPA – Fall 2011

Explore the components of a module or class, learn the syntax and declaration of methods, understand subroutines and functions, and discover how to use recursions.

littlec
Télécharger la présentation

UTPA – Fall 2011

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. CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011

  2. Objectives • In this chapter, you will: • Explore the components of a module or a class • Get familiar with the syntax of methods • Learn the declaration of methods • Subroutines • Functions • Become aware of .Net Namespace • Discover how to use recursions

  3. Modules, Classes and Methods • Modules and classes make up a program • Modules and classes are composed of smaller pieces called methods, instance variables and properties • Related classes are grouped into namespaces • Two types of methods exist: • Subroutines • Functions

  4. Pre-Packaged and User Defined • .NET framework provides a class library that contains many pre-packaged classes: • Mathematical calculations, error checking, GUI & graphics, String and character manipulations, I/O operations, XML, database, web, etc. • Users can define their own classes • You can combine new modules and classes with pre-packaged ones

  5. Method Basics • A method is invoked by a method call • Method call specifies the method name and provides information as arguments • When method is finished the control goes back to the caller • The method may or may not return results • Method provides for divide-and-conquer approach to software engineering • Methods can be used over and over again

  6. Methods That Do Not Return a Value • Methods that only performs a task such as printing, but do not return a value • These methods are generally called Subroutines • The parameters passed into these methods are ByVal parameters.

  7. Declaring a Subroutine • SubmethodName (parameterList) • Declarations and statements End Sub • If you pass a parameter ByRefand if the value of that parameter is changed by the subroutine, that variable’s value also will be changed in the calling module

  8. Methods That Return A Value (Functions) • Functions must have a return statement followed by what is being returned • Declared as follows: Function Square(ByVal v As Integer) As Integer Return v^2 End function • Please pay attention to the type in the last portion of function heading

  9. Shared Methods • Methods declared in a module are shared by default • If you want to share a method in a class, use the modifier Shared in the header of the method (instead of private or public) – write and writeline are shared methods • Class Math contains many shared methods

  10. Subroutines • A Method that does not return a value • Pass values in and out through interfaces, ByVal/ByRef parameters • ByVal • A copy is passed • May pass actual values rather than variables • Original value is not changed • ByRef • Memory location is passed • Value stored in that location may change • Cannot pass a value, must pass a variable • Side effects may occur

  11. Functions • Returns exactly one value through its name • Name must have an associated type

  12. .Net Namespaces • System.windows.forms • Contains the classes required to create and manipulate GUIs • System.io • Contains classes that enable programs to input and output data • System.Data • Contains classes that enable program to access and manipulate databases • There are many others

  13. Recursion • A method calling itself • The method only knows how to solve the simplest case (base case) • A more complex case is divided into two pieces, one base case and remaining as a complex case • Factorial of 5 is 5 times factorial of 4. • Factorial of 1 is 1 (base case)

  14. See Recursion Examples

  15. Method Overloading • Multiple methods with the same name but different signatures • Meaning different numbers and types of parameters OR different ordering of parameters by type Functionsquare(ByVal value AsInteger) AsInteger Return convert.toInt32(value ^ 2) End Function Functionsquare(ByVal value AsDouble) AsInteger Return value ^ 2 EndFunction

  16. More Examples (Optional Parameters) • May have one or more optional parameters (list after the required parameters) • Specify default value in case not passed in FunctionPower(ByVal base AsInteger,OptionalByVal exponent AsInteger =2) AsInteger Dim total AsInteger =1 For i AsInteger = 1 To exponent total *=base Next Return total EndFunction

More Related