views:

244

answers:

6

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.

+3  A: 

See Functions and Subroutines.

John Saunders
This is a great resource! Thanks a lot!
Giffyguy
+1  A: 

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
The questiomer asked about OLD coding styles - let's only have pure FORTRAN IV here, please :-)
anon
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
@Neil Butterworth - FORTRAN IV. Don't you think that's a little too advanced ? :-)
ldigas
+2  A: 

Hi

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

Andriyev
This looks like a good resource as well ... Thanks!
Giffyguy
+6  A: 

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
Now that's more like it! +1
anon
Good grief Tim. Where did you dig that style out from ? :-)
ldigas
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
Heh heh, guess what I have to maintain (and yes, it all looks like that)
Ellery Newcomer
+1  A: 

Some fortran resources (another SO question)

ldigas
+1  A: 

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
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
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