Q re entry points

Q re entry points

In this example it does not share the local variable.

If its an arry, there is not problem, 

the problem is with no-dimensioned arrays.

Perhaps I am omitting a critical step?

-------------------------------------------------------------------------

subroutine sub1

integer l

l=l+1

return

entry sub2

l=0

return

entry  sub3

print *,l

end

----------------------------------------------------------------

Now I should be able to call sub2 to set l=0,

then call sub1 a number of times to increment it,

finally print it with sub3. It does not act like the VARIABLE L is shared, though.

Are the return statements necessary?

 

Is what I am doing even LEGAL?

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Unless the variable l has the SAVE attribute, Sub1 and Sub3 will use undefined variables, and Sub2 may get compiled into the equivalent of an immediate RETURN at entry, since it does nothing useful at present.

Yes, this is legal, but the ENTRY statement is obsolete, so you should not use it for new code.  Instead, you should write separate procedures for all of the operations you want to perform and then group all of those procedures in a module.  This is the recommended upgrade path for older code that uses ENTRY statements.

The ENTRY statement was designed for designating common code paths in a subroutine with several different variations, in the bad old days when main memory (RAM) was extremely limited and horribly expensive.  Those days are long in the past, so scraping out the last few bytes is no longer desirable nor necessary.

Also, prior to when Fortran 90 was published in June 1991, modules and private module procedures did not exist.  Those two features make use of ENTRY no longer necessary.

Here is a more robust method of getting what you want.  I have changed some of the variable and procedure names.

Module Counter_Procedures_M
Integer, Save :: Counter_mi  ! Explicit specification of the SAVE attribute is not really necessary.
! By rule all module variables have the SAVE attribute.  However, it is desirable to make this explicit.
!
Contains
!
Subroutine Initialize_Counter
    Counter_mi = 0
End Subroutine Initialize_Counter
!
Subroutine Increment_Counter
    Counter_mi = Counter_mi + 1
End Subroutine Increment_Counter
!
Subroutine Print_Counter
    Write  (Unit=*, Fmt=*) "Counter value:", Counter_mi
End Subroutine Print_Counter
!
End Module Counter_Procedures_M

Hope this helps.

The local variable would not be saved between calls without the save attribute. Using entry points is thought of as an obsolete practice. Use a module to store your variable and have separate routines to set and increment it. 

OK, I will do as you suggest.

Your suggestions are greatly appreciated.

Leave a Comment

Please sign in to add a comment. Not a member? Join today