replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - Query related to module variables
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 

Query related to module variables

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



Joined: 18 May 2012
Posts: 814
Location: Hamilton, Lanarkshire, Scotland.

PostPosted: Sun May 17, 2015 11:59 am    Post subject: Query related to module variables Reply with quote

When we write a standalone subroutine, with arguments say A, B, C, we can prevent the subroutine changing A and B by using the INTENT(IN) statement.

Now if the subroutine is contained in a module, A and B could be declared as module variables, then they become available to the subroutine (i.e. they are not required to be passed to the subroutine as arguments). If my understanding is correct, this produces faster running code as no copying of variables is required?

Question � is there a way of preventing the subroutine code (i.e. a programming error) inadvertently changing A or B, when declared as module variables?

Thanks

Ken
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1899

PostPosted: Sun May 17, 2015 1:30 pm    Post subject: Reply with quote

A limited amount of protection from inadvertently changing a module variable is obtainable using the PROTECTED attribute. This attribute is available in Fortran 2003 and later, but some F95 compilers implement PROTECTED.

Unwanted changes to module variables can be caused when you "forget" to provide declarations for local variables in a subprogram that USEs a module, and the module has declarations for variables with the same names. You can control this by using the PUBLIC and PRIVATE attributes with selected variables, and by using the ONLY clause in USE statements, all these being features supported in Fortran 95.


Last edited by mecej4 on Sun May 17, 2015 11:27 pm; edited 1 time in total
Back to top
View user's profile Send private message
davidb



Joined: 17 Jul 2009
Posts: 560
Location: UK

PostPosted: Sun May 17, 2015 9:17 pm    Post subject: Reply with quote

As we don't have PROTECTED in this compiler, you can prevent writing to module variables by declaring your function or subroutine as pure (reading will be allowed).
_________________
Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl
Back to top
View user's profile Send private message
Kenneth_Smith



Joined: 18 May 2012
Posts: 814
Location: Hamilton, Lanarkshire, Scotland.

PostPosted: Mon May 18, 2015 8:11 am    Post subject: Reply with quote

Thanks David, PURE does what I need - I've never seen it used before.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support 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