MODULAR PROGRAMMING Computer Programming for
Civil Engineering
Instructor: Nikol O. Telen
May 22, 2023
Mindanao State University - General Santos City
Table of Contents
1. Modular Programming
2. Sub Procedures
3. Function Procedures
4. Exercise
5. Passing By Value or By Reference
6. Static Variables
7. Thank You!
Modular Programming
Modular Programming
Breaking complicated tasks or subjects into more manageable parts to make them easier to
handle
in programming, it is a way to divide complicated programs into smaller procedures or
modules
2
Modular Programming
Advantages of modular programming include:
1. programming logic is easier for the developer to construct and the user to understand
2. development is faster because each procedure can be done separately 3. modules can be
stored in a library for future use
4. programs can be easily debugged and tested
5. program maintenance and modification is easier
3
Modular Programming
Two types of procedure are commonly used:
1. Sub (short for subroutine) procedure - can return several results 2. Function procedure -
returns a single result
Sub Procedures
Sub Procedures
A simplified representation of the syntax for Sub procedures is:
Sub name ( arguments )
statements
Exit Sub
statements
End Sub
where, name is the name of the sub,
arguments represents the argument list that is passed to the Sub procedure when it is called
(multiple variables are separated by commas), statements are any group of statements to be
executed within the Sub procedure
Exit Sub is a statement that provides an immediate exit from the procedure before its
completion
5
Sub Procedures
A Sub procedure can be invoked by a Call statement, which has the syntax:
Call name ( arguments )
The Call statement transfers control to the Sub procedure. After the Sub is executed, control
returns to the line following the Call statement.
6
Sub Example
Sub SimpleAddition ()
’assign values
a = 15
b = 28
’compute the sum
Call Add (a, b, c)
’display the results
MsgBox c
End Sub
Sub Add(a, b, c)
c = a + b
End Sub
Function Procedures
Function Procedures
Like a Sub procedure, a Function procedure is a separate procedure that can take arguments
and perform a series of statements. However, it differs from a Sub in that:
it is invoked through its name and
it returns a single value.
8
Function Procedures
A simplified representation of the syntax of a Function procedure is:
Function name ( arguments )
[ statements ]
[ name = expression ]
[ Exit Function ]
[ statements ]
[ name = expression ]
End Function
where name is the name you give to the function,
arguments represents the argument list that is passed to the function when it is invoked,
statements are any group of statements to be executed within the function, and
Exit Function is a statement that provides an immediate exit from the procedure before its
completion.
9
Function Procedures
A value is returned from a function by assigning the value to the function name.
Any number of such assignments can appear anywhere within the function
a Function procedure may be invoked by using the function’s name, followed by the argument
list in parentheses:
10
Function Example
Sub SimpleAddition ()
’assign values
a = 15
b = 28
’compute the sum
c = Add (a, b)
’display the results
MsgBox c
End Sub
Function Add (a, b)
Add = a + b
End Function
11
Exercise
Exercise
Open Excel, and open a new workbook called ParamList.xlsm. Go to the VBE and insert the
following module:
Sub Test ()
a = 1
b = 2
c = 3
MsgBox a & " " & b & " " & c & " " & d
Call Arg (a, b, c)
MsgBox a & " " & b & " " & c & " " & d
End Sub
Sub Arg (b, a, d)
MsgBox a & " " & b & " " & c & " " & d
d = 4
c = b
MsgBox a & " " & b & " " & c & " " & d
End Sub
Investigate how the parameter list controls the flow of information between procedures:
12
Passing By Value or By Reference
Passing By Value or By Reference
In VBA, ”passing by value” and ”passing by reference” are two ways of passing arguments or
variables within functions or subroutines. In passing by value, the original variable is not
modified, and any changes are local to the function or subroutine.
In passing by reference, changes to the variable are effective throughout the program and
directly affect the original variable.
13
Passing By Value
In passing a method by value, the value of the variable is passed to the method. This means
that a copy of this value is used within the function or subroutine, and the original value of the
variable is not directly modified.
Sub Example ( ByVal x As Integer )
x = x + 1 ’ This change will not affect the original value of the variable passed to the function .
End Sub
Sub Main ()
Dim num As Integer
num = 5
Example (num )
MsgBox num ’ This will display the value 5 because the original value of num was not modified in the function .
End Sub
14
Passing By Reference
In passing a method by reference, the reference or address of the variable is passed to the
method. This means that the actual variable is used within the function or subroutine, and any
changes made to the variable will have a permanent effect on its original value.
Sub Example ( ByRef x As Integer )
x = x + 1 ’ This change will permanently affect the original value of the variable passed to the function .
End Sub
Sub Main ()
Dim num As Integer
num = 5
Example (num )
MsgBox num ’ This will display the value 6 because the original value of num was modified in the function .
End Sub
15
Static Variables
Static Variables
Static variables are variables that retain their values between multiple executions of a
procedure or function.
Unlike local variables that lose their values each time a procedure or function completes, static
variables preserve their values throughout the lifetime of the program or until explicitly reset.
Here’s how you can declare and use a static variable in VBA:
Sub Example ()
Static count As Integer ’ Declare a static variable
count = count + 1 ’ Modify the value of the static variable MsgBox " Count : " & count
’ The value of the static variable will be retained between executions
End Sub
16
Static Variables
Static variables are useful in situations where you need to maintain information or state
across multiple function or subroutine calls. They can be particularly handy for
implementing counters, tracking cumulative values, or storing persistent data within a
procedure.
17
Thank You!