Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

fortran90 - Usage of Fortran statement functions

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...