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

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

问题描述

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

不是这样:

  C = 5.0 *(F  -  32.0)/9.0 

即没有函数部分,或者我错过了什么?



如果它们不一样,我什么时候需要使用语句函数?

解决方案

  C = 5.0 *(F  -  32.0)/9.0 

只是赋值给一个变量 C ,它可以在任何地方并且每隔一次(F)= 5.0 *(F - 32.0)/9.0


  C  

是一个语句函数,可以在任何时候通过 C(100),它大致返回 37.8



从一些代码中可以看出

  xx(i)= dx * i 

f(a)= a * a

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

f(x)用每个新值 x 进行求值并产生一个新值。 x 的值也是前一行语句函数 xx 的评估结果。



但声明函数现在(在Fortran 95中)声明已过时。在任何新代码中更好地使用内部函数。例如,

 程序p 
隐式无
!变量x,i,nx,dx $的声明b
$ b do i = 1,nx
x = xx(i)
print *,f(x)
end do
包含

$实数函数xx(i)
整数,意图(in):: i
xx = dx * i
结束函数

实函数f(a)
real,intent(in):: a
f = a * a
结束函数
结束程序


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?

解决方案

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

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

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