Fortran Module w/ user defined type

In summary, the conversation discusses the use of modules to initialize values of variables, specifically for user defined types. It is mentioned that initializing variables of intrinsic types can be done without using a subroutine, but for user defined types, a subroutine is required. It is also mentioned that user defined functions cannot be used to initialize parameters and the only way to do so is by using the 'SAVE' keyword and a 'CONTAINED' subroutine. This means that any program using the module can change the value of the variables. The question is then raised if there is a way to make the variables in the module 'read-only', but it is stated that the 'PARAMETER' keyword cannot be used. The conversation ends without a solution to this issue.
  • #1
hotvette
Homework Helper
996
5
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
  • #2
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?
 

Related to Fortran Module w/ user defined type

1. What is a Fortran module?

A Fortran module is a self-contained program unit that contains data, procedures, and user-defined types. It allows for the organization and encapsulation of related variables and functions within a single unit, making it easier to manage and re-use code.

2. What is a user-defined type?

A user-defined type is a data type that is created by the programmer, rather than being built-in to the programming language. It allows for the creation of complex data structures that can be used to represent real-world objects or concepts.

3. How do I create a user-defined type in a Fortran module?

To create a user-defined type in a Fortran module, you first need to define the type using the type keyword, followed by the name of the type and any desired components or variables. Then, you can use the contains keyword to add procedures or functions that operate on the type. Finally, you can use the end type statement to close the type definition.

4. How do I use a user-defined type in my program?

To use a user-defined type, you first need to include the module where the type is defined in your program. Then, you can declare variables of that type using the type keyword, followed by the name of the type. You can then access and manipulate the components of the type using the dot notation.

5. What are the advantages of using a Fortran module with user-defined types?

Fortran modules with user-defined types offer several advantages, including better organization and encapsulation of code, improved readability and maintainability, and the ability to create complex data structures that can represent real-world objects or concepts more accurately.

Similar threads

  • Programming and Computer Science
2
Replies
65
Views
3K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
4
Views
773
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
16
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
3K
Replies
2
Views
994
Back
Top