# Fortran/Fortran overview

Part of the Fortran WikiBook
This section is a quick reference on changing from other languages to Fortran.

## Common Mistakes

Listed below are some common mistakes made when using Fortran by users of C or Java:
* Perhaps the most common mistake when moving from one language to another is to try to make the new language look or act too much like the previous language in use. When switching to Fortran (for whatever reason) consider doing things the Fortran way, rather than as you would in some other language.
* In Fortran, variables need not be declared. A new variable beginning with I through N (the "in" group) is an INTEGER. A variable beginning with some other letter is a REAL. So, the disadvantage is that a typo could result in a new variable. The advantage is that coding is fast and easy, in that for most cases, no variables need be declared. If you want Fortran to act like declaration required languages, use IMPLICIT NONE at the top of each program, function, and subroutine.
* Arrays are indexed from 1 to N, not 0 to N−1.
* Arguments passed to functions and subroutines are passed by reference and not by value. Additionally, languages like C typically push each of their arguments onto the stack. Many (if not most) Fortran implementations build an array of argument addresses, and push the address of that array (only) onto the stack. This uses slightly more storage, but less executed code if you average more than 1 argument. The C-user error occurs when you think you know the addresses of argument references.
* Note that arguments may need to be declared, and might not, as per the rules above.
* For fixed form code, the first 6 columns have special meanings, and you can only use the first 72 columns.
* Trying to use allocation too much. When in Fortran, try to use static allocation of arrays whenever possible.
* Thinking you know how big a variable is—Here are the rules: LOGICAL can be any number of bytes. INTEGER is at least as big as LOGICAL. REAL is at least as big as INTEGER. DOUBLE PRECISION is at least as big as REAL. And finally, COMPLEX is twice as big as REAL. Typically, the sizes are 1, 4, 4, 8, and 8 bytes, respectively, but don't rely on that if you're writing portable code. A related mistake is thinking that LOGICAL*4 or REAL*8 gives you data 4 or 8 bytes wide. The number following the * is a suggestion, not a requirement.

## Variables

C / C++
Fortran (with two examples of allocation and deallocation—don't use both at once)
Caveat: Fortran's arrays are more flexible than plain C arrays. By default they are indexed from 1 not 0.
They may be declared to be indexed by different means:
means you can use A(10) as well as A(-2).
Multidimensional arrays should be declared this way:
You will have 5*7*10 elements from A(1,1,1) to A(5,7,10). Note memory is laid out in a way unique to Fortran. The element in memory following A(1,1,1) is A(2,1,1), and not A(1,1,2).
You can use parts of array this way:
will act as
Which is the same as the archaic but shorter form

## Functions & Subroutines

In Fortran, FUNCTIONs return a value whereas SUBROUTINEs do not. In C/C++ this is simply the distinction between returning a value or returning void. In Fortran, the name of the function is assigned to return a value from a function. When calling, a subroutine is called with the CALL keyword whereas a function is called as in C/C++. When there are no arguments, an empty parenthesis is not needed.
C/C++:
Fortran:
C/C++:
Fortran:

## Control structures
### if → if, then, else, end if

C / C++
Fortran

### switch → select case, case, end select

C/C++
Fortran

## Iteration
### for → do, end do

C/C++
Fortran

## External links

* Michael Metcalf's Fortran 90 CNL Articles
* C-Fortran interface