1932aae77SSourabh Singh Tomar<!--===- docs/FortranForCProgrammers.md 2932aae77SSourabh Singh Tomar 3932aae77SSourabh Singh Tomar Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4932aae77SSourabh Singh Tomar See https://llvm.org/LICENSE.txt for license information. 5932aae77SSourabh Singh Tomar SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6932aae77SSourabh Singh Tomar 7932aae77SSourabh Singh Tomar--> 8932aae77SSourabh Singh Tomar 9271a7bb1SRichard Barton# Fortran For C Programmers 10271a7bb1SRichard Barton 11*b7ff0320Scor3ntin```{contents} 12*b7ff0320Scor3ntin--- 13*b7ff0320Scor3ntinlocal: 14*b7ff0320Scor3ntin--- 15271a7bb1SRichard Barton``` 16eaff2004Ssameeran joshi 17eaff2004Ssameeran joshiThis note is limited to essential information about Fortran so that 18eaff2004Ssameeran joshia C or C++ programmer can get started more quickly with the language, 19eaff2004Ssameeran joshiat least as a reader, and avoid some common pitfalls when starting 20eaff2004Ssameeran joshito write or modify Fortran code. 21eaff2004Ssameeran joshiPlease see other sources to learn about Fortran's rich history, 22eaff2004Ssameeran joshicurrent applications, and modern best practices in new code. 23eaff2004Ssameeran joshi 24271a7bb1SRichard Barton## Know This At Least 25271a7bb1SRichard Barton 26eaff2004Ssameeran joshi* There have been many implementations of Fortran, often from competing 27eaff2004Ssameeran joshi vendors, and the standard language has been defined by U.S. and 28eaff2004Ssameeran joshi international standards organizations. The various editions of 29eaff2004Ssameeran joshi the standard are known as the '66, '77, '90, '95, 2003, 2008, and 30eaff2004Ssameeran joshi (now) 2018 standards. 31eaff2004Ssameeran joshi* Forward compatibility is important. Fortran has outlasted many 32eaff2004Ssameeran joshi generations of computer systems hardware and software. Standard 33eaff2004Ssameeran joshi compliance notwithstanding, Fortran programmers generally expect that 34eaff2004Ssameeran joshi code that has compiled successfully in the past will continue to 35eaff2004Ssameeran joshi compile and work indefinitely. The standards sometimes designate 36eaff2004Ssameeran joshi features as being deprecated, obsolescent, or even deleted, but that 37eaff2004Ssameeran joshi can be read only as discouraging their use in new code -- they'll 38eaff2004Ssameeran joshi probably always work in any serious implementation. 39eaff2004Ssameeran joshi* Fortran has two source forms, which are typically distinguished by 40eaff2004Ssameeran joshi filename suffixes. `foo.f` is old-style "fixed-form" source, and 41eaff2004Ssameeran joshi `foo.f90` is new-style "free-form" source. All language features 42eaff2004Ssameeran joshi are available in both source forms. Neither form has reserved words 43eaff2004Ssameeran joshi in the sense that C does. Spaces are not required between tokens 44eaff2004Ssameeran joshi in fixed form, and case is not significant in either form. 45eaff2004Ssameeran joshi* Variable declarations are optional by default. Variables whose 46eaff2004Ssameeran joshi names begin with the letters `I` through `N` are implicitly 47eaff2004Ssameeran joshi `INTEGER`, and others are implicitly `REAL`. These implicit typing 48eaff2004Ssameeran joshi rules can be changed in the source. 49eaff2004Ssameeran joshi* Fortran uses parentheses in both array references and function calls. 50eaff2004Ssameeran joshi All arrays must be declared as such; other names followed by parenthesized 51eaff2004Ssameeran joshi expressions are assumed to be function calls. 52eaff2004Ssameeran joshi* Fortran has a _lot_ of built-in "intrinsic" functions. They are always 53eaff2004Ssameeran joshi available without a need to declare or import them. Their names reflect 54eaff2004Ssameeran joshi the implicit typing rules, so you will encounter names that have been 55eaff2004Ssameeran joshi modified so that they have the right type (e.g., `AIMAG` has a leading `A` 56eaff2004Ssameeran joshi so that it's `REAL` rather than `INTEGER`). 57eaff2004Ssameeran joshi* The modern language has means for declaring types, data, and subprogram 58eaff2004Ssameeran joshi interfaces in compiled "modules", as well as legacy mechanisms for 59eaff2004Ssameeran joshi sharing data and interconnecting subprograms. 60eaff2004Ssameeran joshi 61271a7bb1SRichard Barton## A Rosetta Stone 62271a7bb1SRichard Barton 63eaff2004Ssameeran joshiFortran's language standard and other documentation uses some terminology 64eaff2004Ssameeran joshiin particular ways that might be unfamiliar. 65eaff2004Ssameeran joshi 66eaff2004Ssameeran joshi| Fortran | English | 67eaff2004Ssameeran joshi| ------- | ------- | 68eaff2004Ssameeran joshi| Association | Making a name refer to something else | 69eaff2004Ssameeran joshi| Assumed | Some attribute of an argument or interface that is not known until a call is made | 70eaff2004Ssameeran joshi| Companion processor | A C compiler | 71eaff2004Ssameeran joshi| Component | Class member | 72eaff2004Ssameeran joshi| Deferred | Some attribute of a variable that is not known until an allocation or assignment | 73eaff2004Ssameeran joshi| Derived type | C++ class | 74eaff2004Ssameeran joshi| Dummy argument | C++ reference argument | 75eaff2004Ssameeran joshi| Final procedure | C++ destructor | 76eaff2004Ssameeran joshi| Generic | Overloaded function, resolved by actual arguments | 77eaff2004Ssameeran joshi| Host procedure | The subprogram that contains a nested one | 78eaff2004Ssameeran joshi| Implied DO | There's a loop inside a statement | 79eaff2004Ssameeran joshi| Interface | Prototype | 80eaff2004Ssameeran joshi| Internal I/O | `sscanf` and `snprintf` | 81eaff2004Ssameeran joshi| Intrinsic | Built-in type or function | 82eaff2004Ssameeran joshi| Polymorphic | Dynamically typed | 83eaff2004Ssameeran joshi| Processor | Fortran compiler | 84eaff2004Ssameeran joshi| Rank | Number of dimensions that an array has | 85eaff2004Ssameeran joshi| `SAVE` attribute | Statically allocated | 86eaff2004Ssameeran joshi| Type-bound procedure | Kind of a C++ member function but not really | 87eaff2004Ssameeran joshi| Unformatted | Raw binary | 88eaff2004Ssameeran joshi 89271a7bb1SRichard Barton## Data Types 90271a7bb1SRichard Barton 91eaff2004Ssameeran joshiThere are five built-in ("intrinsic") types: `INTEGER`, `REAL`, `COMPLEX`, 92eaff2004Ssameeran joshi`LOGICAL`, and `CHARACTER`. 93eaff2004Ssameeran joshiThey are parameterized with "kind" values, which should be treated as 94eaff2004Ssameeran joshinon-portable integer codes, although in practice today these are the 95eaff2004Ssameeran joshibyte sizes of the data. 96eaff2004Ssameeran joshi(For `COMPLEX`, the kind type parameter value is the byte size of one of the 97eaff2004Ssameeran joshitwo `REAL` components, or half of the total size.) 98eaff2004Ssameeran joshiThe legacy `DOUBLE PRECISION` intrinsic type is an alias for a kind of `REAL` 99eaff2004Ssameeran joshithat should be more precise, and bigger, than the default `REAL`. 100eaff2004Ssameeran joshi 101eaff2004Ssameeran joshi`COMPLEX` is a simple structure that comprises two `REAL` components. 102eaff2004Ssameeran joshi 103eaff2004Ssameeran joshi`CHARACTER` data also have length, which may or may not be known at compilation 104eaff2004Ssameeran joshitime. 105eaff2004Ssameeran joshi`CHARACTER` variables are fixed-length strings and they get padded out 106eaff2004Ssameeran joshiwith space characters when not completely assigned. 107eaff2004Ssameeran joshi 108eaff2004Ssameeran joshiUser-defined ("derived") data types can be synthesized from the intrinsic 109eaff2004Ssameeran joshitypes and from previously-defined user types, much like a C `struct`. 110eaff2004Ssameeran joshiDerived types can be parameterized with integer values that either have 111eaff2004Ssameeran joshito be constant at compilation time ("kind" parameters) or deferred to 112eaff2004Ssameeran joshiexecution ("len" parameters). 113eaff2004Ssameeran joshi 114eaff2004Ssameeran joshiDerived types can inherit ("extend") from at most one other derived type. 115eaff2004Ssameeran joshiThey can have user-defined destructors (`FINAL` procedures). 116eaff2004Ssameeran joshiThey can specify default initial values for their components. 117eaff2004Ssameeran joshiWith some work, one can also specify a general constructor function, 118eaff2004Ssameeran joshisince Fortran allows a generic interface to have the same name as that 119eaff2004Ssameeran joshiof a derived type. 120eaff2004Ssameeran joshi 121eaff2004Ssameeran joshiLast, there are "typeless" binary constants that can be used in a few 122eaff2004Ssameeran joshisituations, like static data initialization or immediate conversion, 123eaff2004Ssameeran joshiwhere type is not necessary. 124eaff2004Ssameeran joshi 125271a7bb1SRichard Barton## Arrays 126271a7bb1SRichard Barton 127eaff2004Ssameeran joshiArrays are not types in Fortran. 128eaff2004Ssameeran joshiBeing an array is a property of an object or function, not of a type. 129eaff2004Ssameeran joshiUnlike C, one cannot have an array of arrays or an array of pointers, 130eaff2004Ssameeran joshialthough can can have an array of a derived type that has arrays or 131eaff2004Ssameeran joshipointers as components. 132eaff2004Ssameeran joshiArrays are multidimensional, and the number of dimensions is called 133eaff2004Ssameeran joshithe _rank_ of the array. 134eaff2004Ssameeran joshiIn storage, arrays are stored such that the last subscript has the 135eaff2004Ssameeran joshilargest stride in memory, e.g. A(1,1) is followed by A(2,1), not A(1,2). 136eaff2004Ssameeran joshiAnd yes, the default lower bound on each dimension is 1, not 0. 137eaff2004Ssameeran joshi 138eaff2004Ssameeran joshiExpressions can manipulate arrays as multidimensional values, and 139eaff2004Ssameeran joshithe compiler will create the necessary loops. 140eaff2004Ssameeran joshi 141271a7bb1SRichard Barton## Allocatables 142271a7bb1SRichard Barton 143eaff2004Ssameeran joshiModern Fortran programs use `ALLOCATABLE` data extensively. 144eaff2004Ssameeran joshiSuch variables and derived type components are allocated dynamically. 145eaff2004Ssameeran joshiThey are automatically deallocated when they go out of scope, much 146eaff2004Ssameeran joshilike C++'s `std::vector<>` class template instances are. 147eaff2004Ssameeran joshiThe array bounds, derived type `LEN` parameters, and even the 148eaff2004Ssameeran joshitype of an allocatable can all be deferred to run time. 149eaff2004Ssameeran joshi(If you really want to learn all about modern Fortran, I suggest 150eaff2004Ssameeran joshithat you study everything that can be done with `ALLOCATABLE` data, 151eaff2004Ssameeran joshiand follow up all the references that are made in the documentation 152eaff2004Ssameeran joshifrom the description of `ALLOCATABLE` to other topics; it's a feature 153eaff2004Ssameeran joshithat interacts with much of the rest of the language.) 154eaff2004Ssameeran joshi 155271a7bb1SRichard Barton## I/O 156271a7bb1SRichard Barton 157eaff2004Ssameeran joshiFortran's input/output features are built into the syntax of the language, 158eaff2004Ssameeran joshirather than being defined by library interfaces as in C and C++. 159eaff2004Ssameeran joshiThere are means for raw binary I/O and for "formatted" transfers to 160eaff2004Ssameeran joshicharacter representations. 161eaff2004Ssameeran joshiThere are means for random-access I/O using fixed-size records as well as for 162eaff2004Ssameeran joshisequential I/O. 163eaff2004Ssameeran joshiOne can scan data from or format data into `CHARACTER` variables via 164eaff2004Ssameeran joshi"internal" formatted I/O. 165eaff2004Ssameeran joshiI/O from and to files uses a scheme of integer "unit" numbers that is 166eaff2004Ssameeran joshisimilar to the open file descriptors of UNIX; i.e., one opens a file 167eaff2004Ssameeran joshiand assigns it a unit number, then uses that unit number in subsequent 168eaff2004Ssameeran joshi`READ` and `WRITE` statements. 169eaff2004Ssameeran joshi 170eaff2004Ssameeran joshiFormatted I/O relies on format specifications to map values to fields of 171eaff2004Ssameeran joshicharacters, similar to the format strings used with C's `printf` family 172eaff2004Ssameeran joshiof standard library functions. 173eaff2004Ssameeran joshiThese format specifications can appear in `FORMAT` statements and 174eaff2004Ssameeran joshibe referenced by their labels, in character literals directly in I/O 175eaff2004Ssameeran joshistatements, or in character variables. 176eaff2004Ssameeran joshi 177eaff2004Ssameeran joshiOne can also use compiler-generated formatting in "list-directed" I/O, 178eaff2004Ssameeran joshiin which the compiler derives reasonable default formats based on 179eaff2004Ssameeran joshidata types. 180eaff2004Ssameeran joshi 181271a7bb1SRichard Barton## Subprograms 182271a7bb1SRichard Barton 183eaff2004Ssameeran joshiFortran has both `FUNCTION` and `SUBROUTINE` subprograms. 184eaff2004Ssameeran joshiThey share the same name space, but functions cannot be called as 185eaff2004Ssameeran joshisubroutines or vice versa. 186eaff2004Ssameeran joshiSubroutines are called with the `CALL` statement, while functions are 187eaff2004Ssameeran joshiinvoked with function references in expressions. 188eaff2004Ssameeran joshi 189eaff2004Ssameeran joshiThere is one level of subprogram nesting. 190eaff2004Ssameeran joshiA function, subroutine, or main program can have functions and subroutines 191eaff2004Ssameeran joshinested within it, but these "internal" procedures cannot themselves have 192eaff2004Ssameeran joshitheir own internal procedures. 193eaff2004Ssameeran joshiAs is the case with C++ lambda expressions, internal procedures can 194eaff2004Ssameeran joshireference names from their host subprograms. 195eaff2004Ssameeran joshi 196271a7bb1SRichard Barton## Modules 197271a7bb1SRichard Barton 198eaff2004Ssameeran joshiModern Fortran has good support for separate compilation and namespace 199eaff2004Ssameeran joshimanagement. 200eaff2004Ssameeran joshiThe *module* is the basic unit of compilation, although independent 201eaff2004Ssameeran joshisubprograms still exist, of course, as well as the main program. 202eaff2004Ssameeran joshiModules define types, constants, interfaces, and nested 203eaff2004Ssameeran joshisubprograms. 204eaff2004Ssameeran joshi 205eaff2004Ssameeran joshiObjects from a module are made available for use in other compilation 206eaff2004Ssameeran joshiunits via the `USE` statement, which has options for limiting the objects 207eaff2004Ssameeran joshithat are made available as well as for renaming them. 208eaff2004Ssameeran joshiAll references to objects in modules are done with direct names or 209eaff2004Ssameeran joshialiases that have been added to the local scope, as Fortran has no means 210eaff2004Ssameeran joshiof qualifying references with module names. 211eaff2004Ssameeran joshi 212271a7bb1SRichard Barton## Arguments 213271a7bb1SRichard Barton 214eaff2004Ssameeran joshiFunctions and subroutines have "dummy" arguments that are dynamically 215eaff2004Ssameeran joshiassociated with actual arguments during calls. 216eaff2004Ssameeran joshiEssentially, all argument passing in Fortran is by reference, not value. 217eaff2004Ssameeran joshiOne may restrict access to argument data by declaring that dummy 218eaff2004Ssameeran joshiarguments have `INTENT(IN)`, but that corresponds to the use of 219eaff2004Ssameeran joshia `const` reference in C++ and does not imply that the data are 220eaff2004Ssameeran joshicopied; use `VALUE` for that. 221eaff2004Ssameeran joshi 222eaff2004Ssameeran joshiWhen it is not possible to pass a reference to an object, or a sparse 223eaff2004Ssameeran joshiregular array section of an object, as an actual argument, Fortran 224eaff2004Ssameeran joshicompilers must allocate temporary space to hold the actual argument 225eaff2004Ssameeran joshiacross the call. 226eaff2004Ssameeran joshiThis is always guaranteed to happen when an actual argument is enclosed 227eaff2004Ssameeran joshiin parentheses. 228eaff2004Ssameeran joshi 229eaff2004Ssameeran joshiThe compiler is free to assume that any aliasing between dummy arguments 230eaff2004Ssameeran joshiand other data is safe. 231eaff2004Ssameeran joshiIn other words, if some object can be written to under one name, it's 232eaff2004Ssameeran joshinever going to be read or written using some other name in that same 233eaff2004Ssameeran joshiscope. 234eaff2004Ssameeran joshi``` 235eaff2004Ssameeran joshi SUBROUTINE FOO(X,Y,Z) 236eaff2004Ssameeran joshi X = 3.14159 237eaff2004Ssameeran joshi Y = 2.1828 238eaff2004Ssameeran joshi Z = 2 * X ! CAN BE FOLDED AT COMPILE TIME 239eaff2004Ssameeran joshi END 240eaff2004Ssameeran joshi``` 241eaff2004Ssameeran joshiThis is the opposite of the assumptions under which a C or C++ compiler must 242eaff2004Ssameeran joshilabor when trying to optimize code with pointers. 243eaff2004Ssameeran joshi 244271a7bb1SRichard Barton## Overloading 245271a7bb1SRichard Barton 246eaff2004Ssameeran joshiFortran supports a form of overloading via its interface feature. 247eaff2004Ssameeran joshiBy default, an interface is a means for specifying prototypes for a 248eaff2004Ssameeran joshiset of subroutines and functions. 249eaff2004Ssameeran joshiBut when an interface is named, that name becomes a *generic* name 250eaff2004Ssameeran joshifor its specific subprograms, and calls via the generic name are 251eaff2004Ssameeran joshimapped at compile time to one of the specific subprograms based 252eaff2004Ssameeran joshion the types, kinds, and ranks of the actual arguments. 253eaff2004Ssameeran joshiA similar feature can be used for generic type-bound procedures. 254eaff2004Ssameeran joshi 255eaff2004Ssameeran joshiThis feature can be used to overload the built-in operators and some 256eaff2004Ssameeran joshiI/O statements, too. 257eaff2004Ssameeran joshi 258271a7bb1SRichard Barton## Polymorphism 259271a7bb1SRichard Barton 260eaff2004Ssameeran joshiFortran code can be written to accept data of some derived type or 261eaff2004Ssameeran joshiany extension thereof using `CLASS`, deferring the actual type to 262eaff2004Ssameeran joshiexecution, rather than the usual `TYPE` syntax. 263eaff2004Ssameeran joshiThis is somewhat similar to the use of `virtual` functions in c++. 264eaff2004Ssameeran joshi 265eaff2004Ssameeran joshiFortran's `SELECT TYPE` construct is used to distinguish between 266eaff2004Ssameeran joshipossible specific types dynamically, when necessary. It's a 267eaff2004Ssameeran joshilittle like C++17's `std::visit()` on a discriminated union. 268eaff2004Ssameeran joshi 269271a7bb1SRichard Barton## Pointers 270271a7bb1SRichard Barton 271eaff2004Ssameeran joshiPointers are objects in Fortran, not data types. 272eaff2004Ssameeran joshiPointers can point to data, arrays, and subprograms. 273eaff2004Ssameeran joshiA pointer can only point to data that has the `TARGET` attribute. 274eaff2004Ssameeran joshiOutside of the pointer assignment statement (`P=>X`) and some intrinsic 275eaff2004Ssameeran joshifunctions and cases with pointer dummy arguments, pointers are implicitly 276eaff2004Ssameeran joshidereferenced, and the use of their name is a reference to the data to which 277eaff2004Ssameeran joshithey point instead. 278eaff2004Ssameeran joshi 279eaff2004Ssameeran joshiUnlike C, a pointer cannot point to a pointer *per se*, nor can they be 280eaff2004Ssameeran joshiused to implement a level of indirection to the management structure of 281eaff2004Ssameeran joshian allocatable. 282eaff2004Ssameeran joshiIf you assign to a Fortran pointer to make it point at another pointer, 283eaff2004Ssameeran joshiyou are making the pointer point to the data (if any) to which the other 284eaff2004Ssameeran joshipointer points. 285eaff2004Ssameeran joshiSimilarly, if you assign to a Fortran pointer to make it point to an allocatable, 286eaff2004Ssameeran joshiyou are making the pointer point to the current content of the allocatable, 287eaff2004Ssameeran joshinot to the metadata that manages the allocatable. 288eaff2004Ssameeran joshi 289eaff2004Ssameeran joshiUnlike allocatables, pointers do not deallocate their data when they go 290eaff2004Ssameeran joshiout of scope. 291eaff2004Ssameeran joshi 292eaff2004Ssameeran joshiA legacy feature, "Cray pointers", implements dynamic base addressing of 293eaff2004Ssameeran joshione variable using an address stored in another. 294eaff2004Ssameeran joshi 295271a7bb1SRichard Barton## Preprocessing 296271a7bb1SRichard Barton 297eaff2004Ssameeran joshiThere is no standard preprocessing feature, but every real Fortran implementation 298eaff2004Ssameeran joshihas some support for passing Fortran source code through a variant of 299eaff2004Ssameeran joshithe standard C source preprocessor. 300eaff2004Ssameeran joshiSince Fortran is very different from C at the lexical level (e.g., line 301eaff2004Ssameeran joshicontinuations, Hollerith literals, no reserved words, fixed form), using 302eaff2004Ssameeran joshia stock modern C preprocessor on Fortran source can be difficult. 303eaff2004Ssameeran joshiPreprocessing behavior varies across implementations and one should not depend on 304eaff2004Ssameeran joshimuch portability. 305eaff2004Ssameeran joshiPreprocessing is typically requested by the use of a capitalized filename 306eaff2004Ssameeran joshisuffix (e.g., "foo.F90") or a compiler command line option. 307eaff2004Ssameeran joshi(Since the F18 compiler always runs its built-in preprocessing stage, 308eaff2004Ssameeran joshino special option or filename suffix is required.) 309eaff2004Ssameeran joshi 310271a7bb1SRichard Barton## "Object Oriented" Programming 311271a7bb1SRichard Barton 312eaff2004Ssameeran joshiFortran doesn't have member functions (or subroutines) in the sense 313eaff2004Ssameeran joshithat C++ does, in which a function has immediate access to the members 314eaff2004Ssameeran joshiof a specific instance of a derived type. 315eaff2004Ssameeran joshiBut Fortran does have an analog to C++'s `this` via *type-bound 316eaff2004Ssameeran joshiprocedures*. 317eaff2004Ssameeran joshiThis is a means of binding a particular subprogram name to a derived 318eaff2004Ssameeran joshitype, possibly with aliasing, in such a way that the subprogram can 319eaff2004Ssameeran joshibe called as if it were a component of the type (e.g., `X%F(Y)`) 320eaff2004Ssameeran joshiand receive the object to the left of the `%` as an additional actual argument, 321eaff2004Ssameeran joshiexactly as if the call had been written `F(X,Y)`. 322eaff2004Ssameeran joshiThe object is passed as the first argument by default, but that can be 323eaff2004Ssameeran joshichanged; indeed, the same specific subprogram can be used for multiple 324eaff2004Ssameeran joshitype-bound procedures by choosing different dummy arguments to serve as 325eaff2004Ssameeran joshithe passed object. 326eaff2004Ssameeran joshiThe equivalent of a `static` member function is also available by saying 327eaff2004Ssameeran joshithat no argument is to be associated with the object via `NOPASS`. 328eaff2004Ssameeran joshi 329eaff2004Ssameeran joshiThere's a lot more that can be said about type-bound procedures (e.g., how they 330eaff2004Ssameeran joshisupport overloading) but this should be enough to get you started with 331eaff2004Ssameeran joshithe most common usage. 332eaff2004Ssameeran joshi 333271a7bb1SRichard Barton## Pitfalls 334271a7bb1SRichard Barton 335eaff2004Ssameeran joshiVariable initializers, e.g. `INTEGER :: J=123`, are _static_ initializers! 336eaff2004Ssameeran joshiThey imply that the variable is stored in static storage, not on the stack, 337eaff2004Ssameeran joshiand the initialized value lasts only until the variable is assigned. 338eaff2004Ssameeran joshiOne must use an assignment statement to implement a dynamic initializer 339eaff2004Ssameeran joshithat will apply to every fresh instance of the variable. 340eaff2004Ssameeran joshiBe especially careful when using initializers in the newish `BLOCK` construct, 341eaff2004Ssameeran joshiwhich perpetuates the interpretation as static data. 342eaff2004Ssameeran joshi(Derived type component initializers, however, do work as expected.) 343eaff2004Ssameeran joshi 344eaff2004Ssameeran joshiIf you see an assignment to an array that's never been declared as such, 345eaff2004Ssameeran joshiit's probably a definition of a *statement function*, which is like 346eaff2004Ssameeran joshia parameterized macro definition, e.g. `A(X)=SQRT(X)**3`. 347eaff2004Ssameeran joshiIn the original Fortran language, this was the only means for user 348eaff2004Ssameeran joshifunction definitions. 349eaff2004Ssameeran joshiToday, of course, one should use an external or internal function instead. 350eaff2004Ssameeran joshi 351eaff2004Ssameeran joshiFortran expressions don't bind exactly like C's do. 352eaff2004Ssameeran joshiWatch out for exponentiation with `**`, which of course C lacks; it 353eaff2004Ssameeran joshibinds more tightly than negation does (e.g., `-2**2` is -4), 354eaff2004Ssameeran joshiand it binds to the right, unlike what any other Fortran and most 355eaff2004Ssameeran joshiC operators do; e.g., `2**2**3` is 256, not 64. 356eaff2004Ssameeran joshiLogical values must be compared with special logical equivalence 357eaff2004Ssameeran joshirelations (`.EQV.` and `.NEQV.`) rather than the usual equality 358eaff2004Ssameeran joshioperators. 359eaff2004Ssameeran joshi 360eaff2004Ssameeran joshiA Fortran compiler is allowed to short-circuit expression evaluation, 361eaff2004Ssameeran joshibut not required to do so. 362eaff2004Ssameeran joshiIf one needs to protect a use of an `OPTIONAL` argument or possibly 363eaff2004Ssameeran joshidisassociated pointer, use an `IF` statement, not a logical `.AND.` 364eaff2004Ssameeran joshioperation. 365eaff2004Ssameeran joshiIn fact, Fortran can remove function calls from expressions if their 366eaff2004Ssameeran joshivalues are not required to determine the value of the expression's 367eaff2004Ssameeran joshiresult; e.g., if there is a `PRINT` statement in function `F`, it 368eaff2004Ssameeran joshimay or may not be executed by the assignment statement `X=0*F()`. 369eaff2004Ssameeran joshi(Well, it probably will be, in practice, but compilers always reserve 370eaff2004Ssameeran joshithe right to optimize better.) 371eaff2004Ssameeran joshi 372eaff2004Ssameeran joshiUnless they have an explicit suffix (`1.0_8`, `2.0_8`) or a `D` 373eaff2004Ssameeran joshiexponent (`3.0D0`), real literal constants in Fortran have the 374eaff2004Ssameeran joshidefault `REAL` type -- *not* `double` as in the case in C and C++. 375eaff2004Ssameeran joshiIf you're not careful, you can lose precision at compilation time 376eaff2004Ssameeran joshifrom your constant values and never know it. 377