Derived types in Fortran allow you to define custom data structures that can group different types of data together. This is similar to structures in C or classes in other programming languages. Derived types are essential for creating complex data models and are widely used in scientific and engineering applications.
Key Concepts
-
Definition of Derived Types:
- Derived types are user-defined data types that can contain multiple components of different types.
- They are defined using the
TYPEkeyword.
-
Components of Derived Types:
- Components can be of intrinsic types (e.g., INTEGER, REAL) or other derived types.
- Each component is declared with its type and name.
-
Creating and Using Derived Types:
- Once defined, derived types can be used to declare variables.
- Components of derived type variables are accessed using the
%operator.
Defining Derived Types
To define a derived type, use the TYPE and END TYPE statements. Here is a basic example:
In this example, Person is a derived type with three components: name, age, and height.
Declaring Variables of Derived Types
Once a derived type is defined, you can declare variables of that type:
Accessing Components
To access or modify the components of a derived type variable, use the % operator:
Practical Example
Let's create a simple program that uses a derived type to store and display information about a person.
PROGRAM DerivedTypeExample
IMPLICIT NONE
! Define the derived type
TYPE :: Person
CHARACTER(LEN=50) :: name
INTEGER :: age
REAL :: height
END TYPE Person
! Declare a variable of the derived type
TYPE(Person) :: person1
! Assign values to the components
person1%name = 'Alice Smith'
person1%age = 28
person1%height = 1.65
! Print the values
PRINT *, 'Name: ', person1%name
PRINT *, 'Age: ', person1%age
PRINT *, 'Height: ', person1%height
END PROGRAM DerivedTypeExampleExplanation
-
Defining the Derived Type:
- The
Persontype is defined with three components:name,age, andheight.
- The
-
Declaring a Variable:
- A variable
person1of typePersonis declared.
- A variable
-
Assigning Values:
- Values are assigned to the components of
person1using the%operator.
- Values are assigned to the components of
-
Printing Values:
- The values of the components are printed using the
PRINTstatement.
- The values of the components are printed using the
Exercises
Exercise 1: Define and Use a Derived Type
-
Define a derived type
Bookwith the following components:title(CHARACTER, length 100)author(CHARACTER, length 50)year(INTEGER)price(REAL)
-
Declare a variable of type
Bookand assign values to its components. -
Print the values of the components.
Solution
PROGRAM BookExample
IMPLICIT NONE
! Define the derived type
TYPE :: Book
CHARACTER(LEN=100) :: title
CHARACTER(LEN=50) :: author
INTEGER :: year
REAL :: price
END TYPE Book
! Declare a variable of the derived type
TYPE(Book) :: myBook
! Assign values to the components
myBook%title = 'Fortran Programming'
myBook%author = 'John Doe'
myBook%year = 2023
myBook%price = 59.99
! Print the values
PRINT *, 'Title: ', myBook%title
PRINT *, 'Author: ', myBook%author
PRINT *, 'Year: ', myBook%year
PRINT *, 'Price: ', myBook%price
END PROGRAM BookExampleExercise 2: Nested Derived Types
-
Define a derived type
Addresswith the following components:street(CHARACTER, length 100)city(CHARACTER, length 50)zipcode(INTEGER)
-
Define a derived type
Employeewith the following components:name(CHARACTER, length 50)id(INTEGER)address(of typeAddress)
-
Declare a variable of type
Employeeand assign values to its components, including the nestedaddresscomponent. -
Print the values of the components.
Solution
PROGRAM NestedDerivedTypeExample
IMPLICIT NONE
! Define the Address derived type
TYPE :: Address
CHARACTER(LEN=100) :: street
CHARACTER(LEN=50) :: city
INTEGER :: zipcode
END TYPE Address
! Define the Employee derived type
TYPE :: Employee
CHARACTER(LEN=50) :: name
INTEGER :: id
TYPE(Address) :: address
END TYPE Employee
! Declare a variable of the derived type
TYPE(Employee) :: employee1
! Assign values to the components
employee1%name = 'Bob Johnson'
employee1%id = 101
employee1%address%street = '123 Main St'
employee1%address%city = 'Anytown'
employee1%address%zipcode = 12345
! Print the values
PRINT *, 'Name: ', employee1%name
PRINT *, 'ID: ', employee1%id
PRINT *, 'Street: ', employee1%address%street
PRINT *, 'City: ', employee1%address%city
PRINT *, 'Zipcode: ', employee1%address%zipcode
END PROGRAM NestedDerivedTypeExampleCommon Mistakes and Tips
- Initialization: Ensure that all components of a derived type are properly initialized before use.
- Component Access: Use the
%operator correctly to access components of derived type variables. - Nested Types: When using nested derived types, ensure that each level of the type hierarchy is correctly accessed and assigned.
Conclusion
Derived types in Fortran provide a powerful way to create complex data structures that can model real-world entities. By understanding how to define, declare, and use derived types, you can write more organized and maintainable code. In the next section, we will explore pointers and their usage in Fortran, which will further enhance your ability to manage dynamic data structures.
Fortran Programming Course
Module 1: Introduction to Fortran
- Introduction to Fortran
- Setting Up the Development Environment
- Basic Syntax and Structure
- Writing Your First Fortran Program
Module 2: Basic Concepts
- Variables and Data Types
- Operators and Expressions
- Input and Output
- Control Structures: If Statements
- Control Structures: Loops
Module 3: Arrays and Strings
Module 4: Procedures and Functions
Module 5: Advanced Data Structures
Module 6: File Handling
Module 7: Advanced Topics
Module 8: Best Practices and Optimization
- Code Optimization Techniques
- Debugging and Profiling
- Writing Maintainable Code
- Fortran Standards and Portability
