变量#

变量存储可以由程序操作的信息。 Fortran 是一种_强类型_语言,这意味着每个变量都必须有一个类型。

Fortran 中有 5 种内置数据类型:

  • integer —— 表示整数的数据,正数或负数

  • real —— 用于浮点数据(不是整数)

  • complex —— 由实部和虚部组成的对

  • character —— 用于文本数据

  • logical —— 用于表示布尔值(真或假)值的数据

在我们可以使用变量之前,我们必须_声明_它;这告诉编译器变量类型和任何其它变量属性。

Fortran 是一种 静态类型 语言,这意味着每个变量的类型在程序编译时是固定的 —— 变量类型在程序运行时不能改变。

声明变量#

声明变量的语法是:

<variable_type> :: <variable_name>

其中 <variable_type> 是上面列出的内置变量类型之一,而 <variable_name> 是你要调用变量的名称。

变量名必须以字母开头,可以由字母、数字和下划线组成。在下面的示例中,我们为每个内置类型声明一个变量。

**示例:**变量声明

program variables
  implicit none

  integer :: amount
  real :: pi
  complex :: frequency
  character :: initial
  logical :: isOkay

end program variables

Fortran 代码不区分大小写;你不必担心变量名的大小写,但保持一致是一种很好的做法。

注意程序开头的附加语句:implicit none。该语句告诉编译器所有变量都将被显式声明;如果没有此语句,变量将根据它们开头的字母隐式键入。

始终在每个程序和过程的开头使用 implicit none 语句。隐式类型在现代编程中被认为是不好的做法,因为它隐藏了导致更多程序错误的信息。

一旦我们声明了一个变量,我们就可以使用赋值运算符 = 对其进行赋值和重新赋值。

示例: 变量赋值

amount = 10
pi = 3.1415927
frequency = (1.0, -0.5)
initial = 'A'
isOkay = .false.

字符由单引号 (') 或双引号 (") 包围。

逻辑或布尔值可以是 .true..false.

重要

Watch out for assignment at declaration:

integer :: amount = 1

This is NOT a normal initialisation; it implies the save attribute, which means that the variable retains its value between procedure calls. Good practice is to initialise your variables separately to their declaration.

标准输入/输出#

在我们的 Hello World 示例中,我们将文本打印到命令窗口。这通常被称为写入 standard outputstdout

我们可以使用前面介绍的 print 语句将变量值打印到 stdout

print *, 'The value of amount (integer) is: ', amount
print *, 'The value of pi (real) is: ', pi
print *, 'The value of frequency (complex) is: ', frequency
print *, 'The value of initial (character) is: ', initial
print *, 'The value of isOkay (logical) is: ', isOkay

以类似的方式,我们可以使用 read 语句从命令窗口读取值:

program read_value
  implicit none
  integer :: age

  print *, 'Please enter your age: '
  read(*,*) age

  print *, 'Your age is: ', age

end program read_value

此输入源通常称为 standard inputstdin

表达式#

常用的算术运算符集可用,按优先顺序列出:

操作符  

描述

**

指数

*

乘法

/

除法

+

加法

-

减法


例子:

program arithmetic
  implicit none

  real :: pi
  real :: radius
  real :: height
  real :: area
  real :: volume

  pi = 3.1415927

  print *, 'Enter cylinder base radius:'
  read(*,*) radius

  print *, 'Enter cylinder height:'
  read(*,*) height

  area = pi * radius**2
  volume = area * height

  print *, 'Cylinder radius is: ', radius
  print *, 'Cylinder height is: ', height
  print *, 'Cylinder base area is: ', area
  print *, 'Cylinder volume is: ', volume

end program arithmetic

浮点精度#

可以使用 kind 参数显式声明所需的浮点精度。 iso_fortran_env 内置模块为常见的 32 位和 64 位浮点类型提供了 kind 参数。

示例: 显式实数 kind

program float
  use, intrinsic :: iso_fortran_env, only: sp=>real32, dp=>real64
  implicit none

  real(sp) :: float32
  real(dp) :: float64

  float32 = 1.0_sp  ! Explicit suffix for literal constants
  float64 = 1.0_dp

end program float

始终为浮点文字常量使用 kind 后缀。

示例: C 互操作的 kind

program float
  use, intrinsic :: iso_c_binding, only: sp=>c_float, dp=>c_double
  implicit none

  real(sp) :: float32
  real(dp) :: float64

end program float

在下一部分中,我们将学习如何使用数组在变量中存储多个值。

Local scope variables with block construct#

The 2008 Fortran standard introduced the notion of block which enables using local scope variables within a program or procedure.

例子:

module your_module
    implicit none
    integer :: n = 2
end module

program main
    implicit none
    real :: x

    block
        use your_module, only: n ! you can import modules within blocks
        real :: y ! local scope variable
        y = 2.0
        x = y ** n
        print *, y
    end block
    ! print *, y ! this is not allowed as y only exists during the block's scope
    print *, x  ! prints 4.00000000
end program