SlideShare una empresa de Scribd logo
1 de 25
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 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.
• 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.
•
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
Kinds of Procedures
Event-procedures-
executes as events occur
Procedure is really
not an executable
procedure in the way
that event procedures
execute
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
 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
*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
BENEFITS FROM USING SUB
PROCEDURE ROUTINES
Eliminates redundant
codes
Codes will be reusable
Program code will be
simpler to maintain
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
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.
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
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.
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
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
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
Private Sub Form_Load()
Text 1 = “ “
Text 2 = “ “
Text 3 = “ “
End Sub
Public Sub USPesoConvert ()
Text 3 = Text1 & Text2
End Sub
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.
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.
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.
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.
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.
• 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.
• 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.
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.

Más contenido relacionado

La actualidad más candente

visual basic v6 introduction
visual basic v6 introductionvisual basic v6 introduction
visual basic v6 introduction
bloodyedge03
 
Unit 1 introduction to visual basic programming
Unit 1 introduction to visual basic programmingUnit 1 introduction to visual basic programming
Unit 1 introduction to visual basic programming
Abha Damani
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
abex98
 
0106 debugging
0106 debugging0106 debugging
0106 debugging
vkyecc1
 

La actualidad más candente (20)

Monolithic and Procedural Programming
Monolithic and Procedural ProgrammingMonolithic and Procedural Programming
Monolithic and Procedural Programming
 
DIG1108C Lesson 7 Fall 2014
DIG1108C Lesson 7 Fall 2014DIG1108C Lesson 7 Fall 2014
DIG1108C Lesson 7 Fall 2014
 
visual basic v6 introduction
visual basic v6 introductionvisual basic v6 introduction
visual basic v6 introduction
 
Visual basic
Visual basicVisual basic
Visual basic
 
Unit 1 introduction to visual basic programming
Unit 1 introduction to visual basic programmingUnit 1 introduction to visual basic programming
Unit 1 introduction to visual basic programming
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Testing
TestingTesting
Testing
 
Vb basics
Vb basicsVb basics
Vb basics
 
Visual basic
Visual basicVisual basic
Visual basic
 
Coding
CodingCoding
Coding
 
Basic of compiler
Basic of compiler Basic of compiler
Basic of compiler
 
Logic Formulation 1
Logic Formulation 1Logic Formulation 1
Logic Formulation 1
 
Week10 final
Week10 finalWeek10 final
Week10 final
 
Decision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, StringsDecision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, Strings
 
0106 debugging
0106 debugging0106 debugging
0106 debugging
 
Algorithmic problem sloving
Algorithmic problem slovingAlgorithmic problem sloving
Algorithmic problem sloving
 
Automation testing
Automation testingAutomation testing
Automation testing
 
Problem Solving Techniques and Introduction to C
Problem Solving Techniques and Introduction to CProblem Solving Techniques and Introduction to C
Problem Solving Techniques and Introduction to C
 
Program Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State UniversityProgram Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State University
 
Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer   Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer
 

Similar a Using general sub procedures

Modularisation techniques new
Modularisation techniques newModularisation techniques new
Modularisation techniques new
Jeet Thombare
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
Ashesh R
 

Similar a Using general sub procedures (20)

AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1
 
Sub-programs
Sub-programsSub-programs
Sub-programs
 
Presentation of computer
Presentation of computerPresentation of computer
Presentation of computer
 
Introduction to problem solving in C
Introduction to problem solving in CIntroduction to problem solving in C
Introduction to problem solving in C
 
Introduction to Visual Basic 6.0
Introduction to Visual Basic 6.0Introduction to Visual Basic 6.0
Introduction to Visual Basic 6.0
 
MPP-UPNVJ
MPP-UPNVJMPP-UPNVJ
MPP-UPNVJ
 
C programming
C programmingC programming
C programming
 
Modularisation techniques new
Modularisation techniques newModularisation techniques new
Modularisation techniques new
 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing Techniques
 
COM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptxCOM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptx
 
Visusual basic
Visusual basicVisusual basic
Visusual basic
 
Ms vb
Ms vbMs vb
Ms vb
 
Modular programming
Modular programmingModular programming
Modular programming
 
Chapter 2(1)
Chapter 2(1)Chapter 2(1)
Chapter 2(1)
 
PCCF UNIT 1.pptx
PCCF UNIT 1.pptxPCCF UNIT 1.pptx
PCCF UNIT 1.pptx
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
 
Algorithm to programs.pptx
Algorithm to programs.pptxAlgorithm to programs.pptx
Algorithm to programs.pptx
 
Lecture 2.pptx
Lecture 2.pptxLecture 2.pptx
Lecture 2.pptx
 
Coding
CodingCoding
Coding
 
Algorithm-Introduction ,Characterestics & Control Structures.pdf
Algorithm-Introduction ,Characterestics & Control Structures.pdfAlgorithm-Introduction ,Characterestics & Control Structures.pdf
Algorithm-Introduction ,Characterestics & Control Structures.pdf
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Último (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

Using general sub procedures

  • 1. Using General Sub Procedures/Routines and functions in Applications
  • 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. • 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. 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. Kinds of Procedures Event-procedures- executes as events occur Procedure is really not an executable procedure in the way that event procedures execute
  • 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.  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. *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. BENEFITS FROM USING SUB PROCEDURE ROUTINES Eliminates redundant codes Codes will be reusable Program code will be simpler to maintain
  • 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. 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. TYPING THE SUBROUTINE IN THE CODE WINDOW 1. Go to the Code Window. 2.Type Sub GenAverage. 3.Press Enter key.
  • 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. 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. 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. 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. Private Sub Form_Load() Text 1 = “ “ Text 2 = “ “ Text 3 = “ “ End Sub Public Sub USPesoConvert () Text 3 = Text1 & Text2 End Sub
  • 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. 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. 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. 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. 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. • 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. • 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. 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.