|
Also, you do NOT need to define a variable before using it...
program Hello i = 3.14; // You don't need to define i to use it... print *,"i = ", i; end program Hello |
If you should hate anything in a programming language, this feature would be it...
With this "feature", you can mistype a variable name and instead of the compiler alerting of your mistake, your program will compile but will not work....
Example:
program Hello r = 3.14; i = r; print *,"r = ", r, " i = ", i; end program Hello |
implicit none |
Example:
program Hello implicit none i = 3.14; ! Compile error print *,"i = ", i; end program Hello |
Syntax: TYPE VariableName [, VariableName ...] or: TYPE [, ATTRIBUTE] (KIND) :: VariableName [, VariableName ...] |
|
integer | integer valued variable |
real | floating point valued variable |
complex | Complex number, is just a PAIR of float variables |
character | Character (not discussed in this course) |
logical | Boolean valued variables ((not discussed in this course) |
parameter | constant value |
public | "Class" usage |
private | "Class" usage |
external | defined outside a function |
allocatable | dynamic allocation, like "new" in C++ |
intent (in | out | inout ) | specify how a parameter variable is used |
dimension(...) | used to define array variables |
4 | 4 bytes |
8 | 8 bytes |
integer i1, i2 ! F77 style is acceptable real r1, r2 |
Sorry, no arrays yet... they will be covered in a separate webpage...
Example:
complex :: x1, y1, z1 x1 = (0, 1); y1 = (0, 1) ! x1 = i, y1 = i z1 = x1 * y1 ! i*i = -1 |
REAL(8) -> REAL(4) -> INTEGER(8) -> INTEGER(4) -> INTEGER(2) -> INTEGER(1) |
E.g., assigning INTEGER(8) to REAL(4) is safe
Example:
complex(8) :: x; real(8) :: r; ! ================================================ ! Convert complex to real ! ================================================ x = (123, 456) r = x ! ================================================ ! Convert real to complex ! ================================================ r = 12345 x = r |
Example:
integer, parameter :: MAX = 20 real, parameter :: Pi = 3.14159 |
Notice that constants in Fortran has a type