visual basic cda college limassol campus com123 visual programming 1 semester b lecture:pelekanou...

Download Visual Basic CDA College Limassol Campus COM123 Visual Programming 1 Semester B Lecture:Pelekanou Olga Week 5: Useful Functions and Procedures

If you can't read please download the document

Upload: darcy-shelton

Post on 06-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

3 Creating procedures What Are Procedures?  Procedures are the executable code statements in a program, enclosed by a declaration statement and an End statement  Three types: Sub procedures (including event Sub procedures) Function procedures Property procedures  Enable code reuse  Declared as public by default

TRANSCRIPT

Visual Basic CDA College Limassol Campus COM123 Visual Programming 1 Semester B Lecture:Pelekanou Olga Week 5: Useful Functions and Procedures Working with Procedures Creating Procedures What Are Procedures? How to Create Sub Procedures How to Create Function Procedures How to Declare Arguments in Procedures How to Use Optional Arguments Code Reusability Using Procedures Creating and using Procedures 3 Creating procedures What Are Procedures? Procedures are the executable code statements in a program, enclosed by a declaration statement and an End statement Three types: Sub procedures (including event Sub procedures) Function procedures Property procedures Enable code reuse Declared as public by default 4 Definition Procedures are the executable code statements in a program. The statements in a procedure are enclosed by a declaration statement and an End statement. Types of procedures There are three types of procedures in Microsoft Visual Basic.NET: Sub procedures, Function procedures, and Property procedures. Sub procedures perform actions but do not return a value to the calling procedure. Event handlers are Sub procedures that are executed in response to an event. Function procedures can return a value to the calling procedure. The MessageBox.Show statement is an example of a function. Property procedures return and assign values of properties of classes, structures, or modules. 5 Using procedures A procedure can be invoked, or called, from another procedure. When one procedure calls another procedure, control is transferred to the second procedure. When the code in the second procedure is finished running, it returns control to the procedure that called it. Because of this functionality, procedures are useful for performing repeated or shared tasks. Instead of writing the same code more than once, you can write a procedure and call it from various points in your application or from other applications. 6 Procedure accessibility You use an access modifier to define the accessibility of the procedures you write - that is, the permission for other code to call the procedure. If you do not specify an access modifier, procedures are declared as public by default. The table below shows the accessibility options for declaring a procedure within a module. Access modifier Description PublicNo restrictions on access FriendAccessible from within the program that contains the declaration and from anywhere else in the same ssembly PrivateAccessible only in the module that contains the declaration How to Create Sub Procedures Definition A Sub procedure is a series of Visual Basic statements enclosed by the Sub and End Sub statements. Sub procedures perform actions but do not return a value to the calling procedure. Syntax to create a Sub procedure Use the following syntax to create a Sub procedure: Example of a sub procedure The following code creates a Sub procedure Sub AboutHelp that uses a message box to display a product name and version number: How to Create Function Procedures Definition A Function procedure is a series of Visual Basic statements enclosed by the Function and End Function statements. Function procedures are similar to Sub procedures, but functions can return a value to the calling program. Syntax to create a Function procedure Use the following syntax to create a Function procedure: Example of a Function procedure The following code creates a function named Square that returns the square of an integer: Returning Values The value a Function procedure sends back to the calling program is called its return value. The function returns the value in one of two ways: It assigns a value to its own function name in one or more statements of the procedure. Control is not returned to the calling program until an Exit Function or End Function statement is executed. The Exit Function statement causes an immediate exit from a Function procedure. Any number of Exit Function statements can appear anywhere in the procedure. It uses a Return statement to specify the return value, and returns control immediately to the calling program. Returning Values The advantage of assigning the return value to the function name is that control does not return from the function until the program encounters an Exit Function or End Function statement. This allows you to assign a preliminary value and adjust it later, if necessary. How to Declare Arguments in Procedures Introduction A procedure that performs repeated or shared tasks uses different information for each call. This information might consist of variables, constants, and expressions that are passed to the procedure by the calling procedure. Each value passed to a procedure is called an argument.