Presentation is loading. Please wait.

Presentation is loading. Please wait.

Why to Create a Procedure

Similar presentations


Presentation on theme: "Why to Create a Procedure"— Presentation transcript:

1 POWERPOINT PRESENTATION ON PROCEDURE, FUNCTION & MODULE IN VISUAL BASIC

2 Why to Create a Procedure
Procedures A procedure is a block of Visual Basic statements enclosed by a declaration statement (Sub, Function,Get, Set) and a matching End declaration. All executable statements in Visual Basic must be within some procedure. Why to Create a Procedure It allow you to break your program into discrete logical units, each of which can be debug more easily than an entire program without procedures. It can also use as a building block for other programs. If you have code that performs the same task in different places, you can write the task once as a procedure and then call it from different places in your code.

3 Types of Procedures 1. Sub Procedures  A Sub procedure is a series of Visual Basic statements enclosed by the Sub and End Sub statements. The Sub procedure performs a task and then returns control to the calling code, but it does not return a value to the calling code. It is Public by default, which means you can call it from anywhere in your application. It can take arguments, which are passed to it by the calling code. Types of Sub Procedure They are of 2 types: General ProcedureIt tells the application how to perform a specific task. It must be invoked by an application. Event ProcedureEvent procedures execute in response to an event raised by user action or by an occurrence in a program such as click event of an object.

4 Parts of a Function Procedure
A Function Procedure has these parts: Visibility Public, Friend or Private Procedure Type Sub, Function, Property Let, Property Get, Property Set Name Anything you like as long as it starts with a letter and contains only letters numbers and underscores. Argument List a list of the items of data that the procedure needs, Return Type if the type is Function or Property Get this tells the compiler what kind of thing will be returned, for instance, Double or String. Body all the statements that do the work.

5 How to: Create a Procedure
You enclose a procedure between a starting declaration statement (Sub or Function) and an ending declaration statement (End Sub or End Function). All the procedure's code lies between these statements. Declaration Syntax of General Procedure [ modifiers ] Sub subname [( parameterlist )] ' Statements of the Sub procedure. End Sub Declaration Syntax of Event Procedure [ modifiers]Sub objectname_eventname [(parameterlist )] The modifiers can specify access level (Public, Private)

6 Parameters In most cases, a procedure needs to operate on different data each time you call it. You can pass this information to the procedure as part of the procedure call. The procedure defines zero or more parameters, each of which represents a value it expects you to pass to it. By default, all parameters to a VB procedure are passed by reference. When you pass by value, you'll get saved from subtle bugs. Passing by value (ByVal): The procedure gets a copy of the actual value of the argument. Function MultiplyByTwo(ByVal x As Integer) As Integer Passing by reference (ByRef): The procedure gets access to the argument's actual memory address. Function MultiplyByTwo(ByRef x As Integer) As Integer When a ByRef parameter is used, the value of the passed argument can be permanently changed by the procedure.

7 Parameter Declaration
You declare each procedure parameter similarly to how you declare a variable, specifying the parameter name and data type. The syntax for each parameter in the parameter list is as follows: [Optional] [ByVal|ByRef] parametername As datatype If the parameter is optional, you must also supply a default value as part of its declaration. The syntax for specifying a default value is as follows: Optional [ByVal | ByRef] parametername As datatype = defaultval Calling Procedure You invoke a Sub procedure explicitly with a stand-alone calling statement , Call keyword which is optional. The syntax for a call to a Sub procedure [Call] subname [( argumentlist )]

8 Example of Sub Procedure
Private Sub sum(a,b) c=a+b Print c End Sub Private Sub command1_click() Call sum(10,20) Or Sum(10,20)

9 Declaration Syntax of Function Procedure
2. Function Procedures A Function procedure is a series of Visual Basic statements enclosed by the Function and End Function statements. The Function procedure performs a task and then returns control to the calling code. When it returns control, it also returns a value to the calling code. Declaration Syntax of Function Procedure The syntax for declaring a Function procedure is as follows: [modifiers] Function funcname [(paramlist)] As returntype ' Statements of the Function procedure. functionname = expression Return expression End Function Data TypeEvery Function procedure has a data type. This data type is specified by the As clause in the Function statement, and it determines the data type of the value the function returns to the calling code. Returns control to the code that called a Function, Sub, Get, Set, or Operator procedure

10 Calling Function Procedure
You invoke a Function procedure by including its name and arguments either on the right side of an assignment statement or in an expression. You must provide values for all arguments that are not optional, and you must enclose the argument list in parentheses. If no arguments are supplied, you can optionally omit the parentheses. The syntax for a call to a Function procedure : [ modifiers]Sub objectname_eventname [(parameterlist )] varname = functionname [( argumentlist )] End Sub

11 Example of Function Procedure
Public Function Sum(ByRef a As Double, ByRef b As Double) As Double Sum = a + b Return sum End Function Private Sub command1_click() csum= sum(10,20) Print csum End Sub

12 3. Property Procedures  A property procedure is a series of Visual Basic statements that manipulate a custom property on a module, class, or structure. Visual Basic provides for the following property procedures: A Get procedure returns the value of a property. It is called when you access the property in an expression. A Set procedure sets a property to a value, including an object reference. It is called when you assign a value to the property. You usually define property procedures in pairs, using the Get and Set statements, but you can define either procedure alone if the property is read-only (Get Statement) or write-only (Set Statement (Visual Basic)).

13 Syntax of Property Procedure
The syntax for declaring a property and its procedures : [Default] [modifiers] Property propertyname[(parameterlist)] As datatype [accesslevel] Get ' Statements of the Get procedure. ' The following statement returns expression as the property's value. Return expression End Get [accesslevel] Set[(ByVal newvalue As datatype)] ' Statements of the Set procedure. ' The following statement assigns newvalue as the property's value. lvalue = newvalue End Set End Property

14 Example of Property Procedure
Dim firstName, lastName As String Property fullName() As String Get If lastName = "" Then Return firstName Else Return firstName & " " & lastName End If End Get Set(ByVal Value As String) Dim space As Integer If space < 0 Then firstName = Value lastName = "" else firstName = Value.Substring(0, space) lastName = Value.Substring(space + 1) End Set End Property

15 Modules Modules are the code container. Visual Basic supports 3 types of modules: Form Module : It consists of small piece of code called Procedures. It includes the description, settings and properties related to a single form. Its extension is .frm.

16 2. Standard Module: It's better to separate all your Functions and Subs and put them somewhere else - in something called a Module. That way, we can use them in other projects. It does not have a GUI and contains only code. It is a container for procedures and declarations commonly used by within the application. They contain global and ,module level declarations of variables. Its extension is .bas.

17 How to add a standard module?
start a new project. Add a button to you new form. To add a Module in project, click Project from the menubar. From the from down menu, click on "Add Module": From the from down menu, click on "Add Module":You'll should see a blank window, with this code in it:

18 Example of Standard Module
FORM CALLING OF MODULE

19 3. Class Module They are the foundation of Object-oriented programming in Visual Basic. It is used to create new objects. They have no visible user interface. Its extension is .cls.

20 How to add a class module?
To add a Class Module in project, click Project from the menubar. From the from down menu, click on "Add ClassModule": From the Add Class Module window, click on “Open":You'll should see a blank window, with this code in it:


Download ppt "Why to Create a Procedure"

Similar presentations


Ads by Google