CSC301 - CHAPTER 4-2 - Sub Procedures - Functions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Write reusable code in sub procedures

CHAPTER 4: and function procedures

SUB PROCEDURES Call the procedures from other


AND locations
FUNCTIONS
PROCEDURES

¡ When a program is broken into manageable parts, each part is called a


procedure
¡ A procedure is a named set of code that performs a given task
¡ Visual Basic provides two types of procedures: Sub procedures and
Function procedures
CODE A SUB PROCEDURE (1 OF 3)

¡ A Sub procedure is a procedure that completes its task but does


not return any data to the calling procedure
¡ A Sub procedure is the series of Visual Basic statements enclosed by
the Sub and End Sub statements
¡ A Sub procedure is called with a statement consisting of the
procedure name and a set of parentheses in the form of a procedure
call
CODE A SUB
PROCEDURE
(2 OF 3)
CODE A SUB
PROCEDURE
(3 OF 3)
PASS ARGUMENTS
(1 OF 2)
procedure call arguments
¡ When a procedure is
called, however, the call
statement can pass an
argument to the called
procedure
PASS ARGUMENTS (2 OF 2)
¡ When an argument is passed ByVal, it means the
Sub procedure has access to the value of the
passed argument, but does not actually reference
the variable declared in the calling procedure
PASS
¡ The value is copied into a variable whose name is
ARGUMENTS specified in the Sub procedure declaration
BY VALUE statement.
(BYVAL) (1 OF The value 5 is copied into
the variable intNumber
3)
PASS ARGUMENTS BY VALUE (BYVAL) (2 OF 3)
PASS ARGUMENTS BY
VALUE (BYVAL) (3 OF 3)
¡ The Sub procedure
DisplayMessage is called once
and the Square Sub procedure
is called three times
¡ Either literals or variables can
be passed to a Sub procedure
¡ Passing a value by reference allows code in
the Sub procedure to modify the contents of
the variable that is being passed because
when you use ByRef, you are passing a
reference to the variable that holds the value
PASS instead of the value itself, as when you use
ARGUMENTS BY ByVal.
¡ If a Sub procedure changes the value of a
REFERENCE variable passed ByRef, the original variable in
(BYREF) (1 OF 2) the calling procedure is changed
¡ You should select the option to pass a
variable by reference if you intend to change
the original value when it is passed to the
Sub procedure
PASS ARGUMENTS BY REFERENCE (BYREF) (2 OF 2)
PASS MULTIPLE ARGUMENTS (1 OF 2)

If you have more than


one argument, the
You can pass as many
variables are passed in
arguments as needed to a
the same order in which
Sub procedure
they appear in the
procedure call statement
PASS MULTIPLE ARGUMENTS (2 OF 2)
The value of 10 is copied into
intNum1. The value of 5 is copied
into intNum2.
FUNCTION
PROCEDURES
(1 OF 4)
¡ A Function procedure is
similar to a Sub procedure
except that a Function
procedure returns a single
value to the calling
procedure
¡ The Function procedure is different in
appearance from a Sub procedure in the
following ways:
FUNCTION ¡ The Function procedure call has a receiving
variable that is assigned the returned value
PROCEDURES from the Function procedure
(2 OF 4) ¡ The data type of the return value is listed in
the procedure declaration
¡ The keyword Return is used in the Function
procedure to return a single value
¡ Calling Function

FUNCTION
PROCEDURES
(3 OF 4)

¡ Called Function
¡ When writing a Function procedure,
do not place any lines of its code after
the Return statement because control
FUNCTION returns to the calling procedure when
PROCEDURES (4 the Return statement is executed
OF 4)
¡ It is best to use a Function procedure
when you plan to return a value from
a called procedure
EXCEPTION HANDLING (1 OF 6)

¡ A structured mechanism for handling errors in Visual Basic programs.


¡ Exception handling begins with the Try keyword, followed by one or more catch blocks, followed
by End Try.
¡ Some types of errors are preventable by the programmer, such as dividing by zero. Other errors
may be caused by user input, which is beyond the control of the programmer.
¡ When a program throws an exception, it generates a runtime error.
¡ You can write exception handlers that catch exceptions and find ways for the program to recover.
¡ Exception handlers can handle multiple types of exceptions by specifically identifying different
types of exceptions with different catch blocks.
EXCEPTION HANDLING (2 OF 6)
THE TRY-CATCH SET OF STATEMENTS DETECTS EXCEPTIONS AND TAKES CORRECTIVE ACTION
EXCEPTION HANDLING (3 OF 6)
Exception Type Condition when Code Example
Exception Occurs

ArgumentNullException A variable that has no value Dim strTerm As String


is passed to a procedure 1stDisplay.Items.Add(strTerm)

DivideByZeroException A value is divided by zero intResult = intNum / 0


FormatException A variable is converted to another type that is strTerm = “Code”
not possible intValue = Convert.ToInt32(strTerm)

NullReferenceException A procedure is called when Dim strTerm As String


the result is not possible intValue = strTerm.Length

OverflowException A value exceeds its assigned Dim intCost As Integer


data type intCost = 58 ˆ 4000000000

SystemException Generic Catches all other exceptions

EXCEPTION HANDLING (4 OF 6)
EXCEPTION HANDLING (5 OF 6)
EXCEPTION
HANDLING (6
OF 6)
END OF SUBTOPIC …
SUB PROCEDURES AND
FUNCTIONS

You might also like