FORTRAN 中函数的基本结构是什么?

发布于 2024-07-26 06:40:35 字数 452 浏览 7 评论 0原文

这是我最近想知道的事情,主要是出于好奇。 我想学习一些旧的编码风格,FORTRAN 似乎是一个很好的起点。

我想我应该通过提供一个好的起点来帮助你们。
那么这个 C 程序如何用 FORTRAN 编写呢?

int foo ( int x , int y )
{
    int tempX = x ;
    x += y / 2 ;
    y -= tempX * 3 ;    // tempX holds x's original value.
    return x * y ;
}

我知道整个函数可以是一行:

return ( x + ( y / 2 ) ) * ( y - ( x * 3 ) ) ;

但我问这个问题的目的是看看这四个语句如何在 FORTRAN 中单独编写,而不一定组合成单个语句。

This is something that's I've wanted to know recently, mostly out of curiousity. I'm in the mood to learn some old coding styles, and FORTRAN seems like a good place to start.

I guess I should help you guys out by providing a good starting point.
So how would this C procedure be written in FORTRAN?

int foo ( int x , int y )
{
    int tempX = x ;
    x += y / 2 ;
    y -= tempX * 3 ;    // tempX holds x's original value.
    return x * y ;
}

I know the entire function could be a single line:

return ( x + ( y / 2 ) ) * ( y - ( x * 3 ) ) ;

But the point of me asking this question is to see how those four statements would be written individually in FORTRAN, not neccessarily combined into a single statement.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

临走之时 2024-08-02 06:40:35

不要责怪我 - 你说的是编码风格:

C234567
      SUBROUTINE FOO(I,J,K)
C SAVE THE ORIGINAL VALUES
      IORIG = I
      JORIG = J
C PERFORM THE COMPUTATION
      I = I + J/2
      J = J - IORIG*3
      K = I * J
C RESTORE VALUES
      I = IORIG
      J = JORIG
      END SUBROUTINE FOO

当我写这篇文章时,我不寒而栗,但

  • 所有变量都是隐式整数,因为它们以 I 和 N FORTRAN 之间的字母开头
  • ,通过引用传递,所以重置 < code>I 和 J 位于末尾,以提供与原始函数相同的效果(即按值传递,因此 xy 在调用例程中没有改变)
  • 注释从第 1 列开始,实际语句从第 7 列开始

请,请,请永远不要编写这样的新代码,除非是开玩笑。

Don't blame me - you said old coding styles:

C234567
      SUBROUTINE FOO(I,J,K)
C SAVE THE ORIGINAL VALUES
      IORIG = I
      JORIG = J
C PERFORM THE COMPUTATION
      I = I + J/2
      J = J - IORIG*3
      K = I * J
C RESTORE VALUES
      I = IORIG
      J = JORIG
      END SUBROUTINE FOO

I shudder as I write this, but

  • all variables are implicitly integers, since they start with letters between I and N
  • FORTRAN passes by reference, so reset I and J at the end to give the same effect as the original function (i.e. pass-by-value, so x and y were unchanged in the calling routine)
  • Comments start in column 1, actual statements start in column 7

Please, please, please never write new code like this unless as a joke.

源来凯始玺欢你 2024-08-02 06:40:35

您的函数在 Fortran 中可能如下所示

 integer function foo(m, n)
 integer i

 i = m
 m = m + n/2
 n = n - i*3
 foo = m*n

 end function foo

您应该能够开始使用有关 Fortran 的任何教程。 像这样的事情可能会有所帮助 http://www.cs .mtu.edu/~shene/COURSES/cs201/NOTES/fortran.html

干杯

Your function might look like this in Fortran

 integer function foo(m, n)
 integer i

 i = m
 m = m + n/2
 n = n - i*3
 foo = m*n

 end function foo

You should be able to get started with any tutorial on Fortran. Some thing like this might help http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/fortran.html

cheers

对风讲故事 2024-08-02 06:40:35

请参阅函数和子例程

INTEGER FUNCTION foo(i, j)
...
foo = 42
END

然后:

k = foo(1, 2)

See Functions and Subroutines:

INTEGER FUNCTION foo(i, j)
...
foo = 42
END

then later:

k = foo(1, 2)
不乱于心 2024-08-02 06:40:35

你从哪里学习 FORTRAN? 只需看看wikibook

从这个例子中,我想说:

function func(x, y) result(r)
   integer, intent(in) :: x, y 
   integer             :: r 
   integer             :: tempX
   tempX = x
   x = x / 2
   y = y - tempX * 3
   r = x * y
end function foo

Where do you learn FORTRAN from? Just take a look at the wikibooks!

Derived from the example, I'd say:

function func(x, y) result(r)
   integer, intent(in) :: x, y 
   integer             :: r 
   integer             :: tempX
   tempX = x
   x = x / 2
   y = y - tempX * 3
   r = x * y
end function foo
征﹌骨岁月お 2024-08-02 06:40:35

与上面类似,但用一个主程序来说明它是如何调用的。

C2345678
       program testfoo
         implicit none   
         integer r, foo 
         r = foo(4,5)
         print *, 'result = ', r
       end

       integer function foo(x,y)
         integer x, y
         integer tx, ty
         tx = x + y / 2
         ty = y - x * 3
         foo = tx * ty
         return
       end

注意,这是Fortran 77,是我23年前学的。

Similar to above, but with a main program to illustrate how it would be called.

C2345678
       program testfoo
         implicit none   
         integer r, foo 
         r = foo(4,5)
         print *, 'result = ', r
       end

       integer function foo(x,y)
         integer x, y
         integer tx, ty
         tx = x + y / 2
         ty = y - x * 3
         foo = tx * ty
         return
       end

Note that this is Fortran 77, which is what I learned 23 years ago.

戒ㄋ 2024-08-02 06:40:35

应用整数规则 IJKLMN 的真正旧风格

C2345678
      FUNCTION IFOO(I,J)
      II = I + J/2
      JJ = J - I*3
      IFOO = II*JJ
      END

True old style in applying the rule IJKLMN for integer

C2345678
      FUNCTION IFOO(I,J)
      II = I + J/2
      JJ = J - I*3
      IFOO = II*JJ
      END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文