View previous topic :: View next topic |
Author |
Message |
EKruck
Joined: 09 Jan 2010 Posts: 224 Location: Aalen, Germany
|
Posted: Mon Jul 25, 2016 5:11 pm Post subject: Problem With Optional Parameters |
|
|
To check all calls in a larger project, I copy all Fortran files together into one file for compilation. In case of an optional parameter I got a fatal error:
error 326 - SUBROUTINE GETANGLE has been called with too few arguments.
I made a small example:
Code: | SUBROUTINE CALLER1 ()
IMPLICIT NONE
REAL*8 Phi, Omega, Kappa
INTEGER iAerial
CALL getAngle (Phi, Omega, Kappa, iAerial)
RETURN
END
SUBROUTINE CALLER2 ()
IMPLICIT NONE
REAL*8 Phi, Omega, Kappa
CALL getAngle (Phi, Omega, Kappa)
RETURN
END
SUBROUTINE getAngle (Phi, Omega, Kappa, iAerial)
IMPLICIT NONE
Real*8, INTENT(OUT) :: Phi, Omega, Kappa
INTEGER, OPTIONAL :: iAerial
IF (PRESENT(iAerial)) GOTO 10
Phi = 1.0D0
Omega = 1.0D0
Kappa = 1.0D0
RETURN
10 Phi = 2.0D0
Omega = 2.0D0
Kappa = 2.0D0
RETURN
END SUBROUTINE getAngle
|
This provides just a worning:
warning 673 - SUBROUTINE GETANGLE has been called with too few arguments
If I change the sequence of the subroutines it works o.k.
Erwin |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7930 Location: Salford, UK
|
Posted: Mon Jul 25, 2016 5:56 pm Post subject: |
|
|
I guess that you need an interface for each call to GETANGLE. |
|
Back to top |
|
|
simon
Joined: 05 Jul 2006 Posts: 268
|
Posted: Tue Jul 26, 2016 2:46 am Post subject: |
|
|
Try putting them in a module. |
|
Back to top |
|
|
John-Silver
Joined: 30 Jul 2013 Posts: 1520 Location: Aerospace Valley
|
|
Back to top |
|
|
John-Silver
Joined: 30 Jul 2013 Posts: 1520 Location: Aerospace Valley
|
Posted: Tue Jul 26, 2016 5:31 am Post subject: |
|
|
as an aside, while googling around this problem I discovered something I didn't know before when I dropped accidentally on this quote on a webpage :-
Quote: | "I don't know what the technical characteristics of
the standard language for scientific and engineering
computation in the year 2000 will be... but I know it
will be called Fortran." John Backus |
I recommend anyone who's nver heard of him (I hadn't) to read up on his story.
Thnak you Mr Backus (R.I.P. 2007) |
|
Back to top |
|
|
EKruck
Joined: 09 Jan 2010 Posts: 224 Location: Aalen, Germany
|
Posted: Tue Jul 26, 2016 7:01 am Post subject: |
|
|
In the Intel Fortran manual I found now:
"To call a procedure that has an optional argument, you must use an explicit interface."
It would be nice to have such a hint as well in FTN95.
BTW: Practically my routine works fine without INTERFACE as long as I compile it it different files. |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7930 Location: Salford, UK
|
Posted: Tue Jul 26, 2016 9:09 am Post subject: |
|
|
I have made a note that FTN95 needs to issue a warning or error report in this context; perhaps a warning but an error when /iso is applied. |
|
Back to top |
|
|
mecej4
Joined: 31 Oct 2006 Posts: 1888
|
Posted: Tue Jul 26, 2016 10:55 am Post subject: Re: |
|
|
EKruck wrote: | BTW: Practically my routine works fine without INTERFACE as long as I compile it it different files. |
It does so because of a coincidence: the optional argument that is missing in the call is the last argument, and the compiler pushes arguments on the stack in right-to-left order.
If you have a non-terminal optional argument absent in a call, your program will most likely crash or misbehave otherwise if you do not provide an interface. That an interface is required is one of the rules of the Fortran 95 and later standards.
Last edited by mecej4 on Mon Jan 30, 2017 4:07 pm; edited 1 time in total |
|
Back to top |
|
|
John-Silver
Joined: 30 Jul 2013 Posts: 1520 Location: Aerospace Valley
|
Posted: Tue Jul 26, 2016 3:19 pm Post subject: |
|
|
Ekruck - there is one in the Silverfrost documentation, see the link in my comment earlier |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7930 Location: Salford, UK
|
Posted: Mon Jan 30, 2017 1:03 pm Post subject: |
|
|
I have had a further look at this and I don't see how FTN95 can do any better than what it does now.
In theory external subprograms are independently compiled (without any external knowledge). So the caller has no knowledge that the called subroutine has optional arguments.
When the various subprograms are in the same file then FTN95 does the best it can but the ordering of the subprograms will determine the outcome. |
|
Back to top |
|
|
mecej4
Joined: 31 Oct 2006 Posts: 1888
|
Posted: Mon Jan 30, 2017 4:05 pm Post subject: |
|
|
I agree with Paul on this issue.
We users often wish for certain features to make our program development easier and to help us catch bugs early, but in some cases these wishes cannot be fulfilled. That they are unreasonable/unimplementable becomes evident only upon deeper study of the Fortran standard and implementation issues.
For many program errors, the standard does not require the compiler to detect those errors -- these errors are the programmer's responsibility. For other program errors, the standard specifies "constraints", in which case a compiler is required to detect the error. |
|
Back to top |
|
|
wahorger
Joined: 13 Oct 2014 Posts: 1217 Location: Morrison, CO, USA
|
Posted: Tue Jan 31, 2017 12:56 am Post subject: |
|
|
I've found calling with too many arguments can be benign, but calling with too few (i.e. the call has 3 but the subroutine requires 4) will get you in trouble. Something about how the stack is handled in the routine and also back in the caller.
As well as not having a proper reference pointer to the data! |
|
Back to top |
|
|
|