Versions Compared
Key
- This line was added.
- This line was removed.
- Formatting was changed.
This page contains a number of clarifications and explanations that will hopefully help you get a better understanding of Fortran
"Implicit none"
The line 'implicit none' is needed in Fortran to avoid obscure errors when defining variables. In earlier Fortran versions all variables were implicitly given as floats (real) unless the first letter in the name between i and n. 'implicit none' makes sure all variables have to be given-type explicit. Remember to add this line to every program, function and sub-routine. It is a very common form of error.
Subroutine vs. function
The difference between a subroutine and a function can be confusing, as they often can be implemented in similar ways to do the same job. The biggest difference is that subroutines work as pass-by-reference, while functions are pass-by-value. Se the example below for clarity. Many prefer subroutines because of its simpler syntax. Functions has a few syntax requirements. As you see inn the example under the function has to be defined with correct return type (this case float number).
Expand | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
In this example we will compare the implementation of subroutines vs. functions.
As you may notice we run functions on a "traditional" maner, while subroutines require the command "call". The implementation of the subroutine is pretty straight forward:
Intent(in) is telling the compiler that b is an un-changeable variable. If your run this script you will get following output:
|
Recursive functions
In Fortran subroutines and functions that does a recursive call on itself needs to be labeled "recursive function" in its declaration. It's easy to forget when handling programs that intuitively doesn't look like a recursive function. For an example see the calculator application. Below is a simple program for writing the Fibonacci numbers.
Expand | ||||
---|---|---|---|---|
| ||||
|
Higher precision
Standard precision for real numbers in FORTRAN i 6 decimal places. For some application or if you are dividing by very small numbers higher precision is needed to avoid numerical error. As shown in the example below we first create a new kind for real numbers (with precision set to 12 decimals). Then create real variables with the new kind we made.
Expand | |||||
---|---|---|---|---|---|
| |||||
|
Panel | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
|
BibTeX Display Table |
---|