--- marp: true title: Introduction to structured programming with Fortran author: P.Y. Barriat description: https://dev.to/nikolab/complete-list-of-github-markdown-emoji-markup-5aia backgroundImage: url('assets/back.png') _backgroundImage: url('assets/garde.png') footer: 09/11/2022 | Introduction to structured programming with Fortran _footer: "" paginate: true _paginate: false --- Introduction to structured programming with `Fortran` === https://gogs.elic.ucl.ac.be/pbarriat/learning-fortran ![h:150](assets/fortran_logo.png) ### Pierre-Yves Barriat ##### November 09, 2022 ###### CISM/CÉCI Training Sessions --- # Fortran : shall we start ? - You know already one computer language ? - You understand the very basic programming concepts : - What is a variable, an assignment, function call, etc.? - Why do I have to compile my code? - What is an executable? - You (may) already know some Fortran ? - How to proceed from old Fortran, to much more modern languages like Fortran 90/2003 ? --- # Why to learn Fortran ? - Because of the execution `speed` of a program - Well suited for numerical computations : more than 45% of scientific applications are in Fortran - `Fast` code : compilers can optimize well - Optimized `numerical libraries` available - Fortran is a `simple` langage and it is (kind-of) `easy to learn` --- # Fortran is simple - **We want to get our science done! Not learn languages!** - How easy/difficult is it really to learn Fortran ? - The concept is easy: *variables, operators, controls, loops, subroutines/functions* - **Invest some time now, gain big later!** --- # History **FOR**mula **TRAN**slation > invented 1954-8 by John Backus and his team at IBM - FORTRAN 66 (ISO Standard 1972) - FORTRAN 77 (1978) - Fortran 90 (1991) - Fortran 95 (1997) - Fortran 2003 (2004) → `"standard" version` - Fortran 2008 (2010) - Fortran 2018 (11/2018) --- # Starting with Fortran 77 - Old Fortran provides only the absolute minimum! - Basic features : data containers (integer, float, ...), arrays, basic operators, loops, I/O, subroutines and functions - But this version has flaws: no dynamic memory allocation, old & obsolete constructs, “spaghetti” code, etc. - Is that enough to write code ? --- # Fortran 77 → Fortran >90 - If Fortran 77 is so simple, why is it then so difficult to write good code? - Is simple really better? ⇒ Using a language allows us to express our thoughts (on a computer) - A more sophisticated language allows for more complex thoughts - More language elements to get organized ⇒ Fortran 90/95/2003 (recursive, OOP, etc) --- # How to Build a FORTRAN Program FORTRAN is a compiled language (like C) so the source code (what you write) must be converted into machine code before it can be executed (e.g. Make command) ![h:400](assets/build_fortran.png) --- # FORTRAN 77 Format This version requires a fixed format for programs ![h:300](assets/f77_format.png) - max length variable names is 6 characters - alphanumeric only, must start with a letter - character strings are case sensitive --- # FORTRAN >90 Format Versions >90 relaxe these requirements: - comments following statements (! delimiter) - long variable names (31 characters) - containing only letters, digits or underscore - max row length is 132 characters - can be max 39 continuation lines - if a line is ended with ampersand (&), the line continues onto the next line - semicolon (;) as a separator between statements on a single line - allows free field input --- # Program Organization Most FORTRAN programs consist of a main program and one or more subprograms There is a fixed order: ```Fortran90 Heading Declarations Variable initializations Program code Format statements Subprogram definitions (functions & subroutines) ``` --- # Data Type Declarations Basic data types are : - `INTEGER` : integer numbers (+/-) - `REAL` : floating point numbers - `DOUBLE PRECISION` : extended precision floating point - `CHARACTER*n` : string with up to **n** characters - `LOGICAL` : takes on values `.TRUE.` or `.FALSE.` --- # Data Type Declarations `INTEGER` and `REAL` can specify number of bytes to use - Default is: `INTEGER*4` and `REAL*4` - `DOUBLE PRECISION` is same as `REAL*8` Arrays of any type must be declared: - `DIMENSION A(3,5)` - declares a 3 x 5 array - `CHARACTER*30 NAME(50)` - directly declares a character array with 30 character strings in each element --- # Data Type Declarations FORTRAN >90 allows user defined types ```fortran TYPE my_variable character(30) :: name integer :: id real(8) :: value integer, dimension(3,3) :: dimIndex END TYPE variable type(my_variable) var var%name = "salinity" var%id = 1 ``` --- # Implicit vs Explicit Declarations By default, an implicit type is assumed depending on the first letter of the variable name: - `A-H, O-Z` define REAL variables - `I-N` define INTEGER variables Can use the IMPLICIT statement: ```fortran IMPLICIT REAL (A-Z) ``` > makes all variables REAL if not declared --- # Implicit vs Explicit Declarations ```fortran IMPLICIT CHARACTER*2 (W) ``` > makes variables starting with W be 2-character strings ```fortran IMPLICIT DOUBLE PRECISION (D) ``` > makes variables starting with D be double precision **Good habit**: force explicit type declarations ```fortran IMPLICIT NONE ``` > user must explicitly declare all variable types --- # Assignment Statements **Old** assignment statement: `