Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

subroutines in interface-block (very newbie question)

Status
Not open for further replies.

ccornchip2

Programmer
Feb 15, 2011
2
FR
Hi,

I was looking over some fortran code that I am working with, and I've noticed that the functions and subroutines are all "declared" in an interface block.
However, through some of my own testing, I've noticed that not declaring a subroutine in an interface block does NOT cause a compiler error as I thought it should have. But, I did observe some unexpected behaviour and runtime errors (e.g. seg-fault).
Googling around, I found that this was perfectly normal, but it does lead me to my first question:

Why did it not cause a compiler error (or even warning)?

Secondly, I've noticed that subroutines that have no arguments don't appear in the interface block, even if the sub is defined in a different .f90 file. Why is this so?

Is there a rule saying "declare all functions and subs that you will use in your interface block *except* the ones that don't have arguments ... and if you forget to, the compiler won't pick it up" ?


My background is in C, so I'm a bit naive to all this fortran stuff.
 
Why did it not cause a compiler error (or even warning)?

Because interface blocks exist from FORTRAN-90 only and old codes do not use them. The present FORTRAN standard is compatible to the oldest versions of FORTRAN (with very few exceptions).

Secondly, I've noticed that subroutines that have no arguments don't appear in the interface block, even if the sub is defined in a different .f90 file. Why is this so?

Probably because interface blocks especially allow to check arguments. The risk of mistakes with no argument is rather limited. But you may declare procedures without argument in interface blocks.

Is there a rule saying "declare all functions and subs that you will use in your interface block *except* the ones that don't have arguments ... and if you forget to, the compiler won't pick it up" ?

No because using interface blocks is not the recommended way in new FORTRAN codes : the right way consists in creating modules containing procedures. In that way, the interface block are useless (they are even forbidden in that case) because all module procedure interfaces are automatically loaded when using a module (via a USE statement).

Interface blocks are useful for declaring external procedures, i.e. procedures written in the old days or procedures written in another language (have a look into the iso_c_binding module of F2003).

My background is in C, so I'm a bit naive to all this fortran stuff.

You are welcome ! Indeed, an interface block looks like a C include file. But if you want to program correctly in FORTRAN, you must typically forget the reflex to translate several C techniques into FORTRAN techniques. In particular (I might forget important points here) :

- interfaces are useless if you use modules.

- pointers must be just used to point to an existing memory area (already allocated). For dynamic allocation, use ALLOCATABLE variables. In addition, a FORTRAN pointer is more an alias than an address : be aware about this subtle distinction. It is essential to understand how FORTRAN pointers behave.

- for a portable coupling FORTRAN/C, do the work on the FORTRAN side (with the iso_c_binding).


François Jacq
 
Aha! Great, thank you!

It all makes so much sense now: Old (eg. F77) FORTRAN didn't have interfaces, and new (eg. F2003) fortran has 'implied' interfaces via the modules.
I guess the original author of the code that I'm working with comes from the era between the two.

Thanks again for the help.
Chris.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top