VB Modules and Procedures
Q Compare and contrast between Function vs. Procedure.
Ans. A Function is a set of statements that performs a specific task within a program and return either a number or a string value. Visual Basic has a number of functions. A function begins with Function statement and ends with an End Function statement. A function may also accept arguments. These arguments are enclosed in parenthesis ( ) following the function name.
A Procedure is a sequence of code statements that performs a specific task within a program but return no explicit value. These statements are executed as a unit. A Sub Procedure begins with a Sub statement and ends with an End Sub statement. Sub procedure performs actions but do not return any values whereas function procedures perform action and return values.
A Function or Procedure tells an application what and how to perform a specific task when an event is generated. Once the function or procedure is defined it must be explicitly invoked by the application. An event procedure remains idle until called upon to respond to events caused by the user. You can place a function or procedure in a form module, standard module or class module.
Q. Write the scope rule to use a variable in modules.
Ans. To make a module-level variable available to other modules, use the Public keyword to declare the variable. The values in public variables are available to all procedures in your application. Like all module-level variables, public variables are declared in the Declarations section at the top of the module. For example,
Public intTemp As Integer
Q. How do the functions help the programmer to write efficient programs?
Ans. A preprogrammed calculation that can be carried out on request from any point in a program. Because a function takes in one or more arguments and returns a single value, it can be embedded in an expression. Functions contain a pair of parentheses that distinguish them from commands. For example. SurnN() is a function. Writing the name of the function instead of writing entire code can use function. In this way. function helps programmer to write effieient programs.
Q. Write the declaration for each of the following:
(a) Monetary variable crdLimit that is accessible to all form and code modules.
(b) Single variable anyVal that is to retain its value between calls to the procedure in which it is defined.
(c) Integer number whose scope should be restricted to the form module in which it is defined.

Ans. (a) Public crdLimitAs Currency ‘in a code module
(b) Static anyVal As Single ‘local declaration
(c) Dim number As Integer ‘in a form module
Q. For each of the following, state the scope (either local scope, module scope or Public scope) of each of
the following elements
(a) Variable zdeclared in the general declaration of SUM.BAS with keyword Public.
(b) Variable x declared inside procedure Square.
(c) Variable y declared in the general declaration SUM I .BAS with keywoard Dim.
(a Variable ThisYear declared in the general declaration of form module frmYear. fim with keyword Dim.
(e) Function procedure CalcSum in the code module NUM.BAS.
(f) Public Sub procedure SquareNurn in the code module SQUARE.BAS.
(g) Static variable ctr declared in procedure GCD.
(h) Variable wide declared in event procedure Form Load.
(i) Variable animal declared in Private procedure West in code module JUNGLE.BAS.

Aim. (a) Public scope. (b) local scope. (c) module scope
(d) module scope (e) module scope Public scope
(g) local scope (h) local scope (0 local scope

Q. Write the procedure header for each of the following (assume that all headers are preceded by keyword
Private).
(a) Hypotenuse takes two Double arguments, stri and .ctr2 and return a Double result.
(b) Smallest takes tow Singles, x, y and returns a Currency result,
(c) Instructions does not receive any arguments and does not return a value.
(a) IntibSingle takes an Integer argument, number and returns a Single result. The procedure header should also indicate the number is passed call-by-value.
e) PrintMessage takes a String arugment, message and does not return a value.

Find the error(s) in each of the following program segments. If an error can be corrected, explain how the error can be corrected:
(a) Private Procedure MaxN(anyVal As Currency) As Variant
Dim anyVal As Single
anyVal = anyVal * 10
Print “any Val is” & anyVal
MaxN = any Val
End Procedure

(b) Enum Tmp
anyFacti = “Beginner’s Guide to VB6”
anyFact2 = -1000
End Trnp

(c) Private Function SumNum(valA As Long, valB As Long) As Long
Enum Tmp
Temp= 100
Ptemp = 200
End Enum
SumNum = valA + vaIB + Temp + Ptemp
End Function
(a) Private Static Function Square(number As Long) As Long
Dim number As Long, temp As Long
temp = number ^ 2
Function Square = temp
End Function

(e) Private Function Results(x&, y As Long) As Long
Print “The sum is” & x + y
Results x + y
End Function

(f) Public Sub Ctr(number As Single)
LstVal.Addltem number & ““
Exit Sub
End Sub

(g) CallABCl,2,3

(h) Private Sub Find(anyVal As lnteger= 1) As String
Dim vaIX As Integer
For vaIX = Ito any Val
Print “There is lots of problem!!!”
Next vaIX

Find= “There are still problems…..”

End sub