Using General Sub
Procedures/Routines and
functions in Applications
• Using General Sub procedures can
help divide a complex application
into more manageable units of code.
This helps meet t...
• Breaking your programs into as many
small but logical sections as possible.
the smaller routines make your
programming a...
A Visual Basic program consists of:
• A form with controls that act as the
program’s background and user
interface
• A gen...
Kinds of Procedures
Event-procedures-
executes as events occur
Procedure is really
not an executable
procedure in the wa...
Defining a Sub Procedure/Routines
• A sub procedure or a subroutine
is a procedure that executes the
lines of code within ...
 The definition header names the Sub
procedure and defines any argument
passed to the procedure.
 Arguments are a comma-...
*Example of a code that contains an
event-procedure
• Event procedures are specific subroutines
tied directly to control e...
BENEFITS FROM USING SUB
PROCEDURE ROUTINES
Eliminates redundant
codes
Codes will be reusable
Program code will be
simpl...
MAKING SUBROUTINE
• You can add a sub to your
project in two ways:
• By writing the code directly
into the General Declara...
ENABLING THE ADD PROCEDURE
MENU ITEM
 For the Add procedure menu item to be
enabled, you must be in the Code window
view ...
TYPING THE SUBROUTINE IN
THE CODE WINDOW
1. Go to the Code
Window.
2.Type Sub GenAverage.
3.Press Enter key.
 In selecting the Insert Procedure Menu
item, note another option for your
procedure is Scope. You have the option of
Pub...
CALLING A SUB PROCEDURE
• Method 1:
Call GenlSubProc (Arguments) (if
there are no Arguments, do not type
the parentheses)
...
Object Properties Values
Label 1 Caption US Dollar
Label 2 Caption Peso
Exchange
Rate
Label 3 Caption Peso Value
Text 1
Te...
3. Add a procedure name
USPesoConvert by using the
Add Procedure in the tools
menu.
4. Type the following code
for each pr...
Private Sub Form_Load()
Text 1 = “ “
Text 2 = “ “
Text 3 = “ “
End Sub
Public Sub USPesoConvert ()
Text 3 = Text1 & Text2
...
To call dollar exchange routine, we
could use:
Private Sub cmdConvert_Click ()
Call USPesoConvert
End Sub
Or
Private Sub c...
PASSING ARGUMENTS TO SUB
PROCEDURES
You can enhance the power and
versatility of subs and functions
by using arguments.
A...
THE ADVANTAGES OF THE LATTER
METHOD IS TWOFOLD:
One call satisfies many needs
throughout your code.
If you need to enhan...
USING GENERAL FUNCTION
PROCEDURES IN APPLICATIONS
Function procedures work
a lot like subroutine; you
can call them from
e...
In Syntax
• Private/Public are the optional Visual
Basic keywords that define the scope
of the function.
• Function is the...
• ReturnValue is the value that you
pass back from the function by
assigning it top the function’s name
(this is very impo...
• A function procedure always begins
with the Function statement and
ends with the End function
statement.
• A function is...
LOCATING FUNCTION PROCEDURES
• Can be located in forms
or modules.
• They are created using
exactly the same process
descr...
Upcoming SlideShare
Loading in …5
×

Using general sub procedures

612 views

Published on

I hope this can help

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
612
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Using general sub procedures

  1. 1. Using General Sub Procedures/Routines and functions in Applications
  2. 2. • Using General Sub procedures can help divide a complex application into more manageable units of code. This helps meet the above stated goals of readability and reusability. • Most applications have tasks not related to objects that require some code to perform these tasks. Such tasks are usually coded in a general sub procedure, essentially the same as a routine in other languages.
  3. 3. • Breaking your programs into as many small but logical sections as possible. the smaller routines make your programming and subsequent maintenance easier. • In many traditional programming languages such as COBOL and FORTRAW, a program is like a long book without chapters. The code goes on and on and the program’s length is exceeded only by the boredom programmer’s face trying to wade through the code hunting down errors. •
  4. 4. A Visual Basic program consists of: • A form with controls that act as the program’s background and user interface • A general-purpose procedure named, found in the Code Window’s object dropdown list • Event procedures that tie the controls together and add specific direction and calculations to the application • A CONSTANT.TXT file that provides named constants used by your code
  5. 5. Kinds of Procedures Event-procedures- executes as events occur Procedure is really not an executable procedure in the way that event procedures execute
  6. 6. Defining a Sub Procedure/Routines • A sub procedure or a subroutine is a procedure that executes the lines of code within its block but doesn’t return a value. The syntax for a simple sub is as follows: Sub GenlSubProc (Arguments) Definition Header : End Sub
  7. 7.  The definition header names the Sub procedure and defines any argument passed to the procedure.  Arguments are a comma-delimited list of variables passed to and/or from the procedure.  If there are arguments, they must be declared and typed in the definition header in this form: Var1as Type1, Var2 as Type2…  A subroutine always begin with the Sub statement and always ends with the End ?Sub statement.  A subroutine may or may not be an event procedure
  8. 8. *Example of a code that contains an event-procedure • Event procedures are specific subroutines tied directly to control events. • When you write a section of code that your application will have to execute more than once, that section of code is a great candidate for a general-purpose, non- event subroutine. Sub cmdExit_Click () End End Sub
  9. 9. BENEFITS FROM USING SUB PROCEDURE ROUTINES Eliminates redundant codes Codes will be reusable Program code will be simpler to maintain
  10. 10. MAKING SUBROUTINE • You can add a sub to your project in two ways: • By writing the code directly into the General Declarations section of a form or module; or • By using the Tools menu’s Add Procedure option
  11. 11. ENABLING THE ADD PROCEDURE MENU ITEM  For the Add procedure menu item to be enabled, you must be in the Code window view of the form or module into which you want to add the procedure. ADD A SUB TO YOUR PROJECT WITH THE ADD PROCEDURE 1. From the Tools menu, choose Add procedure to display the Add procedure dialog. 2. Enter the Sub name. 3. Click OK to add the sub’s code block to the form or module.
  12. 12. TYPING THE SUBROUTINE IN THE CODE WINDOW 1. Go to the Code Window. 2.Type Sub GenAverage. 3.Press Enter key.
  13. 13.  In selecting the Insert Procedure Menu item, note another option for your procedure is Scope. You have the option of Public or Private.  If a module procedure is public, it can be called from any other procedure in other module.  If a module procedure is Private, it can only be called from the module it is defined in. NOTE: Scope only applies to procedures in modules. *All event procedures and general procedures in a form are Private.
  14. 14. CALLING A SUB PROCEDURE • Method 1: Call GenlSubProc (Arguments) (if there are no Arguments, do not type the parentheses) • Method 2: GenlSubProc Arguments CREATING A PROCEDURE 1. Create a project. 2. Add the following controls in your form with the corresponding properties
  15. 15. Object Properties Values Label 1 Caption US Dollar Label 2 Caption Peso Exchange Rate Label 3 Caption Peso Value Text 1 Text 1 Text 2 Comman d 1 Name cmdConvert Caption &Convert
  16. 16. 3. Add a procedure name USPesoConvert by using the Add Procedure in the tools menu. 4. Type the following code for each procedure as shown below: Private Sub cmdConvert_Click () Call UsPesoConvert End Sub
  17. 17. Private Sub Form_Load() Text 1 = “ “ Text 2 = “ “ Text 3 = “ “ End Sub Public Sub USPesoConvert () Text 3 = Text1 & Text2 End Sub
  18. 18. To call dollar exchange routine, we could use: Private Sub cmdConvert_Click () Call USPesoConvert End Sub Or Private Sub cmdConvert_Click () USPesoConvert End Sub 5. Press F5 to execute the program.
  19. 19. PASSING ARGUMENTS TO SUB PROCEDURES You can enhance the power and versatility of subs and functions by using arguments. ARGUMENTS – also referred to as a parameter PARAMETER- a variable that acts as a placeholder for a value that you’ll pass into the sub or functiobn. Using arguments greatly increases the reusability of your code.
  20. 20. THE ADVANTAGES OF THE LATTER METHOD IS TWOFOLD: One call satisfies many needs throughout your code. If you need to enhance this functionality, you don’t have to go through your code and make enhancements line by line, you simply go back to the function and make the changes within the function’s code block.
  21. 21. USING GENERAL FUNCTION PROCEDURES IN APPLICATIONS Function procedures work a lot like subroutine; you can call them from elsewhere in the program. Unlike subroutine procedures, however, function procedures return values.
  22. 22. In Syntax • Private/Public are the optional Visual Basic keywords that define the scope of the function. • Function is the Visual Basic keyword that denotes the procedure is a function/ • FunctionName is the name that you assign to your function. • As is the Visual Basic keyword that denotes the procedure is a function. • DataType is the data type of the value that the function will return.
  23. 23. • ReturnValue is the value that you pass back from the function by assigning it top the function’s name (this is very important). • End Function is the Visual Basic keywords that denote the end of a code block. • You add a function to your project by using the same two methods that you use to add a subroutine. However, he advised that you have to manually add a little code when you add a function to your code by using the Add Procedure dialog.
  24. 24. • A function procedure always begins with the Function statement and ends with the End function statement. • A function is the same in every way as a subroutine except that the function returns a value. • All you do to call a function is to use the function procedure’s name inside an expression or statement. • If you ever need to exit a function procedure before the function’s normal termination, use the Exit function statement.
  25. 25. LOCATING FUNCTION PROCEDURES • Can be located in forms or modules. • They are created using exactly the same process described for Sub procedures. The only difference is you use the keyword FUNCTION.

×