Sie sind auf Seite 1von 19

Procedures

By the end of this chapter, student will be able to:

Declare the (Sub) procedure.


Call the (Sub) procedure.
Use (Parameters) when declaring a (Sub)
procedure.
Declare a Function.
Call a Function.
Differentiate between a Sub Procedure and a
Function.
List the reasons for using Sub Procedures or
Functions.
Differentiate between variable, constant, and
Function.
Write three of the names of some Predefined
Functions

Chapter Four
Data

Procedures

Dear Student, when adding a new form window, it creates a new (Class) with the
same form name .Within the scope of this (Class), we can declare Event
Procedures as well as variables and constants, that can be declared either within
the scope of the (Event procedures) or within the (Class), also we can declare
what so-called (Block construct), such as (If ... then) statement, and (For ... next)
statement, and others.

Figure (4-1) the Elements declared in the (Code Window)


Note that the user interface will appear as shown in figure (4-3)

Figure (4-2) user interface

(84)

Chapter Four
Data

Procedures

In Figure (4-1) the following elements are declared


1 - A Class of name (Form1).
2 - Variables of names (total, i).
3 - Event procedures of names (Button1_Click, Button2_Click).
You can also declare what so-called (Procedures); this declaration is done only
once and then, you call the procedure many times from anywhere in your
program; which avoids code duplication in places where procedures are called.

Procedures are set of programming statements or units of code.


Procedures must be called by their names, calling a procedure causes
the program to execute procedure's statements or code.

There are two types of procedures in Visual Basic .NET: Sub procedures and
Functions procedures. Sub procedures do not return a value, while Functions
return a value.

You can declare a Sub procedure in a class; if we had a code that will be
repeated in more than one place in this Class; as well as for the organization of
this code, and so it will be easy to read and understand. And then modify it if
necessary.

Sub Name(Parameters)
Code

End Sub

The declaration of a Sub procedure has the following Format:


1- A Sub procedure begins with a (Sub) statement.

(85)

Chapter Four
Data

Procedures

2- A (Name) follows the word Sub; it is the name of the procedure.


3- A set of parenthesis ( ) follows the name; in between the parenthesis is an
optional Parameter list. A parameter is a special variable that receives a
value being passed into a procedure, when it is called.
4- A Code or set of commands and instructions will be executed when calling
the (Sub) procedure.
5- An End sub

The Code written within each event procedure for (Button1_Click) and
(Button2_Click) is repetitive ; except for the starting value written in
each repetition ;where the value in the ( )button starts by (2), and
in the ( )button starts by (1) as shown in figure (4-1).

Dear student, use the Sub procedure to avoid code duplication as shown in
figure (4-3)

Calling a (Sub) Procedure

Calling a (Sub) Procedure


Declaring a (Sub) Procedure

Code executed when you call the


(Sub) procedure

Figure (4-3) declaring and calling a Procedure

(86)

Chapter Four
Data

Procedures

In figure (4-3) a sub Procedure has been declared of name (ShowOddOrEven) ,


the code written in the scope of this Sub Procedure; causes a series of
statements to be executed when calling this procedure.
The names of the procedures have been called (written) in both event procedure
(Button1_Click) and (Button2_Click).

When testing the program, we find that both buttons, the ()


button and the( ) button; when clicking any of them, give the
same result, because the starting value of the iteration (repetition) in
both procedure has the same value 1. As shown in figure (4-4) and
figure (4-5).

Figure (4-4) user interface

Starting value of the


repetition

Figure (4-5) Part of the window code

(87)

Chapter Four
Data

Procedures

To solve the previous problem ;the procedure (ShowOddOrEven) receives


values known as arguments, these arguments are the values (1) or (2) being
passed into the procedure when calling it .These arguments are used to specify
whether the odd numbers will be displayed or the even numbers. This is done by
adding the variable (Start) that will be called later.
as shown in figure (4-6) and figure (4-7).

Declaring a Parameter

Using a Parameter

Figure (4-6) declaring a Parameter


In figure (4-6) a sub Procedure of the name (ShowOddOrEven) has been
declared and a Parameter (a special variable that receives an argument being
passed into the procedure ) named (Start) has been also declared and used in
the code to specify the starting value of the iteration (repetition), accordingly it
displays odd or even numbers.

Setting an Argument value

Figure (4-7) Passing Arguments to Procedures


In figure (4-7) the sub Procedure (ShowOddOrEven) has been called twice, given
a different value in each time, to specify what will be displayed. The odd
numbers or the even numbers, these given values are called arguments.

(88)

Chapter Four
Data

Procedures

When testing the program, we find that both buttons, the ()


button and the( ) button when clicked, give different results as
shown in figure (4-8) and figure (4-9).

Figure (4-8) when clicking the ( )button

Figure (4-9) when clicking the ( )button

(89)

Chapter Four
Data

Procedures

In the procedure declaration, we can use more than one Parameter.

A Parameter allows the calling code to receive values; that doesnt


exist in the procedure and, unidentified in advance; but specified
when you call this procedure.

Dear student, you can develop the sub Procedure of name (ShowOddOrEven)
that receives the start value and end value in the iteration (repetition); as
shown in figure (4-10).

Declaring a Parameter
Using a Parameter

Figure (4-10) the declaration of more parameters


It is clear that two values are given when calling the procedure
(ShowOddOrEven) as shown in figure (4-11)

Calling the procedure


(ShowOddOrEven)
Giving two values

Figure (4-11) Calling the procedure (ShowOddOrEven )

(90)

Chapter Four
Data

Procedures

You can call the Procedure any number of times.

You can also adjust the display of odd and even numbers; as well

as setting their range. For example, if the given values are (5, 35)
the odd numbers from 5 to 35 will be displayed .If the given values
are (8, 45) then the even numbers from 8 to 45 will be displayed
as shown in figure (4-12).

Display the odd numbers from 5 to 35


Or

Display the even numbers from 8 to 45


Figure (4-12)

(91)

Chapter Four
Data

Procedures

We declare a Function if we have a code that returns a value needed in the


program; for example: Circumference of a circle, Area of a square, net salary of
the employee, Tax payable.etc. So that it can be used during the execution of
program instructions or as output to the user.

Function Function Name (Parameters) As DataType


Code
Return Value
EndFunction
Where:
1- The function name follows the word Function.
2- Inside the parenthesis is an optional list of Parameters used in the code
3- Following the parenthesis is As Datatype. The datatype listed is the part of
the declaration that determines the data type of the value returned by the
function.
4- A Code or set of commands and instructions that will be executed when
calling the Function.
5- The value returned by the function.

(92)

Chapter Four
Data

Procedures

Dear student, with the help of your teacher do the following:


1- Design a Forms window as shown in figure (4-13).

Figure (4-13) User interface


2- Open the Code Window, press (F7) key then type the Code as shown in
figure (4-14).

Figure (4-14) The code window where the function (sum) is declared

(93)

Chapter Four
Data

Procedures

Dear student, we declared the (Sum) Function; of Data Type


(Single) that receives two values from the function variables
(First and Second).
A variable named total have been declared of type (Single).
The sum of the two values First and Second will be assigned to

the variable total.


After the function executes, the return statement sends a value
back to the statement and it is assigned to total.
The value total is stored in the name of the function (Sum) as will
be seen when it is called.

3- Create an event procedure for Button1, and then type the code as shown
in figure (4-15).

Figure (4-15) calling the (sum) function

The variables (x) and (y) have been declared and the input values
from a user are assigned to each of them in (TextBox).
The value of the (Sum) function has been assigned to the Property
(Text) of (Label4) control after receiving the two values (x), (y).

(94)

Chapter Four
Data

Procedures

Variables: We can assign values to Variables; during the


declaration and the execution of the Program instructions, as well
as using these values stored.
Constants: We can assign values to Constants; during the
declaration only, as well as using these values stored.
Functions: We cannot assign values to Functions, but function
can be called producing a value that is stored, as well as using
this value stored.

4- Press (F5) key then input the values as shown in figure (4-16).

Figure (4-16) User interface


Dear student, the previous example shows that you can:
1. Declare a Function.
2. Determine its Parameters.
3. Specify the Function type.

(95)

Chapter Four
Data

Procedures

4. Write Code within this Function.


5. Return a value using the Return statement.

It is preferred when naming Functions; to give names related to their


functionality

The declaration of the function (Factorial) ; to calculate the factorial of a number


as shown in figure (4-17).

Figure (4-17) the function (Factorial) to calculate the factorial of a number


The declaration of the function: (Factorial), and the Parameter (Number).
Where the output of this function is an (Integer); also the variables (i) and
(res) are declared; where the variable (i) is a counter for the iteration
process, and (res) to store the factorial calculation.
Calling the function (Factorial)

The call of the function (Factorial) through a (MessageBox)


Calling the function (Factorial); giving a value (5) then displaying the output
through a (MessageBox).

(96)

Chapter Four
Data

Procedures

You call the function (Area); to calculate the area of a circle as shown in figure
(4-18).

Figure (4-18) the function (Area) to calculate the area of a circle


The function (Area), and the Parameter (radius) have been declared; and the
output of this function is declared as (Single); also the variables (x) is
declared as a constant that stores the value (22/7) ,and the variable (res) is
declared to store the calculation of the circle area.
Calling the function (Area)

The call of the function (Area) through a (MessageBox)


Calling the function (Area); giving a value (5) for the radius, then displaying
the output in a message box.

Predefined functions are functions defined in programming languages called


when a program is executed.
We will demonstrate some of these functions:
1. The function (Show) declared within the class (MessageBox); shows a
Message box .The content of this function is determined by the
Parameters given, for example:

(97)

Chapter Four
Data

Procedures

When calling the function, a Message box will be displayed as shown in


figure (4-19)

Figure (4-19) the Message Box


2. The function (IsNumeric) can test a value; if it is numeric or not, where
the result of this function will be (True) when the value is numeric; and
(False) when the value is non-numeric.
Calling the function as follows

When calling the function; the displayed result will be (False) as the value
assigned to it is Five and cannot be converted to a numeric value.

All functions must be written on the right side of the assignment


equation to get the results of these functions.
There are some functions that do not take any parameter while
each function must return a result (output).

The (Sub) Procedures are not used in the assignment statement.


The Event Procedure is considered a (Sub) Procedure.
Parameters given to any Procedure can be (an abstract value or a
variable or a constant or a function).

(98)

Chapter Four
Data

Procedures

First: State whether the following statements are true ( ) or false (X)
1- The Procedure is declared once, while can be called any number
of times.

2- The Predefined Functions should be declared first.

3- The return value of the function (Area) is (Single) as


demonstrated in the declaration:

4- The Parameters of a Procedure can receive values from outside a


procedure.

5- The Sub Procedures are used on both sides of an assignment


statement, while the functions should not be used in any side.

6- The Sub Procedure doesnt return any value, while the function
returns a value.

7- It is required when declaring Procedures; that at least one


Parameter is used.

8- Parameters given to any Procedure can be (an abstract value

Function Area(ByVal radius As Single) As Integer

or a variable or a constant or a function).


Second: Type in front of each statement in column (b), the appropriate number
of column (a)
Serial
A
1
Sub Procedure

Function

Variables

Constants

Serial

B
Is used on the right side of
the assignment statement
and does not have any
value.
Is used on the right side of
the assignment statement
and a value is assigned to it
in declaration only
Is never used in the
assignment statement
Is used on both sides of the
assignment statement

(99)

Chapter Four
Data

Procedures

Third: From the demonstrated code extract the following:

1- Procedure Name:
2- Parameters given to this Procedure and their types:
Fourth: From the demonstrated code extract the following:

1234-

Procedure Name:
Parameters given to this Procedure:
The return value:
The Data type for:
a- A function
b- Parameter given to a function c- the return value

Fifth: From the demonstrated code extract the following:

1234-

Procedure Name:
Parameters given to this Procedure:
The return value:
The Data type for:
a- A function
b- Parameter given to a function c- the return value

(100)

Chapter Four
Data

Procedures

Sixth: Write down if the following is a (Sub) Procedure or a Function:


A-

.....................................................
B-

.....................................................
C-

.....................................................
Seventh: Extract the names of the Functions from the following code:

.....................................................
.....................................................

(101)

Das könnte Ihnen auch gefallen