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

  1. Definition of Derived Types:

    • Derived types are user-defined data types that can contain multiple components of different types.
    • They are defined using the TYPE keyword.
  2. 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.
  3. 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:

TYPE :: Person
    CHARACTER(LEN=50) :: name
    INTEGER :: age
    REAL :: height
END TYPE Person

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:

TYPE(Person) :: person1, person2

Accessing Components

To access or modify the components of a derived type variable, use the % operator:

person1%name = 'John Doe'
person1%age = 30
person1%height = 1.75

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 DerivedTypeExample

Explanation

  1. Defining the Derived Type:

    • The Person type is defined with three components: name, age, and height.
  2. Declaring a Variable:

    • A variable person1 of type Person is declared.
  3. Assigning Values:

    • Values are assigned to the components of person1 using the % operator.
  4. Printing Values:

    • The values of the components are printed using the PRINT statement.

Exercises

Exercise 1: Define and Use a Derived Type

  1. Define a derived type Book with the following components:

    • title (CHARACTER, length 100)
    • author (CHARACTER, length 50)
    • year (INTEGER)
    • price (REAL)
  2. Declare a variable of type Book and assign values to its components.

  3. 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 BookExample

Exercise 2: Nested Derived Types

  1. Define a derived type Address with the following components:

    • street (CHARACTER, length 100)
    • city (CHARACTER, length 50)
    • zipcode (INTEGER)
  2. Define a derived type Employee with the following components:

    • name (CHARACTER, length 50)
    • id (INTEGER)
    • address (of type Address)
  3. Declare a variable of type Employee and assign values to its components, including the nested address component.

  4. 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 NestedDerivedTypeExample

Common 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.

© Copyright 2024. All rights reserved