EMZETT.
Login

Kurz: program haupt use name implicit none real :: y y = funktion(3.0) print *, y end program haupt ```

Teil des Kurses Fortran

Kapitel 8 von 8 im Kurs Fortran (Abschnitt „Nachschlagen“). Mit Fortschritt, Quiz und Zertifikat auf der Lernseite.

Grundgerüst

module name
  implicit none
  private
  public :: funktion
contains
  function funktion(x) result(r)
    real, intent(in) :: x
    real :: r
    r = x * 2
  end function
end module name
 
program haupt
  use name
  implicit none
  real :: y
  y = funktion(3.0)
  print *, y
end program haupt

Datentypen und Deklaration

DeklarationBedeutung
integer :: iGanzzahl
real(real64) :: xdoppelte Genauigkeit
complex :: zkomplex
logical :: okWahrheitswert
character(len=20) :: sText fester Länge
real :: v(10), m(3, 3)Arrays
real, allocatable :: a(:)dynamisches Array
integer, parameter :: n = 10Konstante
type :: name ... end typeeigener Typ

Operatoren

+ - * / ** · == /= < <= > >= · .and. .or. .not. .eqv. · // (Strings) · % (Typkomponente) · => (Zeiger/Prozedurbindung)

Eingebaute Funktionen

GebietFunktionen
Mathematikabs sqrt exp log log10 sin cos tan asin acos atan atan2 sinh mod modulo floor ceiling nint
Typenint real dble cmplx char ichar kind selected_real_kind
Arrayssize shape sum product maxval minval maxloc minloc count any all pack merge reshape transpose matmul dot_product cshift spread
Zeichenlen len_trim trim adjustl adjustr index scan verify repeat
Zufall/Zeitrandom_number random_seed cpu_time system_clock date_and_time

Häufige Compilerfehler

MeldungUrsache
Error: Symbol 'x' at (1) has no IMPLICIT typeVariable nicht deklariert (mit implicit none Pflicht)
Error: Type mismatch in argumentfalsche Argumenttypen oder fehlendes Modul (use)
Error: Unclassifiable statementSyntaxfehler, oft fehlendes then oder end
Error: Expected a right parenthesisKlammern nicht ausgeglichen
Program received signal SIGSEGVZugriff außerhalb von Arrays (-fcheck=bounds hilft) oder nicht allokiertes Array
Fortran runtime error: End of recordEingabe/Format passt nicht
Ergebnisse unterscheiden sich leichtGleitkomma-Rundung, real statt real64
Warning: Possible change of value in conversionreal und real64 gemischt, Literal 1.0d0 oder _real64

Kompiler-Optionen (gfortran)

gfortran -O2 -Wall -Wextra -fcheck=all -g -std=f2018 programm.f90 -o programm   # Entwicklung
gfortran -O3 -march=native -fopenmp programm.f90 -o programm                     # schnell und parallel
gfortran -c modul.f90                                                            # nur Objektdatei und .mod
gfortran modul.o haupt.f90 -o programm -llapack -lblas                           # mit Bibliotheken

Reihenfolge beachten: Module müssen vor den Dateien übersetzt werden, die sie benutzen. Build-Werkzeuge: make, CMake, fpm (fpm run, fpm test).

Weiterlernen

  • fortran-lang.org (Tutorials, Paketindex, Community), Modern Fortran Explained
  • Stdlib (Fortran Standard Library), BLAS/LAPACK, FFTW, netCDF
  • Übungen: Project Euler, Rosetta Code (Fortran), Advent of Code

Merke

  • implicit none immer; Module statt common-Blöcken; intent bei jedem Argument
  • Array-Ausdrücke statt Schleifen; links schnellster Index
  • real64, Toleranzvergleiche; -fcheck=all beim Entwickeln

Quiz zur Selbstkontrolle

Weiter im Kurs

Zurück: Numerik, Parallelrechnen und Hochleistung

Alle Kapitel: Fortran im Überblick