Fortran 语句函数的使用 [英] Usage of Fortran statement functions

查看:33
本文介绍了Fortran 语句函数的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到了语句函数,比如例子:

I read about statement functions, such as the example:

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

这不就是:

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?

推荐答案

C = 5.0*(F - 32.0)/9.0

只是赋值给一个变量C,它可以在任何地方,每次程序流到达它时都会计算一次.

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

是一个语句函数,并且可以在任何时候通过例如返回大约 37.8C(100) 来评估它.

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.

来自一些代码

  xx(i) = dx*i

  f(a) = a*a

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

print 语句中的 f(x)x 的每个新值求值并产生一个新值.x 的值也是上一行语句函数 xx 求值的结果.

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.

但是语句函数现在(在 Fortran 95 中)被声明为过时了.在任何新代码中更好地使用内部函数.例如,

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

这篇关于Fortran 语句函数的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆