1<!--
2Copyright (c) 2018-2019, NVIDIA CORPORATION.  All rights reserved.
3-->
4
5Fortran For C Programmers
6=========================
7
8This note is limited to essential information about Fortran so that
9a C or C++ programmer can get started more quickly with the language,
10at least as a reader, and avoid some common pitfalls when starting
11to write or modify Fortran code.
12Please see other sources to learn about Fortran's rich history,
13current applications, and modern best practices in new code.
14
15Know This At Least
16------------------
17* There have been many implementations of Fortran, often from competing
18  vendors, and the standard language has been defined by U.S. and
19  international standards organizations.  The various editions of
20  the standard are known as the '66, '77, '90, '95, 2003, 2008, and
21  (now) 2018 standards.
22* Forward compatibility is important.  Fortran has outlasted many
23  generations of computer systems hardware and software.  Standard
24  compliance notwithstanding, Fortran programmers generally expect that
25  code that has compiled successfully in the past will continue to
26  compile and work indefinitely.  The standards sometimes designate
27  features as being deprecated, obsolescent, or even deleted, but that
28  can be read only as discouraging their use in new code -- they'll
29  probably always work in any serious implementation.
30* Fortran has two source forms, which are typically distinguished by
31  filename suffixes.  `foo.f` is old-style "fixed-form" source, and
32  `foo.f90` is new-style "free-form" source.  All language features
33  are available in both source forms.  Neither form has reserved words
34  in the sense that C does.  Spaces are not required between tokens
35  in fixed form, and case is not significant in either form.
36* Variable declarations are optional by default.  Variables whose
37  names begin with the letters `I` through `N` are implicitly
38  `INTEGER`, and others are implicitly `REAL`.  These implicit typing
39  rules can be changed in the source.
40* Fortran uses parentheses in both array references and function calls.
41  All arrays must be declared as such; other names followed by parenthesized
42  expressions are assumed to be function calls.
43* Fortran has a _lot_ of built-in "intrinsic" functions.  They are always
44  available without a need to declare or import them.  Their names reflect
45  the implicit typing rules, so you will encounter names that have been
46  modified so that they have the right type (e.g., `AIMAG` has a leading `A`
47  so that it's `REAL` rather than `INTEGER`).
48* The modern language has means for declaring types, data, and subprogram
49  interfaces in compiled "modules", as well as legacy mechanisms for
50  sharing data and interconnecting subprograms.
51
52A Rosetta Stone
53---------------
54Fortran's language standard and other documentation uses some terminology
55in particular ways that might be unfamiliar.
56
57| Fortran | English |
58| ------- | ------- |
59| Association | Making a name refer to something else |
60| Assumed | Some attribute of an argument or interface that is not known until a call is made |
61| Companion processor | A C compiler |
62| Component | Class member |
63| Deferred | Some attribute of a variable that is not known until an allocation or assignment |
64| Derived type | C++ class |
65| Dummy argument | C++ reference argument |
66| Final procedure | C++ destructor |
67| Generic | Overloaded function, resolved by actual arguments |
68| Host procedure | The subprogram that contains a nested one |
69| Implied DO | There's a loop inside a statement |
70| Interface | Prototype |
71| Internal I/O | `sscanf` and `snprintf` |
72| Intrinsic | Built-in type or function |
73| Polymorphic | Dynamically typed |
74| Processor | Fortran compiler |
75| Rank | Number of dimensions that an array has |
76| `SAVE` attribute | Statically allocated |
77| Type-bound procedure | Kind of a C++ member function but not really |
78| Unformatted | Raw binary |
79
80Data Types
81----------
82There are five built-in ("intrinsic") types: `INTEGER`, `REAL`, `COMPLEX`,
83`LOGICAL`, and `CHARACTER`.
84They are parameterized with "kind" values, which should be treated as
85non-portable integer codes, although in practice today these are the
86byte sizes of the data.
87(For `COMPLEX`, the kind type parameter value is the byte size of one of the
88two `REAL` components, or half of the total size.)
89The legacy `DOUBLE PRECISION` intrinsic type is an alias for a kind of `REAL`
90that should be bigger than the default `REAL`.
91
92`COMPLEX` is a simple structure that comprises two `REAL` components.
93
94`CHARACTER` data also have length, which may or may not be known at compilation
95time.
96`CHARACTER` variables are fixed-length strings and they get padded out
97with space characters when not completely assigned.
98
99User-defined ("derived") data types can be synthesized from the intrinsic
100types and from previously-defined user types, much like a C `struct`.
101Derived types can be parameterized with integer values that either have
102to be constant at compilation time ("kind" parameters) or deferred to
103execution ("len" parameters).
104
105Derived types can inherit ("extend") from at most one other derived type.
106They can have user-defined destructors (`FINAL` procedures).
107They can specify default initial values for their components.
108With some work, one can also specify a general constructor function,
109since Fortran allows a generic interface to have the same name as that
110of a derived type.
111
112Last, there are "typeless" binary constants that can be used in a few
113situations, like static data initialization or immediate conversion,
114where type is not necessary.
115
116Arrays
117------
118Arrays are not types in Fortran.
119Being an array is a property of an object or function, not of a type.
120Unlike C, one cannot have an array of arrays or an array of pointers,
121although can can have an array of a derived type that has arrays or
122pointers as components.
123Arrays are multidimensional, and the number of dimensions is called
124the _rank_ of the array.
125In storage, arrays are stored such that the last subscript has the
126largest stride in memory, e.g. A(1,1) is followed by A(2,1), not A(1,2).
127And yes, the default lower bound on each dimension is 1, not 0.
128
129Expressions can manipulate arrays as multidimensional values, and
130the compiler will create the necessary loops.
131
132Allocatables
133------------
134Modern Fortran programs use `ALLOCATABLE` data extensively.
135Such variables and derived type components are allocated dynamically.
136They are automatically deallocated when they go out of scope, much
137like C++'s `std::vector<>` class template instances are.
138The array bounds, derived type `LEN` parameters, and even the
139type of an allocatable can all be deferred to run time.
140(If you really want to learn all about modern Fortran, I suggest
141that you study everything that can be done with `ALLOCATABLE` data,
142and follow up all the references that are made in the documentation
143from the description of `ALLOCATABLE` to other topics; it's a feature
144that interacts with much of the rest of the language.)
145
146I/O
147---
148Fortran's input/output features are built into the syntax of the language,
149rather than being defined by library interfaces as in C and C++.
150There are means for raw binary I/O and for "formatted" transfers to
151character representations.
152There are means for random-access I/O using fixed-size records as well as for
153sequential I/O.
154One can scan data from or format data into `CHARACTER` variables via
155"internal" formatted I/O.
156I/O from and to files uses a scheme of integer "unit" numbers that is
157similar to the open file descriptors of UNIX; i.e., one opens a file
158and assigns it a unit number, then uses that unit number in subsequent
159`READ` and `WRITE` statements.
160
161Formatted I/O relies on format specifications to map values to fields of
162characters, similar to the format strings used with C's `printf` family
163of standard library functions.
164These format specifications can appear in `FORMAT` statements and
165be referenced by their labels, in character literals directly in I/O
166statements, or in character variables.
167
168One can also use compiler-generated formatting in "list-directed" I/O,
169in which the compiler derives reasonable default formats based on
170data types.
171
172Subprograms
173-----------
174Fortran has both `FUNCTION` and `SUBROUTINE` subprograms.
175They share the same name space, but functions cannot be called as
176subroutines or vice versa.
177Subroutines are called with the `CALL` statement, while functions are
178invoked with function references in expressions.
179
180There is one level of subprogram nesting.
181A function, subroutine, or main program can have functions and subroutines
182nested within it, but these "internal" procedures cannot themselves have
183their own internal procedures.
184As is the case with C++ lambda expressions, internal procedures can
185reference names from their host subprograms.
186
187Modules
188-------
189Modern Fortran has good support for separate compilation and namespace
190management.
191The *module* is the basic unit of compilation, although independent
192subprograms still exist, of course, as well as the main program.
193Modules define types, constants, interfaces, and nested
194subprograms.
195
196Objects from a module are made available for use in other compilation
197units via the `USE` statement, which has options for limiting the objects
198that are made available as well as for renaming them.
199All references to objects in modules are done with direct names or
200aliases that have been added to the local scope, as Fortran has no means
201of qualifying references with module names.
202
203Arguments
204---------
205Functions and subroutines have "dummy" arguments that are dynamically
206associated with actual arguments during calls.
207Essentially, all argument passing in Fortran is by reference, not value.
208One may restrict access to argument data by declaring that dummy
209arguments have `INTENT(IN)`, but that corresponds to the use of
210a `const` reference in C++ and does not imply that the data are
211copied; use `VALUE` for that.
212
213When it is not possible to pass a reference to an object, or a sparse
214regular array section of an object, as an actual argument, Fortran
215compilers must allocate temporary space to hold the actual argument
216across the call.
217This is always guaranteed to happen when an actual argument is enclosed
218in parentheses.
219
220The compiler is free to assume that any aliasing between dummy arguments
221and other data is safe.
222In other words, if some object can be written to under one name, it's
223never going to be read or written using some other name in that same
224scope.
225```
226  SUBROUTINE FOO(X,Y,Z)
227  X = 3.14159
228  Y = 2.1828
229  Z = 2 * X ! CAN BE FOLDED AT COMPILE TIME
230  END
231```
232This is the opposite of the assumptions under which a C or C++ compiler must
233labor when trying to optimize code with pointers.
234
235Overloading
236-----------
237Fortran supports a form of overloading via its interface feature.
238By default, an interface is a means for specifying prototypes for a
239set of subroutines and functions.
240But when an interface is named, that name becomes a *generic* name
241for its specific subprograms, and calls via the generic name are
242mapped at compile time to one of the specific subprograms based
243on the types, kinds, and ranks of the actual arguments.
244A similar feature can be used for generic type-bound procedures.
245
246This feature can be used to overload the built-in operators and some
247I/O statements, too.
248
249Polymorphism
250------------
251Fortran code can be written to accept data of some derived type or
252any extension thereof using `CLASS`, deferring the actual type to
253execution, rather than the usual `TYPE` syntax.
254This is somewhat similar to the use of `virtual` functions in c++.
255
256Fortran's `SELECT TYPE` construct is used to distinguish between
257possible specific types dynamically, when necessary.  It's a
258little like C++17's `std::visit()` on a discriminated union.
259
260Pointers
261--------
262Pointers are objects in Fortran, not data types.
263Pointers can point to data, arrays, and subprograms.
264A pointer can only point to data that has the `TARGET` attribute.
265Outside of the pointer assignment statement (`P=>X`) and some intrinsic
266functions and cases with pointer dummy arguments, pointers are implicitly
267dereferenced, and the use of their name is a reference to the data to which
268they point instead.
269
270Unlike C, a pointer cannot point to a pointer *per se*, nor can they be
271used to implement a level of indirection to the management structure of
272an allocatable.
273If you assign to a Fortran pointer to make it point at another pointer,
274you are making the pointer point to the data (if any) to which the other
275pointer points.
276Similarly, if you assign to a Fortran pointer to make it point to an allocatable,
277you are making the pointer point to the current content of the allocatable,
278not to the metadata that manages the allocatable.
279
280Unlike allocatables, pointers do not deallocate their data when they go
281out of scope.
282
283A legacy feature, "Cray pointers", implements dynamic base addressing of
284one variable using an address stored in another.
285
286Preprocessing
287-------------
288There is no standard preprocessing feature, but every real Fortran implementation
289has some support for passing Fortran source code through a variant of
290the standard C source preprocessor.
291Since Fortran is very different from C at the lexical level (e.g., line
292continuations, Hollerith literals, no reserved words, fixed form), using
293a stock modern C preprocessor on Fortran source can be difficult.
294Preprocessing behavior varies across implementations and one should not depend on
295much portability.
296Preprocessing is typically requested by the use of a capitalized filename
297suffix (e.g., "foo.F90") or a compiler command line option.
298(Since the F18 compiler always runs its built-in preprocessing stage,
299no special option or filename suffix is required.)
300
301"Object Oriented" Programming
302-----------------------------
303Fortran doesn't have member functions (or subroutines) in the sense
304that C++ does, in which a function has immediate access to the members
305of a specific instance of a derived type.
306But Fortran does have an analog to C++'s `this` via *type-bound
307procedures*.
308This is a means of binding a particular subprogram name to a derived
309type, possibly with aliasing, in such a way that the subprogram can
310be called as if it were a component of the type (e.g., `X%F(Y)`)
311and receive the object to the left of the `%` as an additional actual argument,
312exactly as if the call had been written `F(X,Y)`.
313The object is passed as the first argument by default, but that can be
314changed; indeed, the same specific subprogram can be used for multiple
315type-bound procedures by choosing different dummy arguments to serve as
316the passed object.
317The equivalent of a `static` member function is also available by saying
318that no argument is to be associated with the object via `NOPASS`.
319
320There's a lot more that can be said about type-bound procedures (e.g., how they
321support overloading) but this should be enough to get you started with
322the most common usage.
323
324Pitfalls
325--------
326Variable initializers, e.g. `INTEGER :: J=123`, are _static_ initializers!
327They imply that the variable is stored in static storage, not on the stack,
328and the initialized value lasts only until the variable is assigned.
329One must use an assignment statement to implement a dynamic initializer
330that will apply to every fresh instance of the variable.
331Be especially careful when using initializers in the newish `BLOCK` construct,
332which perpetuates the interpretation as static data.
333(Derived type component initializers, however, do work as expected.)
334
335If you see an assignment to an array that's never been declared as such,
336it's probably a definition of a *statement function*, which is like
337a parameterized macro definition, e.g. `A(X)=SQRT(X)**3`.
338In the original Fortran language, this was the only means for user
339function definitions.
340Today, of course, one should use an external or internal function instead.
341
342Fortran expressions don't bind exactly like C's do.
343Watch out for exponentiation with `**`, which of course C lacks; it
344binds more tightly than negation does (e.g., `-2**2` is -4),
345and it binds to the right, unlike what any other Fortran and most
346C operators do; e.g., `2**2**3` is 256, not 64.
347Logical values must be compared with special logical equivalence
348relations (`.EQV.` and `.NEQV.`) rather than the usual equality
349operators.
350
351A Fortran compiler is allowed to short-circuit expression evaluation,
352but not required to do so.
353If one needs to protect a use of an `OPTIONAL` argument or possibly
354disassociated pointer, use an `IF` statement, not a logical `.AND.`
355operation.
356In fact, Fortran can remove function calls from expressions if their
357values are not required to determine the value of the expression's
358result; e.g., if there is a `PRINT` statement in function `F`, it
359may or may not be executed by the assignment statement `X=0*F()`.
360(Well, it probably will be, in practice, but compilers always reserve
361the right to optimize better.)
362