forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem With Optional Parameters

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
EKruck



Joined: 09 Jan 2010
Posts: 224
Location: Aalen, Germany

PostPosted: Mon Jul 25, 2016 5:11 pm    Post subject: Problem With Optional Parameters Reply with quote

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
View user's profile Send private message Visit poster's website
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7916
Location: Salford, UK

PostPosted: Mon Jul 25, 2016 5:56 pm    Post subject: Reply with quote

I guess that you need an interface for each call to GETANGLE.
Back to top
View user's profile Send private message AIM Address
simon



Joined: 05 Jul 2006
Posts: 268

PostPosted: Tue Jul 26, 2016 2:46 am    Post subject: Reply with quote

Try putting them in a module.
Back to top
View user's profile Send private message
John-Silver



Joined: 30 Jul 2013
Posts: 1520
Location: Aerospace Valley

PostPosted: Tue Jul 26, 2016 4:45 am    Post subject: Reply with quote

Quote:
External procedures that have optional arguments must have an explicit interface.


not probably, must have.

ref. http://www.silverfrost.com/ftn95-help/language/xarguments.aspx
Back to top
View user's profile Send private message
John-Silver



Joined: 30 Jul 2013
Posts: 1520
Location: Aerospace Valley

PostPosted: Tue Jul 26, 2016 5:31 am    Post subject: Reply with quote

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
View user's profile Send private message
EKruck



Joined: 09 Jan 2010
Posts: 224
Location: Aalen, Germany

PostPosted: Tue Jul 26, 2016 7:01 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7916
Location: Salford, UK

PostPosted: Tue Jul 26, 2016 9:09 am    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
mecej4



Joined: 31 Oct 2006
Posts: 1885

PostPosted: Tue Jul 26, 2016 10:55 am    Post subject: Re: Reply with quote

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
View user's profile Send private message
John-Silver



Joined: 30 Jul 2013
Posts: 1520
Location: Aerospace Valley

PostPosted: Tue Jul 26, 2016 3:19 pm    Post subject: Reply with quote

Ekruck - there is one in the Silverfrost documentation, see the link in my comment earlier
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7916
Location: Salford, UK

PostPosted: Mon Jan 30, 2017 1:03 pm    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
mecej4



Joined: 31 Oct 2006
Posts: 1885

PostPosted: Mon Jan 30, 2017 4:05 pm    Post subject: Reply with quote

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
View user's profile Send private message
wahorger



Joined: 13 Oct 2014
Posts: 1217
Location: Morrison, CO, USA

PostPosted: Tue Jan 31, 2017 12:56 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group