Comparing with matlab/python, fortran program runs faster for loops after compiling. Although fortran is an old language, it is capable of parallel computing and still wildly used in scientific computing today.
Hello world
First we introduce a minibird example of the fortran programming for a fresh start.
Helloworld source code
Here we demonstrate a minibird Helloworld example of the fotran programming language.
1 | ! helloworld.f90 test |
Complile and run fortran on linux
1 | ifort/gfortran helloworld.f90 -o hello.out |
After execute these command in terminal, you should be able to view the printed output in your terminal window. Here, ifort is for intel fortran compiler, gfortran is for GNU fortran compiler.
Function definition
Singular value function
Here we demonstrate the definition of a single value function z = f(x, y).
1 | program main |
Multivalue function
Here we demonstrate the definition of a multivalue function [c, d] = f(a, b). Output of more than one variable, you should use subroutine other than function. Function format in fortran can only have one output.
1 | program main |
Optional inputs
The optional inputs of fortran is a bit like python, where you can given the inputs of fortran variable without consider their correct order [1].
1 | module procs |
All the three methods to call the function cals() returns the same result regardless of the calling order of the arguments.
Reference
[1] S. Chapman, 2007, “fotran 95/2003 for scientists and engineers”, 3rd edition, McGraw-Hill press…