Fortran Module w/ user defined type

  • Context: Fortran 
  • Thread starter Thread starter hotvette
  • Start date Start date
  • Tags Tags
    Fortran module Type
Click For Summary
SUMMARY

This discussion centers on initializing user-defined types in Fortran modules. It confirms that variables of user-defined types cannot be initialized directly in a module without using a subroutine, unlike intrinsic types. The use of the 'SAVE' keyword and 'CONTAINED' subroutines is necessary for initializing user-defined types, which allows modifications by any program that uses the module. The participant seeks a method to make these variables 'read-only' for programs that utilize the module, noting that the 'PARAMETER' keyword is not applicable in this context.

PREREQUISITES
  • Understanding of Fortran modules and user-defined types
  • Familiarity with Fortran subroutines and the 'SAVE' keyword
  • Knowledge of intrinsic types in Fortran
  • Basic concepts of variable scope and accessibility in Fortran
NEXT STEPS
  • Research Fortran's 'CONTAINED' subroutines and their implications on variable initialization
  • Explore methods to enforce read-only access for module variables in Fortran
  • Investigate the use of the 'PARAMETER' keyword and its limitations in user-defined types
  • Learn about encapsulation techniques in Fortran to protect module data
USEFUL FOR

This discussion is beneficial for Fortran developers, particularly those working with modules and user-defined types, as well as programmers seeking to manage variable accessibility and initialization effectively.

hotvette
Homework Helper
Messages
1,001
Reaction score
11
I have a bunch of programs that initialize values of variables of a user defined type, so I thought I'd initialize them all in a module and have each program 'use' the module. But, it seems the only way to initialize the variables in a module is to put the initializations in a subroutine within the module and then call the subroutine, say from the MAIN program. Initializing variables of intrinsic type in a module can be done w/o an imbedded subroutine.

Is my understanding correct, and if so, why the difference? Thanks for any comments.
 
Last edited:
Technology news on Phys.org
I think I found the explanation. User defined functions can't be used to initialize parameters. But, this brings up a 2nd question. The only way I've figured out how to initialize the values of variables using a user defined function is to use the 'SAVE' keyword in the declaration of the variable and a 'CONTAINED' subroutine to initialize the values. This means any program 'USING' the module can change the value of the variables.

Simply put, is there is any way to have a variable defined in a module be 'read only' by programs that use the module? Can't use the 'PARAMETER' keyword per the above explanation. Is there any other option?
 

Similar threads

  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 59 ·
2
Replies
59
Views
12K
Replies
65
Views
5K
  • · Replies 16 ·
Replies
16
Views
6K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K