Usage of Fortran statement functions

僤鯓⒐⒋嵵緔 提交于 2019-11-28 09:29:42

问题


I read about statement functions, such as the example:

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

Isn't this the same as:

C = 5.0*(F - 32.0)/9.0

i.e. without the function part, or maybe I'm missing something?

If they're not the same, when do I need to use a statement function?


回答1:


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


来源:https://stackoverflow.com/questions/25182512/usage-of-fortran-statement-functions

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