6

I'm currently learning FORTRAN (I am familiar with MatLab) and I am very confused about the point of subroutines. Why would anyone use them as opposed to functions. Also, how is it that they can return values when called in the main programs? For example:

PROGRAM SUBDEM 
    REAL A,B,C,SUM,SUMSQ 
    CALL INPUT( + A,B,C)
    CALL CALC(A,B,C,SUM,SUMSQ)
    CALL OUTPUT(SUM,SUMSQ)
END

SUBROUTINE INPUT(X, Y, Z)
    REAL X,Y,Z
    PRINT *,'ENTER THREE NUMBERS => '
    READ *,X,Y,Z
    RETURN
 END

SUBROUTINE CALC(A,B,C, SUM,SUMSQ)
    REAL A,B,C,SUM,SUMSQ
    SUM = A + B + C
    SUMSQ = SUM **2
    RETURN
END

SUBROUTINE OUTPUT(SUM,SUMSQ)
    REAL SUM, SUMSQ
    PRINT *,'The sum of the numbers you entered are: ',SUM
    PRINT *,'And the square of the sum is:',SUMSQ
    RETURN
END

My question is essentially, how do I know what each subroutine is returning? Thank you.

2
  • 2
    I think you might be the first person to try to learn FORTRAN in 40 years.
    – user22815
    Commented Feb 4, 2015 at 21:13
  • 5
    @Snowman: Fortran is alive and kicking in numerical-heavy areas. If you wonder what powers the nice hip things like numpy and scipy, it's partially old pal Fortran. OTOH it's reasonable to learn Fortran 90/95, not Fortran IV. I believe Fortran 90 allows you to pass / return parameters in more sophisticated ways.
    – 9000
    Commented Apr 12, 2015 at 15:12

2 Answers 2

7

In Fortran, a subroutine "returns" everything you pass to it. You can think of it almost like a C Macro. In this:

 SUBROUTINE CALC(A,B,C, SUM,SUMSQ)

The subroutine "returns" A, B, C, SUM, and SUMSQ. (Really, it just modifies the values passed to it in-place.)

In contrast, FUNCTION works like a C function, creating locals and returning a single value, which is what people used to more modern languages typically expect.

1
  • Note: You can change what gets returned by defining the intent for each variable. Commented Dec 17, 2022 at 17:58
2

As a rough guide, functions are generally used to enclose a small segment of code that has no side-effects. For example, we can write

x = 2*foo(a) + foo(b)/4

if the order of execution is not critical (either because of no side-effects or the side-effects don't influence the result). If the code segment is large and/or contains many side-effects, it might be better to use a subroutine. A subroutine call is always a separate statement and thus the execution order is always unambiguous.

A subroutine is like a C function with a void return type and with some arguments passed by reference (by default all, actually). Ignoring good style and code safety, it is in principle possible to use only functions or only subroutines when writing modern Fortran.

1
  • Good answer! Just made a minor edit to make the formulation of a sentence clearer to me ...
    – logc
    Commented Apr 12, 2015 at 17:17

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.