Sams Teach Yourself Visual Basic 6 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- Introduction
- Who Should Read This Book
- What This Book Will Do for You
- Can This Book Really Teach Visual Basic in 24 Hours?
- What You Need
- Files on the Visual Basic Distribution CD-ROM
- Conventions Used in This Book
- Enough! Time Is Ticking!
- Part I: Introducing Visual Basic
- Hour 1. Visual Basic at Work
- Hour 2.Analyzing Visual Basic Programs
- Hour 3.Controls and Properties
- Hour 4.Examining Labels, Buttons, and Text Boxes
- Part II: Coding the Details
- Hour 5.Putting Code into Visual Basic
- Hour 6.Message and Input Boxes
- Hour 7.Making Decisions
- Hour 8.Visual Basic Looping
- Part III:Putting Code to Work
- Hour 9.Combining Code and Controls
- Hour 10.List Boxes and Data Lists
- Hour 11.Additional Controls
- Hour 12.Dialog Box Basics
- Part IV:Programming with Data
- Hour 13.Modular Programming
- Structured Programming
- Calling Procedures and Returning from Them
- Coding Subroutines
- Coding Functions
- Coding Arguments
- Receiving by Reference and by Value
- Summary
- Q&A
- Workshop
- Hour 14.Built-In Functions Save Time
- Hour 15.Visual Basic Database Basics
- Hour 16.Printing with Visual Basic
- Part V:Sprucing Up Programs
- Hour 17.Menus and Visual Basic
- Hour 18.The Graphic Image Controls
- Hour 19.Toolbars and More Graphics
- Hour 20.Writing Correct Applications
- Part VI:Advancing Visual Basic Applications
- Hour 21.Visual Basic and ActiveX
- Hour 22.Object Basics
- Hour 23.Distributing Your Applications
- Hour 24.Online Visual Basic
- Part VII:Appendixes
- Appendix A.Operator Precedence
- Appendix B.Answers
- Appendix C.Using the CD-ROM
Calling Procedures and Returning from Them
The previous section discusses calling procedures. You have learned about the Call keyword, but you haven't been exposed to Call before now. That is, you haven't been directly exposed to Call even though you have performed a similar action by using the built-in Val() and Format() functions.
When one procedure contains a Call statement, the Call statement puts the current procedure on hold and executes the called procedure. Here is one of the formats of the Call statement:
Call Procedure
When one procedure's execution reaches its Call statement, that procedure is put on hold and execution begins at the called Procedure . After the called procedure ends (whether it ends with the End Sub statement or an Exit Sub statement or by other means), the called procedure returns control to the calling procedure. The same thing happens when you call the built-in functions because a built-in function is a special kind of procedure: Your code temporarily stops, and the built-in function's code takes over and uses the argument(s), and finally returns a value as well as control back to your code.
You've seen event procedures and you've executed the built-in function procedures, and Visual Basic supports two other kinds of procedures:
- Standard subroutine procedures
- Standard function procedures that you write
A standard subroutine or function procedure doesn't respond to an event. A standard procedure only executes when called from elsewhere in the program.
Standard procedures, whether they are subroutines or functions, can reside either inside a form module (following the event procedures) or inside an external module file you add to your project. Figure 13.1 illustrates the difference between subroutines and functions. The calling code calls both and they both do work. The subroutine doesn't return a value to the calling procedure. The function does return a value to the calling procedure, and the calling procedure must do something with that value such as assign the value to a variable or control. By the way, you'll understand all that's happening in Figure 13.1 before this lesson is over, so if some of it confuses you right now, don't be alarmed.
Figure 13.1 Both subroutines and functions do work, but only functions return values.
As Figure 13.1 illustrates, when you want to write a procedure that performs a task but doesn't need to return a value, write a subroutine procedure. If you need to write a procedure that performs a task and returns a value, such as a calculated result, write a function procedure. You can pass arguments to either kind of procedure.
A standard function procedure is a standalone nonevent procedure that does work when called by another procedure and returns a single value to that called procedure. A standard subroutine procedure is a standalone nonevent procedure that does work when called by another procedure.
Coding Subroutines | Next Section

Account Sign In
View your cart