Intrinsic Functions
8 methodsFortran 标准内置函数与语句集合。
PRINT *, items以默认格式输出列表项。
Parameters
| Name | Type | Description |
|---|---|---|
| items | any | 待输出项,逗号分隔 |
Returns
无,输出到标准输出
Example
fortran
program demo
integer :: n = 42
print *, 'answer =', n
end program demoREAD *, variables从标准输入读取数据到变量。
Parameters
| Name | Type | Description |
|---|---|---|
| variables | any | 接收数据的变量 |
Returns
无,数据写入变量
Example
fortran
program demo
integer :: n
read *, n
print *, 'got', n
end program demoSIZE(array)返回数组的元素总数。
Parameters
| Name | Type | Description |
|---|---|---|
| array | array | 任意维数组 |
Returns
integer,数组元素总数
Example
fortran
integer :: a(3, 4)
print *, size(a) ! 12
print *, size(a, dim=1) ! 3ALLOCATE(array(n))为可分配数组分配内存。
Parameters
| Name | Type | Description |
|---|---|---|
| array | allocatable array | 可分 配数组 |
| n | integer | 维度大小 |
Returns
无,数组被分配
Example
fortran
real, allocatable :: a(:)
allocate(a(100))
a = 1.0
print *, size(a) ! 100DEALLOCATE(array)释放可分配数组的内存。
Parameters
| Name | Type | Description |
|---|---|---|
| array | allocatable array | 已分配数组 |
Returns
无,数组被释放
Example
fortran
real, allocatable :: a(:)
allocate(a(50))
deallocate(a)SUM(array)返回数组所有元素之和。
Parameters
| Name | Type | Description |
|---|---|---|
| array | numeric array | 数值数组 |
Returns
numeric,元素之和
Example
fortran
integer :: a(5) = [1, 2, 3, 4, 5]
print *, sum(a) ! 15
print *, sum(a, mask=a>2) ! 12MATMUL(a, b)执行矩阵乘法。
Parameters
| Name | Type | Description |
|---|---|---|
| a | 2D array | 左侧矩阵 |
| b | 2D array | 右侧矩阵 |
Returns
2D array,乘积矩阵
Example
fortran
integer :: a(2,3), b(3,2), c(2,2)
a = reshape([1,2,3,4,5,6], [2,3])
b = reshape([1,2,3,4,5,6], [3,2])
c = matmul(a, b)
print *, cOPEN(unit, file)打开文件并关联到指定单元号。
Parameters
| Name | Type | Description |
|---|---|---|
| unit | integer | 文件单元号 |
| file | character | 文件名 |
Returns
无,文件被打开
Example
fortran
integer :: u = 10
open(unit=u, file='data.txt', status='old')
read(u, *) x
close(u)