|
| 1 | +--- |
| 2 | +title: 'Introduction' |
| 3 | +teaching: 10 |
| 4 | +exercises: 10 |
| 5 | +--- |
| 6 | + |
| 7 | +::::::::::::::::::::::::::::::::::::: questions |
| 8 | + |
| 9 | +- What is Fortran? |
| 10 | + |
| 11 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
| 12 | + |
| 13 | +::::::::::::::::::::::::::::::::::::: objectives |
| 14 | + |
| 15 | +- Understand some of Fortrans colourful history |
| 16 | + |
| 17 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
| 18 | + |
| 19 | +## Introduction |
| 20 | +A very simple program might be: |
| 21 | + |
| 22 | +```fortran |
| 23 | +program example1 |
| 24 | +
|
| 25 | + ! An example program prints "Hello World" to the screen |
| 26 | +
|
| 27 | + print * , "Hello World" |
| 28 | +
|
| 29 | +end program example1 |
| 30 | +``` |
| 31 | + |
| 32 | +Formally, a Fortran program consists of one or more lines made up of |
| 33 | +Fortran _statements_. Line breaks are significant (e.g., there are |
| 34 | +no semi-colons `;` required here). |
| 35 | + |
| 36 | +Comments are introduced with an exclamation mark `!`, and may trail |
| 37 | +other statements. |
| 38 | + |
| 39 | +The `program` statement is roughly doing the equivalent job of `main()` |
| 40 | +in C/C++. However, note there is not (and must not be) a return statement. |
| 41 | + |
| 42 | +::::::::::::::::::::::::::::::::::::: challenge |
| 43 | + |
| 44 | +## Compiling your first program |
| 45 | + |
| 46 | +Check now you can compile and run the first example program `example1.f90`. |
| 47 | + |
| 48 | +:::::::::::::::: solution |
| 49 | + |
| 50 | +Using `ftn`: |
| 51 | + |
| 52 | +```shell |
| 53 | +$ something something something |
| 54 | +``` |
| 55 | + |
| 56 | +::::::::::::::::::::::::: |
| 57 | +::::::::::::::::::::::::::::::::::::::::::::::: |
| 58 | + |
| 59 | +### Formal description |
| 60 | + |
| 61 | +``` |
| 62 | + [ program [program-name] ] |
| 63 | + [ specification-part ] |
| 64 | + [ exectuable-part ] |
| 65 | + [ contains |
| 66 | + internal-subprogram-part ] |
| 67 | + end [program-name] |
| 68 | +``` |
| 69 | + |
| 70 | +Optional components are represented with square brackets `[...]`. It |
| 71 | +follows that the shortest standard-conforming program will be (see |
| 72 | +`example2.f90`): |
| 73 | + |
| 74 | +```fortran |
| 75 | +end |
| 76 | +``` |
| 77 | + |
| 78 | +If the `program-name` is present, it must be at both the beginning and |
| 79 | +the end, and must be the same in both places. |
| 80 | + |
| 81 | +We will return to the `contains` statement in the context of modules. |
| 82 | + |
| 83 | +## `print` statement |
| 84 | + |
| 85 | +In general |
| 86 | + |
| 87 | +```fortran |
| 88 | + print format [ , output-item-list ] |
| 89 | +``` |
| 90 | + |
| 91 | +where the `format` is a format specifier (discussed later) and the |
| 92 | +`output-item-list` is a comma-separated list of values/variables |
| 93 | +to be printed to the standard output. |
| 94 | + |
| 95 | +If the format is a `*` (a so-called free-format) the implementation |
| 96 | +is allowed to apply a default format for a given type of item. |
| 97 | + |
| 98 | + |
| 99 | +## Alternative |
| 100 | + |
| 101 | +Consider the following program (available as `example3.f90`): |
| 102 | + |
| 103 | +```fortran |
| 104 | +program example3 |
| 105 | +
|
| 106 | + use iso_fortran_env, only : output_unit |
| 107 | +
|
| 108 | + write (output_unit, *) "Hello ", "world" |
| 109 | +
|
| 110 | +end program example3 |
| 111 | +``` |
| 112 | + |
| 113 | +This example shows a more general way to provide some output. Here we are |
| 114 | +also going to employ the `use` statement to import a symbol from the |
| 115 | +(intrinsic) module `iso_fortran_env`. The symbol is `output_unit` which |
| 116 | +identifies the default standard output (cf. `stdout`). |
| 117 | + |
| 118 | + |
| 119 | +### `use` statement |
| 120 | + |
| 121 | +Formally, |
| 122 | + |
| 123 | +```fortran |
| 124 | + use [[ , module-nature] ::] module-name [ , only : [only-list]] |
| 125 | +``` |
| 126 | + |
| 127 | +If `module-nature` is present, it must be either `intrinsic` or |
| 128 | +`non_intrinsic`. The implementation must provide certain intrinsic |
| 129 | +modules such `iso_fortran_env`. |
| 130 | + |
| 131 | +There is no formal namespace mechanism in Fortran (cf. C++), so |
| 132 | +restrictions on which symbols are visible can be made via an optional |
| 133 | +`only-list`. If there is no `only-list` then all the public symbols |
| 134 | +from `module-name` will be visible. |
| 135 | + |
| 136 | + |
| 137 | +### `write` statement |
| 138 | + |
| 139 | +Formally, |
| 140 | + |
| 141 | +```fortran |
| 142 | + write (io-control-spec-list) [output-item-list] |
| 143 | +``` |
| 144 | + |
| 145 | +where the `output-item-list` is a comma separated list of items to |
| 146 | +be output. The `io-control-spec-list` has a large number of potential |
| 147 | +arguments (again comma separated). For formatted output, these must |
| 148 | +include at least a unit number and a format: |
| 149 | + |
| 150 | +```fortran |
| 151 | + write ([unit = ] io-unit, [fmt = ] format) [output-item-list] |
| 152 | +``` |
| 153 | + |
| 154 | +where the `io-unit` is a valid integer unit number, and the `format` |
| 155 | +is a format-specifier (as for `print`). |
| 156 | + |
| 157 | +Examples are |
| 158 | + |
| 159 | +```fortran |
| 160 | + write (unit = output_unit, fmt = *) |
| 161 | + write (output_unit, *) |
| 162 | + write (*, *) |
| 163 | +``` |
| 164 | + |
| 165 | +C programmers looking for a new-line like symbol will notice that none |
| 166 | +has appeared so far. The default situation is that both `print` and |
| 167 | +`write` generate a new-line automatically. The `*` symbol in the context |
| 168 | +of `io-unit` is a default output unit (usually the screen). |
| 169 | + |
| 170 | +We will return to the `write` statement and format-specifiers in more |
| 171 | +detail in the context of i/o to external files. |
| 172 | + |
| 173 | +## Some comments on style |
| 174 | + |
| 175 | +Modern Fortran is not case sensitive. Older versions required capitals, |
| 176 | +a style which has persisted to the present day in some places. So you |
| 177 | +may see things such as |
| 178 | + |
| 179 | +```fortran |
| 180 | +PROGRAM example1 |
| 181 | +
|
| 182 | + PRINT *, "Hello World" |
| 183 | +
|
| 184 | +END PROGRAM example1 |
| 185 | +``` |
| 186 | + |
| 187 | +As modern etiquette tends to regard capitals as shouting, this can cause |
| 188 | +some strain. In addition, as the compiler will accept mixed |
| 189 | +case, an additional tool would be required to enforce style (if |
| 190 | +enforcement was wanted). |
| 191 | + |
| 192 | +This course therefore prefers an all lower-case style. |
| 193 | + |
| 194 | +::::::::::::::::::::::::::::::::::::: challenge |
| 195 | + |
| 196 | +## Writing your first program |
| 197 | + |
| 198 | +Write a program which prints out the actual values of the symbols |
| 199 | +`output_unit`, `error_unit`, and `input_unit` |
| 200 | +(all from `iso_fortran_env`) to the screen. |
| 201 | + |
| 202 | +If you haven't used the `only` clause in your `use iso_fortran_env`, |
| 203 | +add it now. What happens to the results if you miss out one of the |
| 204 | +symbols referenced from the `only` clause? This behaviour will be |
| 205 | +explained in the following section. |
| 206 | + |
| 207 | +:::::::::::::::: solution |
| 208 | + |
| 209 | +A version of this program is available as `exercise1.f90`. |
| 210 | + |
| 211 | +::::::::::::::::::::::::: |
| 212 | +::::::::::::::::::::::::::::::::::::::::::::::: |
| 213 | + |
| 214 | +::::::::::::::::::::::::::::::::::::: keypoints |
| 215 | + |
| 216 | +- A Fortran program is made up of one or more _statements_ which are separated by line breaks |
| 217 | +- Comments are declared with an `!` and may trail other statements |
| 218 | + |
| 219 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
| 220 | + |
0 commit comments