1 / 29

Chapter 8 - Functions and Functionality

Chapter 8 - Functions and Functionality. 092600. Todays lesson is like an episode of the X-files…. Todays lesson is like an episode of the X-files… A lot of your questions will finally be answered…. Todays lesson is like an episode of the X-files…

lcaballero
Download Presentation

Chapter 8 - Functions and Functionality

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. Chapter 8 - Functions and Functionality 092600

  2. Todays lesson is like an episode of the X-files…

  3. Todays lesson is like an episode of the X-files… A lot of your questions will finally be answered…

  4. Todays lesson is like an episode of the X-files… A lot of your questions will finally be answered… …And a lot will be raised.

  5. Chapter 8 is a must-read chapter It covers the essentials of VB Programming

  6. What is a VB Program? The Visual Basic program code is, for the most part, on long set of event procedures with a Declarations section at the beginning of the code. p. 216

  7. This is too much to memorize… • For now, just know where to look it up!

  8. Modules • A module is a file that holds procedure code. The file that holds the form and its code is technically called the Form module. • You can add a form or a module by right clicking on the project window. • You can view your project properties by clicking on the “view” menu and choosing “project properties”. p116

  9. Modules • All forms are modules. • Modules may also hold only code. • In brief, a module is a large section of related code. • The declarations section appears at the beginning of a module’s code section. • A procedure resides in a module and performs a specific task.

  10. Your Project • You can manage your project from the project window. • Add/Delete Modules and Forms • Change Views

  11. Making a procedure • Go to form view • Click on Tools • Add Procedure

  12. 3 types of procedures covered so far • Event ProceduresSections of code that handle occurrences. • SubroutinesSections of code that perform routine processing • FunctionsSections of code that return a value.

  13. Calling a subroutine • Call mySubroutine() • mySubroutine() • The first syntax is clearer, so lets use it.

  14. Calling an Event Procedure The computer keeps a constant watch out for events.

  15. Calling a function • A function essentially becomes a value and passes that value back to the program. • For instance, an InputBox “becomes” the string someone puts in. • strMyName=InputBox(“Type your Name”) • intX=sqr(16)

  16. Scope • Scope is the realm of visibility, or use, of a variable or procedure.

  17. Procedures -- Public or Private • Controls (buttons, etc) are always Public • Public Procedures are visible to the entire project • Private Procedures are visible to the current module. • Very analogous to public places and Private places in real life.

  18. Variable Scope • Public (or global) • Private (or local)

  19. Variable Scope • A local variable can be used only by code to which it is directly visible. • A global variable can be used by code outside its declared area, but not outside its module. • A global variable is visible to other modules in the application if you declare it such:Public gIntMyAge as Integer

  20. So a variable has three scope levels: • Private to a subroutine • Module level • And truly global

  21. Functions

  22. Functions return a value intDiceRoll=RollDice(6) Public Function RollDice(intSides as Integer) Randomize RollDice=int(rnd()*intSides +1) End Function There are actually three functions here -- can you spot the intrinsic functions?

  23. Arguments • Arguments are the values passed into a subroutine. In events such arguments may automatically include mouse coordinates, ASCII values, and so on. • Private Sub mySubroutine(intX as Integer, strName as String) • The following call the above subroutine: • Call mySubroutine(intZ, strEmployee) • mySubroutine(5, “Bob”) • The missing Callkeyword does not otherwise affect the syntax.

  24. Changing Data • Subroutines and Functions can change all the variables because the values are passed “by reference”. That means they refer to the actual memory locations. • If you don’t want the values changed, pass the data “by value” only. • Keywords: ByRef, ByVal • ByRef is the default.

  25. Example • Public Sub myStudent (ByRef intGrade as Integer, ByVal strName as String, intFinal as Integer) • What above value(s) can the subroutine change. What above value(s) can it not.

  26. Warning! Warning! • The default is ByRef • You can change other variables to which you have access--for instance public variables. • Make variables private whenever possible for proper housekeeping and scalability of your program.

  27. You can pass an object as an argument. • A control is an object. • Useful under some circumstances

  28. Summary • Module -- A file containing code • Form Module -- A file containing form generation information and code • Procedure -- A section of code that performs a task • Event Procedure -- A section of code that responds to an occurrence • Subroutine -- A section of code you may call from elsewhere in the program • Function -- A procedure that returns a value • Arguments --The values passed to a subroutine or function

  29. Summary • Variable scope can be private, public to the module, or global to the application • Procedures can be private to the module or public to the application. • You may make a module to store procedures. Then the procedures are easily reusable.

More Related