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