Fortran for C Programmers
a year ago
- #C/C++
- #Programming
- #Fortran
- Fortran is designed for C/C++ programmers to quickly adapt, focusing on essential features and avoiding common pitfalls.
- Fortran standards have evolved through versions like '66, '77, '90, '95, 2003, 2008, and 2018, emphasizing forward compatibility.
- Fortran supports two source forms: fixed-form (.f) and free-form (.f90), with no reserved words and case insensitivity.
- Variables starting with I-N are implicitly INTEGER; others are REAL, but these rules can be modified.
- Arrays must be declared explicitly; parentheses denote both array references and function calls.
- Fortran includes many intrinsic functions, available without declaration, with names reflecting implicit typing rules.
- Modern Fortran uses modules for declaring types, data, and subprogram interfaces, alongside legacy mechanisms.
- Key Fortran terms include 'Association', 'Derived type' (similar to C++ class), and 'Generic' (overloaded function).
- Built-in data types: INTEGER, REAL, COMPLEX, LOGICAL, CHARACTER, with 'kind' parameters for non-portable byte sizes.
- User-defined types can be created, similar to C structs, with inheritance, destructors, and default initial values.
- Arrays in Fortran are multidimensional, with rank indicating dimensions, stored with the last subscript having the largest stride.
- ALLOCATABLE data is dynamically allocated, automatically deallocated when out of scope, similar to C++ std::vector.
- I/O is built into Fortran syntax, with formatted and unformatted options, and uses unit numbers similar to UNIX file descriptors.
- Fortran has FUNCTION and SUBROUTINE subprograms, with one level of nesting and internal procedures referencing host names.
- Modules support separate compilation and namespace management, with USE statements for accessing module objects.
- Argument passing is by reference, with INTENT(IN) for read-only arguments, and VALUE for pass-by-value.
- Overloading is supported via named interfaces, allowing generic names for subprograms based on argument types.
- Polymorphism is possible with CLASS, deferring actual types to runtime, similar to C++ virtual functions.
- Pointers in Fortran are objects, not types, implicitly dereferenced, and cannot point to other pointers or allocatable metadata.
- Preprocessing is implementation-dependent, often using C preprocessors, with varying support across compilers.
- Type-bound procedures provide an analog to C++ member functions, with the object passed as an additional argument.
- Pitfalls include static initializers for variables, statement functions, and differences in expression evaluation compared to C.