2

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.

Giffyguy
  • 20,378
  • 34
  • 97
  • 168
  • 2
    Pretty impressive, figuring out FORTRAN without a manual. – dkretz Jul 03 '09 at 18:34
  • Why is this tagged C and C++? – c4757p Jul 03 '09 at 23:39
  • "I'm in the mood to learn some old coding styles, and FORTRAN seems like a good place to start. " - why ? Could you explain what you ment by that ? – Rook Jul 04 '09 at 11:33
  • ldigas - yeah, it looks like FORTRAN is still being updated and used in modern development environments of sorts. I wasn't aware of this when I asked the question, and I had thought of FORTRAN as somewhat archaic. My mistake. – Giffyguy Jul 04 '09 at 22:46
  • c4757p - It's tagged C/C++ because that's my starting point. I'm a C developer. But that's not entirely relavent, is it? Again, my bad. – Giffyguy Jul 04 '09 at 22:47

6 Answers6

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.

Tim Whitcomb
  • 10,447
  • 3
  • 35
  • 47
  • Good grief Tim. Where did you dig that style out from ? :-) – Rook Jul 04 '09 at 11:34
  • Unfortunately it's quite similar to some of the code I've found floating around the office - I "solved" a problem a colleague had by giving him a subroutine that used a computed GOTO and he didn't realize I was kidding. I resisted the urge here to post a sequence of images from http://www.kloth.net/services/cardpunch.php – Tim Whitcomb Jul 04 '09 at 19:07
  • Heh heh, guess what I have to maintain (and yes, it all looks like that) – Ellery Newcomer Jul 11 '09 at 16:08
  • This is why RATFOR was invented. – Richard Chambers Sep 08 '12 at 16:28
2

See Functions and Subroutines:

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

then later:

k = foo(1, 2)
John Saunders
  • 160,644
  • 26
  • 247
  • 397
2

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

Arnkrishn
  • 29,828
  • 40
  • 114
  • 128
1

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
Dario
  • 48,658
  • 8
  • 97
  • 130
  • The questiomer asked about OLD coding styles - let's only have pure FORTRAN IV here, please :-) –  Jul 03 '09 at 18:39
  • Ha! I don't think I'm looking to get that specific yet. I'm just wanted to get a general idea. This looks pretty interest from the grammar perspective. Thanks a lot! – Giffyguy Jul 03 '09 at 18:41
  • @Neil Butterworth - FORTRAN IV. Don't you think that's a little too advanced ? :-) – Rook Jul 04 '09 at 11:36
1

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.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • You young whippersnappers, with your fancy "print *". We had to do something like "WRITE(6, 100) R" and "100 FORMAT (9HRESULT = , I3)" in FORTRAN IV. – David Thornley Jul 09 '09 at 16:22
  • 2
    That can't possibly be the Fortran 77 they taught you 23 years ago - what are all those lower-case letters doing there? :) – Tim Whitcomb Jul 10 '09 at 19:52
0

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
Francois Jacq
  • 1,244
  • 10
  • 18