Usage of Fortran statement functions

时光总嘲笑我的痴心妄想 提交于 2019-11-29 15:54:38
C = 5.0*(F - 32.0)/9.0

is just assignment to a variable C, it can be anywhere and is evaluated once every time when the program flow reaches it.

C(F) = 5.0*(F - 32.0)/9.0

is a statement function, and can be evaluated any time it is in the scope by, e.g., C(100) which returns approximately 37.8.

From some code

  xx(i) = dx*i

  f(a) = a*a

  do i = 1, nx
     x = xx(i)
     print *, f(x)
  end do

The f(x) in the print statement is evaluated with each new value of x and yields a new value. The value of x is also result of evaluation of the statement function xx on the previous line.

But statement functions are now (in Fortran 95) declared obsolete. Better use internal functions in any new code. E.g.,

program p
  implicit none
  !declarations of variables x, i, nx, dx

  do i = 1, nx
     x = xx(i)
     print *, f(x)
  end do
contains

  real function xx(i)
    integer, intent(in) :: i
    xx = dx*i
  end function

  real function f(a)
    real, intent(in) :: a
    f = a*a
  end function
end program
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!