I've inherited about 400 lines of very weirdly written Fortran 77 code, and I'm trying to analyze it step by step to make it clear in my mind.
Anyway, I have an header-like file (actually a .h
, but the code in it is in fortran not C/C++) with JUST two statements in it, called getarg.h
:
character*80 serie
integer ln
Then I have another fortran file (.f
) called getserie.h
which has this code inside it:
subroutine getserie(serie, ln)
include 'getarg.h'
call getarg(1, serie)
ln = index(serie, ' ') - 1
return
end
My question being: can I call
an external file with just variables declarations in it? What's the effect of doing this?
No, you can call only subroutines. This means subprograms designated as subroutine
. However the definition of the subroutine does not have to be in your source file. It just have to be supplied at link time.
The getarg
subroutine is probably an intrinsic subroutine of your compiler which gets the command line arguments. This means that the compiler provides the code of the subroutine to the linker automatically.
The file getarg.h
is not called in any way. Its content is just copied directly to the place of the include
statement.
There are situations where you need to have an (explicit) interface of the called subroutine available, but in later Fortran versions, 90 and later. In these modern versions you normally place the subroutines and functions in modules, so that the compiler can check you are calling them correctly.