Monday, August 17, 2020

Procedures in Visual Basic:

Procedures in Visual Basic:

Procedure is a sequence of statements executed as a unit.  Procedure normally simplifies programming by breaking the program into smaller logic components. Procedures are useful for condense repeating or sharing the task.

Benefits of programming with procedure

  1. The repetition of the code is avoided.
  2. If program size is reduced, which consume less memory and result in faster program execution?
  3. Maintenance of program (applying changes) become is   easier.
  4. The debugging of the program( pressing and eliminating errors) also become easier
  5. The program become easy to understand

Types of procedure

  1.  General procedure
  2.  Event procedure

General procedure: A sub procedure is a block of code which is not associated with an event and is to be explicitly called. By breaking the code into sub procedures, it becomes easier to find or modify the code in your application.

Event procedure: In an event procedure the code gets automatically trigger when an object in Visual Basic recognizes that any event has occurred like a click or a double click.  The name of the procedure established and association between the object and the code, event procedure is attach to forms and controls

All event procedures used the same general syntax.

Private sub  controlname_ eventname( arguments)

            Statements

End sub

Creating a General procedure

You can create general procedures in two ways.  One way by manually going to the general declaration section and typing through to the menu

To create a new general procedure manually type a procedure heading in the code window and press enter. The procedure heading can be as simple as sub or function followed by name

e.g. 

Sub Show()

Function Fact()

When you press enter Visual Basic responds by completing the template for the procedure.

Sub show()

End sub

Function Fact()

End  Function

Then you can type code to complete procedure

Sub Show()

Msgbox “hello”

End Sub

Creating a General procedure through the menu:

To add a procedure with the help of menu from the tool menu to display the following add procedure dialogue box.

Creating General Procedure

Type the name of the procedure in the name box; select the type of procedure and scope of the click ok button.

This would automatically enter the sub and end sub statements within the declaration section with the specified name

Calling a procedure

There are two ways to call a sub procedure:

Both this statement call a sub named add,

Call Show()

Or

Show()

The procedure accepting the arguments:

Arguments can be passed to the procedure which is to be mentioned in the pair of parentheses following the procedure name.

The values for the procedure or to be passed while calling the particular procedure.


Public Sub Add( a as integer, b  as integer)

Dim c as integer

 c= a + b

Msgbox “ The sum of two arguments =”&c

End sub


Private sub command1_click ()

Dim num1,num2 as integer

num1 = Inputbox(“ Enter the first number”)

num2 = Inputbox(“ Enter the Second number”)

call Add( num1 num2)

End sub

Argument ByValue and ByRef

By default Visual Basic procedure accept the argument by reference.  The user can also pass argument by Value using ByVal keyword before the argument name.

Public Sub Add( a is integer)

End Sub

The procedure Add() accepts the augment of type integer by reference.  To pass an argument by value to the procedure

Private Sub Add( ByVal a as integer)

End sub

Difference between passing and argument By Value and By Reference:

By Value

 By Reference

A separate copy of that variable is passed as an argument

A reference of a variable is pass to the procedure

The variable value in the calling and the called procedure is stored at separate location

The variable value in the calling and the called procedure is stored at same location

Any changes made in the called procedure are not updated in calling procedure

Any changes made in called procedure are updated in the calling procedure

Consumption of memory space is more

Consumption of memory space is less


The optional arguments:

When a procedure is called then it is must to pass the list of arguments it is going to accept.

e,g The add procedure is designed to accept two integer as the augments and if it is called as

Call  add(10)

Or 

Call add()

It will result in an error.  The argument can be made as an optional argument by preceding its declaration with the keyword optional.  If the value is not pass for an optional argument then it will not result in an error.

Public Sub Add(a As Integer, Optional b As Integer)

MsgBox "Sum of two numbers is" & a + b

End Sub

Providing the default value for the optional argument:

If the value is not passed to the optional argument then its value is assumed to be zero.  If some default value is to be assigned to the function then the default value can be mentioned in the following format.

Public sub pro_name(arg_name1 as data_type, Optional arg_name2 as data_type=default _value)

Public Sub Add(a As Integer, Optional b As Integer=20)

MsgBox "Sum of two numbers is" & a + b

End Sub

Private sub Command1_click()

Call add(10)

End sub

If the value is not passed to the second argument then it will consider the default value for the optional argument b. 

The scope of the Procedure:

The scope of the procedure may be declared either as a private or public. If the scope of the procedure is declared as a private then that particular procedure can be called from the same form

If the scope of the procedure is declared as a public it can be called from the same form as well as from the different forms. 

Add one more forms to the project

PROJECT-> ADD FORM

If the following procedure is written in form2 and its scope is declared as public then it can be called from form 1 as well as form 2.

Public  Sub  Add( a as integer,  b as integer)

Dim c as Integer

Msgbox “The sum of two numberis”&c

End sub

Executing procedure on Different Form

To call the add procedure present on form2 from the code in form1. Write the following code in the code window of the form.

Private Sub Command1_Click()

Call Form2.Add(10,20)

End Sub


No comments:

Post a Comment