- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
[fortran]
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
[/fortran]
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
OK, I will do as you suggest.
Your suggestions are greatly appreciated.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page