Вы находитесь на странице: 1из 4

Front End Design Tools

BCA III(E)

Lecture No.16

16. PROCEDURES
Procedures are bits of code that can be used to respond to events, or procedures can be written in general to perform some action. Procedures do not return values to the calling routines. Procedures are identified by the keyword Sub in their declaration. 16.1. Naming the procedure Rules that are followed for naming the variables are same for naming the procedures and functions. They must begin with a letter followed by a combination of letters and underscore. Procedure name may be upto 255 characters long. 16.2. Events/Subprocedures Event procedures are always declared with the keyword Sub, which stands for Subroutine. A subroutine is a set of statements executed by calling a name where these statements are stored. No value is returned from a sub procedure. The difference between a subprocedure and an event procedure is that Visual Basic defines the name of the event and what arguments are passed into the procedure. Procedures can be given any userdefined name and any number of arguments as well as user defined arguments can be passed. 16.3. Scope of a Variable A procedure can be either public or private. A private procedure can be called only by other procedures within the same form, standard module or a class module. A public procedure can be called from any other procedure outside the module. For code in a standard module, a public procedure is global to the entire application. 16.4. Creating a procedure Code editor windowselect Tools Add Procedure to display a dialog box that prompts to name the procedure.

Ms. Ramandeep Kaur

Front End Design Tools

BCA III(E)

Lecture No.16

Enter the procedure nameSelect Sub/ Function Select Public/Private Scope of procedure click on OK. Or In the General Declaration section create a sub. [Private/Public] Sub ProcedureName() End Sub 16.5. Procedures with private scope

Private Sub display() Dim strname As String strname = Text1.Text + Text2.Text MsgBox strname End Sub Private Sub cmdShow_Click() 'display Call display End Sub

Ms. Ramandeep Kaur

Front End Design Tools

BCA III(E)

Lecture No.16

16.6. Procedures with Public Scope Public Sub EmpInfo(strname As String) MsgBox strname End Sub Private Sub cmdShow_Click() Dim name As String name = Text1.Text + Text2.Text Call EmpInfo(name) End Sub 16.7. Passing Parameters and Arguments to procedure When a value is passed to a procedure, it is called a Parameter. In the declaration of that procedure, the values coming in are called arguments.

Sub Sum(X As Integer, Y As Integer) Dim intsum As Integer intsum = X + Y MsgBox intsum

Ms. Ramandeep Kaur

Front End Design Tools

BCA III(E)
End Sub Private Sub cmdSum_Click() Dim n1 As Integer, n2 As Integer n1 = Val(Text1.Text) n2 = Val(Text2.Text) Call Sum(n1, n2) End Sub

Lecture No.16

Ms. Ramandeep Kaur

Вам также может понравиться