 |
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
Kenneth_Smith
Joined: 18 May 2012 Posts: 814 Location: Hamilton, Lanarkshire, Scotland.
|
Posted: Sun May 17, 2015 11:59 am Post subject: Query related to module variables |
|
|
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 |
|
 |
mecej4
Joined: 31 Oct 2006 Posts: 1899
|
Posted: Sun May 17, 2015 1:30 pm Post subject: |
|
|
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 |
|
 |
davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Sun May 17, 2015 9:17 pm Post subject: |
|
|
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 |
|
 |
Kenneth_Smith
Joined: 18 May 2012 Posts: 814 Location: Hamilton, Lanarkshire, Scotland.
|
Posted: Mon May 18, 2015 8:11 am Post subject: |
|
|
Thanks David, PURE does what I need - I've never seen it used before. |
|
Back to top |
|
 |
|
|
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
|