Chapter 8 - Functions and Functionality

Slides:



Advertisements
Similar presentations
Sub and Function Procedures
Advertisements

Chapter 7: Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
VBA Modules, Functions, Variables, and Constants
Example 2.
Chapter 5 - Menus, Sub Procedures, and Sub Functions  Menus - controls - properties and events –menu editor - create and change –defining menus - menu.
1 Prototyping for HCI Spring 2004 (Week 8) Jorge A. Toro.
Using the Visual Basic Editor Visual Basic for Applications 1.
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
Chapter 7: Sub and Function Procedures
Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.
VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Apply Sub Procedures/Methods and User Defined Functions
CS0004: Introduction to Programming Variables – Numbers.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Automating Tasks with Visual Basic. Introduction  When can’t find a readymade macro action that does the job you want, you can use Visual Basic code.
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 6 Multiple Forms.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Multiple Forms and Standard Modules
Why to Create a Procedure
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
Multiple Forms, Container Controls, AddHandler This presentation is based on the Forms and ContainerControls VB Projects 1.
CS0004: Introduction to Programming Subprocedures and Modular Design.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
Sub procedures School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 6, Friday 2/21/03)
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
Chapter 6 Sub Procedures
SYSTEMSDESIGNANALYSIS 1 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright © 1999.
Introduction to VB.NET 2005 Dr. McDaniel IDS4704 Spring 2005.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
Lesson 1. Security At the menu bar at the top you will see the word Tools. Click your mouse on Tools scroll down to Macro. Move the Mouse over and down.
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
Controlling Program Flow with Decision Structures.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
PaintPictureBoxDemo Refers to the PaintPictureBoxDemo Visual Basic Program Included With The Lecture.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Subroutines and Functions Chapter 6. Introduction So far, all of the code you have written has been inside a single procedure. –Fine for small programs,
Sub Procedures Chapter 6-Part 1. Chapter 6 Part 12 Event Procedures Code that occurs based upon event. Mouse click Got focus Repetitive code that might.
Visual Basic I Programming
Multiple Forms and Menus
Visual Basic Fundamental Concepts
Programming Right from the Start with Visual Basic .NET 1/e
VBA - Excel VBA is Visual Basic for Applications
IS 350 Application Structure
Apply Procedures to Develop Message, Input, and Dialog Boxes
Microsoft Access 2003 Illustrated Complete
Using Procedures and Exception Handling
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Chapter 4 - Visual Basic Schneider
Microsoft Visual Basic 2005 BASICS
Visual Basic..
Chapter 6 Sub Procedures
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
CS285 Introduction - Visual Basic
Controls, Properties, and Procedures
Procedures: Functions and Subroutines
Controls, Properties, and Procedures
Sub Procedures Functions And Call Statements
Introduction to VB programming
10.2 Procedures Passing Parameters 30/08/2019.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

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… 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 Procedures Sections of code that handle occurrences. Subroutines Sections of code that perform routine processing Functions Sections 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

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 Call keyword 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.