Attribute Purpose

The public attribute is used to specify that a module's variables, internal functions or subroutines can be accessed by other program units with the USE association.

Examples and Rules of Use

If you totally ignore the use of PUBLIC and PRIVATE attributes, the default is that any program unit using the module has full (public) access to all variables and procedures declared in the module. You can clearly state the same intent with a simple PUBLIC statement.
		Module stuff
		     Function nfac(n)
		       integer n,nfac,i
		       nfac = 1
		       do i=2,n
		         nfac = nfac * i
		       end do
		     end function nfac
		end module stuff
The function nfac could then be accessed by any program unit that accesses the module "stuff" through the USE statement. In fact, if there were any other variables, functions or subroutines declared in the above module they would have been accessible to other program units as well, because the PUBLIC attribute was used to set the accessability of all program units to be public. If you did not want some of your internal procedures or variables accessible to other program units, then you would have to use the private statement to block the global accessability. For example, if you had numerous internal procedures inside a module and the only one that you wanted directly accessible to other program units was the nfac function from above, the opening lines of code for that module might look like the following.
		Module mystuff
		public nfac  
One final thing that you should know about this attribute is that it can only be used inside a module program unit. Any attempt to use this attribute in any other program unit would result in a compiler error.

Up one level / Home

Written by Jason Wehr: and Maintained by John Mahaffy :