Help us understand the problem. What is going on with this article?

Fortranのpure functionはModuleで定義されるグローバル変数を書き換えない。

More than 1 year has passed since last update.

例:


module mod
  integer :: glocalVar = 0

contains

  pure function square(x)
    real, intent(in) :: x
    real :: square
    square = x * x
    glocalVar = 1 !<---ここで書き換え発生させる
  end function

end module

program main
  use mod
  real :: a, b ! square
  a = 2.0
  b = square(a)
  print*, a, b, glocalVar
end program main

このブログラムを実行すると、ちゃんとエラーがでる。

glocalVar = 1
    1
Error: Variable 'glocalvar' can not appear in a variable definition context (assignment) at (1) in PURE procedure

結論

Fortranのpure functionはmodule内部のグローバル変数を書き換えない、安全な関数である。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away