You are on page 1of 9

Chapter - 6

Procedures and Structures


Procedures
• Compartmentalization of code data can be
done with the help of procedures
• The events in one pgm will not affect other
pgms
• It makes debugging, testing and maintanance
easier
Procedure Types
• Subroutine Procedures
– Performs any actions but will not return values
– Called as ‘subs’
• Function Procedures
– Performs any action and also returns values
• Property Procedures
– Mainly used in Objects
– They also assign and return values
– Eg: Font, color, open, save Dialog boxes used in the
pgms
Functions ….
• Types
– Library Fns
• Sin(), cos(), pow(), etc.,
– User-Defined Fns
• Created by users on their own
– Syntax:
• [accessibility] Function Function_Name[{arg. list}] As datatype
• Eg : Public Function factorial(ByVal x As Integer) As Integer
[accessibility] Function Function_Name[{arg. list}] As datatype

• Accessibility :
– Private, Public, Protected, Friend, Protected Friend
• Return :
– Syntax : Return(expression)
– Eg : Return (f)
– By default value will return with the help of function
name itselt
– It can also be returned by the user
• Advantages :
– Causes immediate exit from the fn
– Returns the needed value to the fn
Calling Fns…
• Call by Value :
– Values passed to the fns are passed to fns as
arguments
– Public Function factorial(ByVal x As Integer) As Integer
• Call by Reference
– Values are passed as references and so if the
values gets affected inside the fn it will reflect in
the main calling fn
– Public Function factorial(ByRef x As Integer) As Integer
Functions Eg Pgm…
Coding…
• Procedure Button Sub swap(ByVal x As Integer, ByVal
Click: y As Integer) ' procedure call
' local variable Dim temp As Integer
definition temp = x
a = TextBox1.Text x=y
b = TextBox2.Text y = temp
' calling a procedure ' ' Procedure cant return values
swap(a, b) a=x
TextBox1.Text = a b=y
TextBox2.Text = b End Sub
Coding…
• Sum(call by value) button Public Function sumfn(ByVal x As Integer, ByVal y
As Integer) As Integer ' call by value
Dim temp As Integer
MsgBox(sumfn(TextBox1. temp = x + y
Text, TextBox2.Text)) Return temp
End Function

Public Function sumfnref(ByRef x As Integer,


• Sumfnref (call by Refere) ByRef y As Integer) As Integer ' call by reference
button
sumfnref = x + y
x = x + 100
MsgBox(sumfnref(TextBox1. y = y + 100
Text, TextBox2.Text)) Return sumfnref
End Function

You might also like