Subroutine or program?

Sorry to bombard with questions but the help so far has been amazing!

So, I have my program (stelcor) that is in old Fortran and begins with…

SUBROUTINE STELCOR(CorM,CorR,CorL,Fmass,Timod,iprint)

My other program calls this file, simply by…

call stelcor(CorM, CorR, CorL, fluxM, time, iprint)

I am migrating to F90.

So, I have my head around the idea of having…

program main

end program

But do I ignore this and keep to having the whole thing set up as a collection of subroutines (with stelcor being the main subroutine)?

Equally, does the call statement tell the compiler to look at local f90 files for that subroutine or does the makefile system I have with the original resolve all of this?

Sorry, truly trying to get my head around this; and it is working - with much help from this forum!

A complete Fortran program always has a main program, even if program does not appear in the code. It could be as small as

end

Your main program could be as simple as

program abc
use m, only: stelcor ! stelcor is imported from module m
implicit none
real :: CorM, CorR, CorL, fluxM, time
integer :: iprint
! set variables
call stelcor(CorM, CorR, CorL, fluxM, time, iprint)
end program abc

or

use m, only: stelcor ! stelcor is imported from module m
implicit none
real :: CorM, CorR, CorL, fluxM, time
integer :: iprint
! set variables
call stelcor(CorM, CorR, CorL, fluxM, time, iprint)
end

I prefer the former syntax, to make it clear that the main program is being defined.

If module m is defined in file m.f90 and the main program in abc.f90, when you compile with (for example) gfortran m.f90 abc.f90 the compiler will find the definition of stelcor. The same will happen if you compile with

gfortran -c m.f90
gfortran -c abc.f90
gfortran m.o abc.o

Make is just a tool to generate the compilation commands.

1 Like

Thanks for this.

Still a little confused.

The stelcor file has the following as it’s very first line (line 1)…

SUBROUTINE STELCOR(CorM,CorR,CorL,Fmass,Timod,iprint)

This is followed by a series of comments before the code begins…

 include 'parm.h'
        include 'xvar.h'
        
        data ifirst / 0 /
        data zMmin / 9.945d31 /

        save
        
        Zflux  = Fmass
        xTimod = Timod
        xCorM  = CorM
        ipxx   = iprint

Do I therefore, do this…?

SUBROUTINE STELCOR(CorM,CorR,CorL,Fmass,Timod,iprint)

main program
    include 'parm.h'
    include 'xvar.h'
        
    data ifirst / 0 /
    data zMmin / 9.945d31 /

    Zflux  = Fmass
    xTimod = Timod
    xCorM  = CorM
    ipxx   = iprint

end main

I’m going to recommend you take a course, read a book, or at least go through a good tutorial. (FYI I have some online courses available). I worry that by learning from this old code base you are likely to pick up misunderstandings and unnecessary habits (i.e. Cargo cult programming - Wikipedia).

You should make sure you have a firm understanding of things like

  • how source code is organized
  • what the flow of execution is like
  • variable scope

Imagine trying to learn a spoken language by reading a story you’ve never heard before and looking up the translations of individual words as you encounter them. There’s a lot of possibility for misinterpretation. Same thing with learning a programming language by reading a program you’re unfamiliar with and then only asking about specific aspects.

Of course do come back here and ask questions if the material you choose to learn from leaves you confused or doesn’t explain something in enough detail for you.

3 Likes

… or, even worse, Voodoo Programming.

1 Like

I feel it’s possibly an idea to state that I am not new to programming per se. Not only have I been programming in a variety of languages for over 30 years, but I teach programming; from VB to Python to Java. In the latter case I also wrote a book on programming (not published but used in schools around me and in Hong Kong).

This is NOT to boast, but I feel my posts might make me appear a novice; which I am not.

I have 3 books on Fortran coming; one on the old version, one on the new and a bridge. I also have a course book from a professor who taught Fortran.

The issue is, like all languages, learning the nuances. And, unusually, I am trying to learn these whilst also develop an appreciation of the differences between the old and new approaches.

And that’s the problem that I am finding; not the understanding of the terms or approaches but more the migration from the one to the other.

Equally, I don’t want to use the poorest migration step; that’s where the expertise in this forum helps considerably.

I do hope this explains my situation, does not make me sound arrogant or ungrateful for your advice; I will be looking to your online course as well (which I would not have known about without this forum).

Oh, and my frequency of posting is more down to a sign of how much time I am spending on this; which is probably too much! Hahahaha!

EDIT: to add to this lengthy (and self-congratulatory) reply, I also used to code in machine code (6502) and assembler (for 6502, Z80 and x86).

I even wrote a graphical simulator of my old 6502 tutor system; the EMMA II.

1 Like

Probably my worst trait is that I MUST understand what I am doing, rather than copy-paste.

I know I have driven my fellow students mad (I am, and have been, a part time student whilst I worked full time - now part time as well) by not simply accepting a concept but insisting I comprehend each step.

So, I rarely copy-paste code but do use examples to construct my own solutions (unless the example is already the best approach, which is rare). :slight_smile:

I can promise you, I have never written a program I have not understood completely.

1 Like

As a note, I have resolved this; in large part to the help given here, along with an exploration of the code (and a little bit of trial and error).

Thanks to everyone for their help and advice; it is very gratefully received.

You have a file with a SUBROUTINE definition (I assume it is the only definition in there, you haven’t said). That is not a “program”. It is a program unit, specifically, an external subprogram.
To make a program in Fortran you need another type of program unit, specifically, a main program. Alternatively, you can create a program in a different language and you arrange for that to call your external subprogram.
You can put many program units in the same file.
Your “other program” (I assume in a different file) that calls STELCOR sounds like it is the main program. It may not have a PROGRAM statement already because that is optional, but it will have an END statement because that is obligatory. Only a main program is allowed to be written like that.

1 Like

Yes, that’s exactly what it was. I did some experimenting and realised the relationship.

It was the faulty way I was perceiving STELCOR that caused me ‘code blindness’! Hahaha!