2.3.6 Structured Programming
2.3.6 Structured Programming
Procedural languages are often referred to as third generation languages and include FORTRAN, ALGOL,
COBOL, BASIC, and PASCAL.
Statement
A statement is a single instruction in a program, which can be converted into machine code and executed.
In most languages a statement is written on a single line, but some languages allow multiple lines for
single statements.
A=X*X
While x < 10
Subroutine
A subroutine is a self-contained section of program code that performs a specific task, as part of the main
program.
Procedure
A procedure is a subroutine that performs a specific task without returning a value to the part of the
program from which it was called.
Function
A function is a subroutine that performs a specific task and returns a value to the part of the program
from which it was called.
Note that a function is ‘called’ by writing it on the right hand side of an assignment statement.
Page 1 of 11
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
Parameter
The subroutine uses the value of the parameter within its execution. The action of the subroutine will be
different depending upon the parameters that it is passed.
Parameters are placed in parenthesis after the subroutine name. For example:
Subroutine/sub-program
A subroutine is a self-contained section of program code which performs a specific task and is referenced
by a name.
A subroutine resembles a standard program in that it will contain its own local variables, data types, labels
and constant declarations.
There are two types of subroutine. These are procedures and functions.
Procedures are subroutines that input, output or manipulate data in some way.
A subroutine is executed whenever its name is encountered in the executable part of the main program.
The execution of a subroutine by referencing its name in the main program is termed ‘calling’ the
subroutine.
Page 2 of 11
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
The same lines of code are re-used whenever they are needed – they do not have to be repeated in
different sections of the program.
It is easy to share procedures and functions with other programs – they can be incorporated into
library files which are then ‘linked’ to the main program.
A programmer can create their own routines that can be called in the same way as any built-in
command.
A Sub procedure is a series of Visual Basic statements enclosed by the Sub and End Sub statements.
0T 0T3 0T3 0T 0T 0T3 0T3 0T 0T 0T3 0T3 0T
The Sub procedure performs a task and then returns control to the calling code, but it does not return a
0T 0T3 0T3 0T
Each time the procedure is called, its statements are executed, starting with the first executable statement
after the Sub statement and ending with the first End Sub, Exit Sub, or Return statement encountered.
0T 0T3 0T3 0T 0T 0T3 3T 0T 0T3 3T 0T 0T3 3T
You can define a Sub procedure in modules, classes, and structures. By default, it is Public, which means
0T 0T3 0T3 0T 0T 0T3 3T
you can call it from anywhere in your application that has access to the module, class, or structure in
which you defined it. The term, method, describes a Sub or Function procedure that is accessed from
0T 0T39 39T 0T 0T3 0T3 0T 0T 0T3 0T3 0T
outside its defining module, class, or structure. For more information, see Procedures in Visual Basic. 0T 0T32U U32T
A Sub procedure can take arguments, such as constants, variables, or expressions, which are passed to it
0T 0T3 0T3 0T
Page 3 of 11
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
End Sub
1T
The modifiers can specify access level and information about overloading, overriding, sharing, and
0T 0T39 0T39 0T
shadowing. For more information, see Sub Statement (Visual Basic). 0T 0T32U U32T
Parameter Declaration
You declare each procedure parameter similarly to how you declare a variable, specifying the parameter
name and data type. You can also specify the passing mechanism, and whether the parameter is optional
or a parameter array.
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:
When control passes to the procedure, each parameter is treated as a local variable. This means that its
lifetime is the same as that of the procedure, and its scope is the whole procedure.
Page 4 of 11
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
name in an expression. You must provide values for all arguments that are not optional, and you must
enclose the argument list in parentheses. If noarguments are supplied, you can optionally omit the
parentheses. The use of
the Call keyword is optional but not recommended.
0T 0T3 0T3 0T
The following Sub procedure tells the computer operator which task the application is about to perform, 0T 0T3 0T3 0T
and also displays a time stamp. Instead of duplicating this code at the start of every task, the application
just calls tellOperator from various locations. Each call passes a string in the task argument that identifies
0T 0T1 0T1 0T 0T 0T1 0T1 0T
VB
VB
tellOperator("file update")
A Function procedure is a series of Visual Basic statements enclosed by the Function and End
0T 0T3 0T3 0T 0T 0T3 0T3 0T 0T 0T3
Function statements. The Function procedure performs a task and then returns control to the calling
0T3 0T 0T 0T3 0T3 0T
code. When it returns control, it also returns a value to the calling code.
Each time the procedure is called, its statements run, starting with the first executable statement after
the Function statement and ending with the first End Function, Exit Function, or Return statement
0T 0T3 0T3 0T 0T 0T3 3T 0T 0T3 3T 3T 0T3 0T
encountered.
Page 5 of 11
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
you can call it from anywhere in your application that has access to the module, class, or structure in
which you defined it.
A Function procedure can take arguments, such as constants, variables, or expressions, which are passed
0T 0T3 0T3 0T
Declaration Syntax
VB
Data Type
Every Function procedure has a data type, just as every variable does. This data type is specified by
0T 0T3 0T3 0T
the As clause in the Function statement, and it determines the data type of the value the function returns
0T 0T3 0T3 0T 0T 0T3 0T3 0T
VB
Page 6 of 11
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
The value a Function procedure sends back to the calling code is called its return value. The procedure
0T 0T3 0T3 0T
It uses the Return statement to specify the return value, and returns control immediately to the
0T 0T3 0T3 0T
VB
It assigns a value to its own function name in one or more statements of the procedure. Control
does not return to the calling program until an Exit Function or End Functionstatement is executed.
0T 0T3 0T3 0T 0T 0T3 3T
VB
The advantage of assigning the return value to the function name is that control does not return from the
procedure until it encounters an Exit Function or End Function statement. This allows you to assign a
0T 0T3 0T3 0T 0T 0T3 0T3 0T
Page 7 of 11
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
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.
When you call a Function procedure, you do not have to use its return value. If you do not, all the actions
0T 0T3 0T3 0T
of the function are performed, but the return value is ignored.” MsgBox” is often called in this manner. 0T 0T32U U32T 0T 0T
The following Function procedure calculates the longest side, or hypotenuse, of a right triangle, given the
0T 0T3 0T3 0T
VB
VB
Page 8 of 11
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
In Visual Basic, you can pass an argument to a procedure by value or by reference. This is known as the
passing mechanism, and it determines whether the procedure can modify the programming element
underlying the argument in the calling code. The procedure declaration determines the passing
mechanism for each parameter by specifying the ByVal (Visual Basic) or ByRef (Visual Basic) keyword.
Distinctions
When passing an argument to a procedure, be aware of several different distinctions that interact with
each other:
Whether the underlying programming element is modifiable or not
Whether the argument itself is modifiable or not
Whether the argument is being passed by value or by reference
Whether the argument data type is a value type or a reference type
For more information, see Differences Between Modifiable and Nonmodifiable Arguments (Visual Basic)
and Differences Between Passing an Argument By Value and By Reference (Visual Basic).
You should choose the passing mechanism carefully for each argument.
Protection.
In choosing between the two passing mechanisms, the most important criterion is the exposure of calling
variables to change. The advantage of passing an argument ByRef is that the procedure can return a value to
the calling code through that argument. The advantage of passing an argument ByVal is that it protects a
variable from being changed by the procedure.
Performance.
Although the passing mechanism can affect the performance of your code, the difference is usually minor.
One exception to this is a value type passed ByVal. In this case, Visual Basic copies the entire data contents
of the argument. Therefore, for a large value type such as a structure, it can be more efficient to pass it
ByRef.
Page 9 of 11
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
The procedure declaration specifies the passing mechanism for each parameter. The calling code can't
override a ByVal mechanism.
If a parameter is declared with ByRef, the calling code can force the mechanism to ByVal by enclosing the
argument name in parentheses in the call. For more information, see How to: Force an Argument to Be
Passed by Value (Visual Basic).
The default in Visual Basic is to pass arguments by value.
U
If the calling code element underlying the argument is a nonmodifiable element, declare the corresponding
parameter ByVal (Visual Basic). No code can change the value of a non-modifiable element.
If the underlying element is modifiable, but you do not want the procedure to be able to change its value,
declare the parameter ByVal. Only the calling code can change the value of a modifiable element passed by
value.
If the procedure has a genuine need to change the underlying element in the calling code, declare the
corresponding parameter ByRef (Visual Basic).
If the correct execution of the code depends on the procedure changing the underlying element in the
calling code, declare the parameter ByRef. If you pass it by value, or if the calling code overrides the ByRef
passing mechanism by enclosing the argument in parentheses, the procedure call might produce unexpected
results.
Example:
Description:The following example illustrates when to pass arguments by value and when to pass them by
reference. Procedure Calculate has both a ByVal and a ByRef parameter. Given an interest rate, rate, and a
sum of money, debt, the task of the procedure is to calculate a new value for debt that is the result of
applying the interest rate to the original value of debt. Because debt is a ByRef parameter, the new total is
reflected in the value of the argument in the calling code that corresponds to debt. Parameter rate is a
ByVal parameter because Calculate should not change its value.
Page 10 of 11
Computer Science 9608 (Notes)
Chapter: 2.3 Programming
Module Module1
Sub Main()
' Two interest rates are declared, one a constant and one a
' variable.
Const highRate As Double = 12.5
Dim lowRate = highRate * 0.6
' Calculate the total debt with the high interest rate applied.
' ArgumenthighRate is a constant, which is appropriate for a
' ByVal parameter. Argument debtWithInterest must be a variable
' because the procedure will change its value to the calculated
' total with interest applied.
Calculate(highRate, debtWithInterest)
Page 11 of 11