As part of an assignment, I've been given (old) code for a FORTRAN program. I only have experience with C++ (and Oberon) and what knowledge I do have of Fortran, I've obtained through various (parts of) tutorials, which aren't always consistent with one another. However, despite my basic knowledge this code hurts my eyes with its lack of elegance or consistency. But even more disturbing: it seems to just be full of programming malpractices.
The particular piece of the code I want to ask about looks similar to this example:
PROGRAM Foo
IMPLICIT NONE
CALL Bar(0.0,-1)
END PROGRAM Foo
SUBROUTINE Bar(t,i)
DIMENSION(5) :: R = 0.5
REAL :: s = 0.5
DO k = 1, 5
R(k) = k * i
END DO
CALL Random(s) ! Random is a well-behaved pseudorandom number generator
! on the interval ]0,1[ implemented elsewhere.
k = 1 + (s * 5)
t = R(k)
END SUBROUTINE Bar
I'll explain what I believe the idea is. Random(s)
stores a value between 0.0 and 1.0 in s
. This means that 1 + (s * 5)
will yield a real number between 1 and 5 (some testing of the pRNG shows that its maximum leads to ~5.992 and its minimum to ~1.0016, say). If the left hand side is an integer, automatic type conversion of the right hand side will occur and the mantissa will be chopped off, leaving an integer value on the interval [1,5]. But k
isn't declared as an INTEGER
anywhere. Instead, k
has been initialized as the loop counter for the DO
-loop. Of course this means it must be an integer, but I would think this is bad practice. Am I right? Additionally, I would frown upon the declaration of the array R
, since there is no explicit type specifier. I've made it clear that it will store REALs
by initializing the array, but this is not done in the actual code...
Of course I've adjusted the actual code more than just that initialization, hopefully adhering to a higher standard. (Imagine IMPLICIT
variables, inconsistent capitalization and goto
's all over the place.) Also, perhaps I can ask this as well while I'm at it: is the modern standard to end programs, subroutines and functions with the syntax END <PROGRAM/SUBROUTINE/FUNCTION> Name
? In the code I was given they simply used END
. I've been assuming the correct modern syntax is the one I mentioned, but I'm not sure. Just like I'm not sure if names of programs etc. should be uncapitalized. (in the code I was given they are in all-caps)