Lecture 10
Lecture 10
Civil Engineering
1. Modular Programming
2. Sub Procedures
3. Function Procedures
4. Exercise
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
1. Sub (short for subroutine) procedure - can return several results 2. Function procedure -
returns a single result
Sub Procedures
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
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:
8
Function Procedures
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
Investigate how the parameter list controls the flow of information between procedures:
12
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
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
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
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!