be of type
. VBA also supports implicit variable declarations (using the
data type if none is specified) and type shortcuts (e.g.
in VBA tags
). Because these shortcuts provide “ample rope
with which to hang oneself” we advocate explicit typing as a more sound programming
practice. By including
at the top of a VBA code module, we ask the
compiler to notify us when a variable has not been declared explicitly. It can sometimes
be tedious to follow this methodology, but the benefits are sufficient to warrant the effort.
Symbolic constants in FORTRAN are declared as follows:
The equivalent VBA code is
Const PI As Single = 3.14159
. Note that in FOR-
is implicitly assumed to be of type
(because it begins with a letter out-
side the range I–N). We could first give an explicit declaration, e.g.
, which would change the underlying data type to double precision instead.
Array declaration is similar in the two languages. FORTRAN’s
Dim I(1 To 10) As Integer
in VBA. Again,
is implicitly of type
in FORTRAN. It is also possible to declare array types explicitly, so
Dim X(-5 To 5) As Double
. However, note
that the similar statements
Dim I(7) As Integer
slightly different. FORTRAN assumes 1 as the lower bound for array subscripts, while
VBA assumes 0. So we prefer always to specify upper and lower array bounds explicitly
to avoid any possible confusion.
4.2 Subtle Differences
An issue of especial note is that FORTRAN and VBA treat the implicit conversion of real
to integer values differently. For example, what happens when we assign 3.7 or -2.3 to
an integer variable? The conversion rule in FORTRAN is essentially to truncate the real
number (so 3.7 becomes 3 and -2.3 becomes -2). However VBA rounds real numbers to
the closest integer (or to the closest even integer if the fractional part is .5). Thus, one
must pay special attention to the case where a FORTRAN program assigns real expres-
sions to integer variables. The VBA
function implements the truncation operation
used by FORTRAN. Thus, the FORTRAN statement
I = X
could be coded in VBA as
I = Fix(X)
. If your VBA program is not generating the same values as the original
FORTRAN program, it is a good idea to check for real-to-integer conversion problems.
FORTRAN is somewhat more convenient for initializing blocks of data than VBA. The
statement is often used in FORTRAN to provide initial values for arrays (see the
implementation of Montgomery’s(1982) program in Appendix A, and note the use of
to initialize the 7-element array
near the end of the program). VBA requires the
use of explicit assignment statements to accomplish the same task (observe the corre-
sponding VBA code in Appendix B).
FORTRAN also has a convenient shorthand called the “statement function” which is es-
sentially a one-statement macro. The ORPS algorithms declare several statement func-
FM2(X, Y) = Y * FM1(X, Y)
. This code looks a lot like an assign-
ment to a two-dimensional array
. It is actually the definition of a function
receives two parameters,
. The translation to VBA is straightforward once you
recognize that this is not an assignment statement:
Function FM2(x As Double, y As Double) As Double
FM2 = y * FM1(x, y)
4.3 Comparing Decision and Iteration Structures
Once data types and expressions are properly converted, the other major aspect of FOR-
TRAN-to-VBA translation is rewriting control-flow statements (for decisions and loops).