290 likes | 306 Views
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…
E N D
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… A lot of your questions will finally be answered… …And a lot will be raised.
Chapter 8 is a must-read chapter It covers the essentials of VB Programming
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
This is too much to memorize… • For now, just know where to look it up!
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
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.
Your Project • You can manage your project from the project window. • Add/Delete Modules and Forms • Change Views
Making a procedure • Go to form view • Click on Tools • Add Procedure
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.
Calling a subroutine • Call mySubroutine() • mySubroutine() • The first syntax is clearer, so lets use it.
Calling an Event Procedure The computer keeps a constant watch out for events.
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)
Scope • Scope is the realm of visibility, or use, of a variable or procedure.
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.
Variable Scope • Public (or global) • Private (or local)
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
So a variable has three scope levels: • Private to a subroutine • Module level • And truly global
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?
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.
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.
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.
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.
You can pass an object as an argument. • A control is an object. • Useful under some circumstances
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
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.