SlideShare a Scribd company logo
Understanding Subroutines and
Functions in VB
What is/are Subroutines?
A subroutine has a name attributed with it, much
like a variable does. Unlike a variable, a
subroutine doesn't hold data. Instead, it holds
code.
For example, the Form_Load subroutine is
automatically called when a form loads. This is
where you have been inserting your test code
before. What you were actually doing was adding
code to this subroutine.
What is/are Functions?
Functions are basically the exact same as
subroutines, except that they return a value.
That means that the function itself has a
type, and the function will return a value to
the calling subroutine based on the code
that it contains.
A function is declared the exact same way as
a subroutine, except using the "Function"
keyword instead of "Sub".
The difference between a
Subs and a Functions
Sub does not produce a return value (one
that can be assigned directly to a variable)
whereas a
Function does produce a return value.
Both Subroutines and Functions can be
called with or without parameters.
Calling a Sub Without Parameters
To call a Sub, you use the
Call statement. Example,
the statement below calls
a Sub named "AddNum".
Call AddNum
This would cause VB to
execute the statements
in the Sub named
"AddEm" and then
return to the statement
following the Call.
Private Sub AddNum()
[statements]
End Sub
The keyword "Call" is optional, thus a
call can be made simply by coding the
name of the Sub:
AddNum ' same as "Call AddNum"
Example of Calling a Sub Without Parameters
Option Explicit ‘which requires that you declare every variable before you use it.
Dim mintNum1 As Integer
Dim mintNum2 As Integer
Dim mintSum As Integer
Private Sub cmdTryIt_Click()
mintNum1 = Val(InputBox("Enter first number:", "Add Program"))
mintNum2 = Val(InputBox("Enter second number:", "Add Program"))
AddNum ' Could also be coded as "Call AddNum()" with or without the ()
Print "The sum is "; mintSum
End Sub
Private Sub AddNum()
mintSum = mintNum1 + mintNum2
End Sub
Calling a Sub With Parameters
A Sub Procedure will be expecting one or more
parameters. This enhances the reusability and
flexibility of the Sub procedure. The full syntax for
a Sub procedure is:
[Private | Public] Sub SubName[(parameter list)]
[statements]
End Sub
where "parameter list" is a comma-separated
list of the parameter variables with their data
types (ex. var1 As datatype, var2 As datatype,
etc.).
Example: Private Sub AddNum(pintNum1 As Integer, pintNum2 As Integer, pintSum As Integer)
The CALL can be coded in one of two ways.
Call AddNum(intNum1, intNum2, intSum)
or
AddEm intNum1, intNum2, intSum
With the naming conventions use to start
the variable names of the parameters in
a Sub or Function header with the letter
"p" for "parameter
Example of Calling a Sub With Parameters
Private Sub cmdTryIt_Click()
' The variables can be declared at the local level
Dim intNum1 As Integer
Dim intNum2 As Integer
Dim intSum As Integer
intNum1 = Val(InputBox("Enter first number:", "Add Program"))
intNum2 = Val(InputBox("Enter second number:", "Add Program"))
AddNum intNum1, intNum2, intSum
' Could also be coded as:' Call AddNum(intNum1, intNum2, intSum)
Print "The sum is "; intSum
End Sub
Private Sub AddNum(pintNum1 As Integer, pintNum2 As Integer, pintSum As Integer)
pintSum = pintNum1 + pintNum2
End Sub
Calling a Function
To call a Function, the syntax is identical to an assignment statement that uses a
VB built-in function:
VariableName = FunctionName[(argument list)]
For example, if you made a function called "AddNum" that returned a value (like
the sum, for example), you could invoke the function as follows:
intSum = AddEm(intNum1, intNum2)
The above statement would cause the following to happen:
1. The procedure AddNum would be called, passing intNum1 and intNum2
2. The AddNum procedure would add the values of intNum1 and intNum2 to produce its
return value, the sum.
3. When the AddNum procedure exits, its return value would be stored in the variable
intSum
The format of the Function
Procedure
[Private | Public] Function FunctionName
[(parameter list)] [As datatype]
[statements]
FunctionName = value
[statements]
End Function
Example of Calling a Function
Option Explicit
. . .
Private Sub cmdTryIt_Click()
' The variables can be declared at the local level
Dim intNum1 As Integer
Dim intNum2 As Integer
Dim intSum As Integer
intNum1 = Val(InputBox("Enter first number:", "Add Program"))
intNum2 = Val(InputBox("Enter second number:", "Add Program"))
intSum = AddNum(intNum1, intNum2)
Print "The sum is "; intSum
End Sub
Private Function AddNum(pintNum1 As Integer,pintNum2 As
Integer) As Integer
AddNum= pintNum1 + pintNum2
End Function
Laboratory Hands-on Exercise:
Apply the Subroutines and Functions in a Simple
Math Operation (that can add, subtract, multiply
and divide)
Filenames: UsingSubs.vbp and UsingFunctions.vbp
In the last line of the program/code answer the
question and put as remarks to avoid errors. What
do you think the best way to determine which is
better?

More Related Content

What's hot

Chapter 1
Chapter 1Chapter 1
Chapter 1
gebrsh
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
VB Function and procedure
VB Function and procedureVB Function and procedure
VB Function and procedurepragya ratan
 
UNIX Operating System ppt
UNIX Operating System pptUNIX Operating System ppt
Data types and operators in vb
Data types and operators  in vbData types and operators  in vb
Data types and operators in vb
alldesign
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state management
priya Nithya
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
Kuppusamy P
 
Python GUI
Python GUIPython GUI
Python GUI
LusciousLarryDas
 
VB.NET:An introduction to Namespaces in .NET framework
VB.NET:An introduction to  Namespaces in .NET frameworkVB.NET:An introduction to  Namespaces in .NET framework
VB.NET:An introduction to Namespaces in .NET framework
Richa Handa
 
Operators
OperatorsOperators
Visual basic databases
Visual basic databasesVisual basic databases
Visual basic databases
Speed Cyber Cafe
 
Introduction to visual basic programming
Introduction to visual basic programmingIntroduction to visual basic programming
Introduction to visual basic programmingRoger Argarin
 
visual basic programming
visual basic programmingvisual basic programming
visual basic programming
sowndaryadharmaraj
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
Properties and indexers in C#
Properties and indexers in C#Properties and indexers in C#
Properties and indexers in C#
Hemant Chetwani
 

What's hot (20)

Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Visual Basic 6.0
Visual Basic 6.0Visual Basic 6.0
Visual Basic 6.0
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
VB Function and procedure
VB Function and procedureVB Function and procedure
VB Function and procedure
 
UNIX Operating System ppt
UNIX Operating System pptUNIX Operating System ppt
UNIX Operating System ppt
 
Data types and operators in vb
Data types and operators  in vbData types and operators  in vb
Data types and operators in vb
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state management
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Python GUI
Python GUIPython GUI
Python GUI
 
VB.NET:An introduction to Namespaces in .NET framework
VB.NET:An introduction to  Namespaces in .NET frameworkVB.NET:An introduction to  Namespaces in .NET framework
VB.NET:An introduction to Namespaces in .NET framework
 
Operators
OperatorsOperators
Operators
 
Visual basic databases
Visual basic databasesVisual basic databases
Visual basic databases
 
Introduction to visual basic programming
Introduction to visual basic programmingIntroduction to visual basic programming
Introduction to visual basic programming
 
visual basic programming
visual basic programmingvisual basic programming
visual basic programming
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Active x control
Active x controlActive x control
Active x control
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
 
Properties and indexers in C#
Properties and indexers in C#Properties and indexers in C#
Properties and indexers in C#
 

Similar to Understanding Subroutines and Functions in VB6

Unit iii vb_study_materials
Unit iii vb_study_materialsUnit iii vb_study_materials
Unit iii vb_study_materials
gayaramesh
 
Cpp functions
Cpp functionsCpp functions
Cpp functions
NabeelaNousheen
 
Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
ethiouniverse
 
Functions
FunctionsFunctions
Functions
zeeshan841
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
Muhammad Hammad Waseem
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
nikshaikh786
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
Arpit Meena
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
Mehul Desai
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
MikialeTesfamariam
 
procedures and arrays
procedures and arraysprocedures and arrays
procedures and arrays
DivyaR219113
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
ssuser823678
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
ssuser2076d9
 
functioninpython-1.pptx
functioninpython-1.pptxfunctioninpython-1.pptx
functioninpython-1.pptx
SulekhJangra
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
Ahmad Kamal
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce function
ARVIND PANDE
 

Similar to Understanding Subroutines and Functions in VB6 (20)

Unit iii vb_study_materials
Unit iii vb_study_materialsUnit iii vb_study_materials
Unit iii vb_study_materials
 
Cpp functions
Cpp functionsCpp functions
Cpp functions
 
Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
 
Functions
FunctionsFunctions
Functions
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
Function in c
Function in cFunction in c
Function in c
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
 
procedures and arrays
procedures and arraysprocedures and arrays
procedures and arrays
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
functioninpython-1.pptx
functioninpython-1.pptxfunctioninpython-1.pptx
functioninpython-1.pptx
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce function
 

More from Notre Dame of Midsayap College (16)

CC 101 Introduction to Computing/Intro Comp
CC 101 Introduction to Computing/Intro CompCC 101 Introduction to Computing/Intro Comp
CC 101 Introduction to Computing/Intro Comp
 
HTML for Education
HTML for EducationHTML for Education
HTML for Education
 
Creating Reports in VB6 using MS Access
Creating Reports in VB6 using MS AccessCreating Reports in VB6 using MS Access
Creating Reports in VB6 using MS Access
 
Is 21 -_databases
Is 21 -_databasesIs 21 -_databases
Is 21 -_databases
 
Arrays
ArraysArrays
Arrays
 
Educational technology final
Educational technology finalEducational technology final
Educational technology final
 
Systems Analysis and Design for BSA
Systems Analysis and Design for BSASystems Analysis and Design for BSA
Systems Analysis and Design for BSA
 
Stat topics
Stat topicsStat topics
Stat topics
 
Learning session on research
Learning session on researchLearning session on research
Learning session on research
 
CSE 312 web programming
CSE 312 web programmingCSE 312 web programming
CSE 312 web programming
 
Email Gmail
Email GmailEmail Gmail
Email Gmail
 
Introduction to Computer
Introduction to ComputerIntroduction to Computer
Introduction to Computer
 
What is Internet
What is InternetWhat is Internet
What is Internet
 
Cont ADO Data Control
Cont ADO Data ControlCont ADO Data Control
Cont ADO Data Control
 
Using ADO Data Control
Using ADO Data ControlUsing ADO Data Control
Using ADO Data Control
 
VB6 Using ADO Data Control
VB6 Using ADO Data ControlVB6 Using ADO Data Control
VB6 Using ADO Data Control
 

Recently uploaded

Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Delivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and TrainingDelivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and Training
AG2 Design
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Landownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptxLandownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptx
JezreelCabil2
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 

Recently uploaded (20)

Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Delivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and TrainingDelivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and Training
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Landownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptxLandownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptx
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 

Understanding Subroutines and Functions in VB6

  • 2. What is/are Subroutines? A subroutine has a name attributed with it, much like a variable does. Unlike a variable, a subroutine doesn't hold data. Instead, it holds code. For example, the Form_Load subroutine is automatically called when a form loads. This is where you have been inserting your test code before. What you were actually doing was adding code to this subroutine.
  • 3. What is/are Functions? Functions are basically the exact same as subroutines, except that they return a value. That means that the function itself has a type, and the function will return a value to the calling subroutine based on the code that it contains. A function is declared the exact same way as a subroutine, except using the "Function" keyword instead of "Sub".
  • 4. The difference between a Subs and a Functions Sub does not produce a return value (one that can be assigned directly to a variable) whereas a Function does produce a return value. Both Subroutines and Functions can be called with or without parameters.
  • 5. Calling a Sub Without Parameters To call a Sub, you use the Call statement. Example, the statement below calls a Sub named "AddNum". Call AddNum This would cause VB to execute the statements in the Sub named "AddEm" and then return to the statement following the Call. Private Sub AddNum() [statements] End Sub The keyword "Call" is optional, thus a call can be made simply by coding the name of the Sub: AddNum ' same as "Call AddNum"
  • 6. Example of Calling a Sub Without Parameters Option Explicit ‘which requires that you declare every variable before you use it. Dim mintNum1 As Integer Dim mintNum2 As Integer Dim mintSum As Integer Private Sub cmdTryIt_Click() mintNum1 = Val(InputBox("Enter first number:", "Add Program")) mintNum2 = Val(InputBox("Enter second number:", "Add Program")) AddNum ' Could also be coded as "Call AddNum()" with or without the () Print "The sum is "; mintSum End Sub Private Sub AddNum() mintSum = mintNum1 + mintNum2 End Sub
  • 7. Calling a Sub With Parameters A Sub Procedure will be expecting one or more parameters. This enhances the reusability and flexibility of the Sub procedure. The full syntax for a Sub procedure is: [Private | Public] Sub SubName[(parameter list)] [statements] End Sub where "parameter list" is a comma-separated list of the parameter variables with their data types (ex. var1 As datatype, var2 As datatype, etc.). Example: Private Sub AddNum(pintNum1 As Integer, pintNum2 As Integer, pintSum As Integer) The CALL can be coded in one of two ways. Call AddNum(intNum1, intNum2, intSum) or AddEm intNum1, intNum2, intSum With the naming conventions use to start the variable names of the parameters in a Sub or Function header with the letter "p" for "parameter
  • 8. Example of Calling a Sub With Parameters Private Sub cmdTryIt_Click() ' The variables can be declared at the local level Dim intNum1 As Integer Dim intNum2 As Integer Dim intSum As Integer intNum1 = Val(InputBox("Enter first number:", "Add Program")) intNum2 = Val(InputBox("Enter second number:", "Add Program")) AddNum intNum1, intNum2, intSum ' Could also be coded as:' Call AddNum(intNum1, intNum2, intSum) Print "The sum is "; intSum End Sub Private Sub AddNum(pintNum1 As Integer, pintNum2 As Integer, pintSum As Integer) pintSum = pintNum1 + pintNum2 End Sub
  • 9. Calling a Function To call a Function, the syntax is identical to an assignment statement that uses a VB built-in function: VariableName = FunctionName[(argument list)] For example, if you made a function called "AddNum" that returned a value (like the sum, for example), you could invoke the function as follows: intSum = AddEm(intNum1, intNum2) The above statement would cause the following to happen: 1. The procedure AddNum would be called, passing intNum1 and intNum2 2. The AddNum procedure would add the values of intNum1 and intNum2 to produce its return value, the sum. 3. When the AddNum procedure exits, its return value would be stored in the variable intSum
  • 10. The format of the Function Procedure [Private | Public] Function FunctionName [(parameter list)] [As datatype] [statements] FunctionName = value [statements] End Function
  • 11. Example of Calling a Function Option Explicit . . . Private Sub cmdTryIt_Click() ' The variables can be declared at the local level Dim intNum1 As Integer Dim intNum2 As Integer Dim intSum As Integer intNum1 = Val(InputBox("Enter first number:", "Add Program")) intNum2 = Val(InputBox("Enter second number:", "Add Program")) intSum = AddNum(intNum1, intNum2) Print "The sum is "; intSum End Sub Private Function AddNum(pintNum1 As Integer,pintNum2 As Integer) As Integer AddNum= pintNum1 + pintNum2 End Function
  • 12. Laboratory Hands-on Exercise: Apply the Subroutines and Functions in a Simple Math Operation (that can add, subtract, multiply and divide) Filenames: UsingSubs.vbp and UsingFunctions.vbp In the last line of the program/code answer the question and put as remarks to avoid errors. What do you think the best way to determine which is better?