{ "cells": [ { "cell_type": "markdown", "id": "21326c2d-1aeb-45f5-a4f8-ce49901e15ba", "metadata": {}, "source": [ "\n", "" ] }, { "cell_type": "markdown", "id": "210449e5-31bb-4323-b21b-0be4a1b02d7a", "metadata": {}, "source": [ "# Types and Kinds\n", "\n", "\n", "```{contents}\n", ":local:\n", "```\n", "\n", "INTRO HERE?\n", "\n", "## Types\n", "\n", "There are five types in Fortran. They are:\n", "\n", "| Type | example |\n", "| ------------ | -------------------- |\n", "| integer | `1` |\n", "| real (float) | `1.0` or `1.2d-11` |\n", "| character | `'text'` |\n", "| complex | `( 1.0, 1.0 )` |\n", "| logical | `.true.` or `.false` |\n", "\n", "## Declaring Variables\n", "\n", "Variables can be declared using the `::` syntax:\n", "\n", "```fortran\n", "integer :: i, j ! simple counters for looping\n", "real :: my_height\n", "```\n", "\n", "Here we declared two integer variables, then a real variable. Notice how the `::` are aligned for readability. Variables names can contain letters, numbers, and `_` only. They must start with a letter.\n", "\n", "### implicit none\n", "\n", "Fortran assumes that the single character variables i through n will be used for counting during loops and automatically declares them as integer types. It is good practice in modern Fortran to declare `implicit none` at the start of all programs, modules, functions, and subroutines to supress this behaviour.\n", "\n", "## Assigning Variables\n", "\n", "Variables can be assigned when they are declared however it is best practice to assign a value with a new statement for instance:\n", "\n", "```fortran\n", "integer :: i, j ! simple counters for looping\n", "real :: my_height\n", "\n", "i = 0\n", "my-height = 54.7\n", "```\n", "\n", ":::{note}\n", ":name: assigning-vars\n", "Always ensure variables are assinged a value. If a variable is delcared and referenced but not assigned unexpected behaviour can occur:\n", "\n", "```fortran\n", "real :: y\n", "print *, y\n", "! gfortran prints 4.56472975E-41\n", "! ifort prints 0\n", "```\n", "\n", "Here the gfortran compiler printed what was already in the memory it assigned for y, whereas the ifort compiler set the value to zero.\n", ":::\n", "\n", "### Parameters\n", "\n", "Parameters are variables with a fixed value that cannot be changed. Parameters must be declared with their value:\n", "\n", "```fortran\n", "real :: earth_radius = 6371000\n", "character(*), parameter :: earth_radius_unit = 'm'\n", "```\n", "\n", "They are often used, as in the example above, for physical constants.\n", "\n", ":::{tip}\n", ":name: constants-pi\n", "Fortran doesn't have any built in constants such as $e$ or $\\pi$. To calculate these constants use:\n", "\n", "```fortran\n", "e = exp(1.0)\n", "pi = 4.0*atan(1.0)\n", "```\n", "\n", "From [Rosetta Code](https://rosettacode.org/wiki/Real_constants_and_functions#Fortran).\n", ":::\n", "\n", "### Character Type\n", "\n", "Characters are declared with a length variable. This allocates the correct amount of memory for the string length. If the string is shorter than the declared length it will be padded with spaces. If the string is longer than the declared length it will be cut to the declared length.\n", "\n", "::::{margin} \n", ":::{admonition} trim()\n", ":class: note\n", "When printing a character type the `trim()` command will remove any trailing whitespaces.\n", ":::\n", "::::\n", "\n", "
Interactive
\n", "\n", " Make sure you have [activated interactivity]() on this page.\n", "
\n", "'Voyager'
to 'Enterprise'
, what happens to the stored string?'Enterprise'
string change the len=7
command to `len=*`
or simply to `*`
and make the variable space_ship
a parameter, what happens to the stored string?