100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Derived Types

Learn how Fortran's TYPE construct lets you bundle related data of different kinds into a single named structure, and how to declare, initialize, and access it.

Modern FortranIntermediate9 min readJul 10, 2026
Analogies

What Is a Derived Type?

A derived type is a user-defined data structure declared with a TYPE ... END TYPE block that groups components of possibly different intrinsic types (INTEGER, REAL, CHARACTER, LOGICAL) or even other derived types under one name. Instead of tracking a particle's mass, position, and velocity as three separate, loosely related arrays, you define TYPE :: particle with components mass, position(3), and velocity(3), so the compiler enforces that every particle variable carries all three pieces of data together, reducing bookkeeping errors in large simulation codes.

🏏

Cricket analogy: Instead of tracking a batter's runs, balls faced, and strike rate as three unrelated scoreboards, a derived type is like a single player-scorecard entry, the way Virat Kohli's ODI stats are always reported as one linked record rather than scattered numbers.

Declaring and Accessing Components

Once a type is defined, you declare variables of that type with TYPE(particle) :: p1, p2 and reach individual components using the percent operator, as in p1%mass = 2.5 or p1%position = [0.0, 0.0, 1.0]. The compiler type-checks each component access, so assigning a CHARACTER value to a REAL component fails at compile time, and whole-object assignment such as p2 = p1 performs a memberwise copy of every component in one statement, which is far less error-prone than copying each field individually across parallel arrays.

🏏

Cricket analogy: Accessing p1%mass is like a scorer writing kohli%centuries on a stat sheet, drilling into one named field of a bigger record rather than guessing which loose number belongs to which player.

Composition: Arrays and Nested Types

Components can themselves be arrays, allocatable arrays, or other derived types, which lets you build composite structures such as TYPE :: simulation containing an allocatable array of TYPE(particle) :: bodies(:). Default component values can be set at declaration time inside the TYPE block, for example REAL :: mass = 1.0, so every new particle starts with a sensible default unless overridden, and nested access chains like sim%bodies(3)%position(1) let you drill into deeply composed structures using repeated percent and array-index operators.

🏏

Cricket analogy: A team object holding an array of eleven player derived types, like India's ODI squad list where each entry is itself a full player record, mirrors sim%bodies(:) holding an array of particle types.

Type-Bound Procedures and Assignment Semantics

Since Fortran 2003, a TYPE block can include a CONTAINS section that binds procedures to the type, so p1%kineticEnergy() can compute a value using the object's own components without an external function call syntax; this is the gateway into Fortran's object-oriented features covered in a later topic. A subtler point is that default assignment (p2 = p1) is a shallow, component-by-component copy, so if a component is a POINTER, both p1 and p2 end up pointing at the same target after assignment, which can cause unintended aliasing unless you define a custom assignment via generic interfaces or deep-copy explicitly.

🏏

Cricket analogy: Binding p1%kineticEnergy() to the particle type is like a stats app offering kohli.strikeRate() as a built-in function on the player object rather than a separate loose calculation elsewhere in the code.

Default assignment between derived-type variables copies pointer components by reference, not by value. If TYPE :: particle has a POINTER component, p2 = p1 leaves p1 and p2 sharing the same target, so modifying it through one variable silently changes what the other sees. Define a custom assignment (generic interface) or deep-copy pointer components explicitly when this aliasing is not intended.

Derived type components can carry default initial values directly in the TYPE block, e.g. REAL :: mass = 1.0. Combined with ALLOCATABLE array components, this makes it easy to build safe, self-initializing data structures for simulation state without relying on separate initialization subroutines.

  • TYPE ... END TYPE defines a named structure bundling components of different intrinsic or derived types.
  • Declare variables with TYPE(name) :: var and access components using the percent operator, e.g. p1%mass.
  • Components can be arrays, allocatable arrays, or other derived types, enabling composite structures.
  • Default component values can be specified directly in the TYPE block declaration.
  • Default assignment (p2 = p1) performs a memberwise copy, but pointer components are copied by reference, causing aliasing.
  • CONTAINS inside a TYPE block enables type-bound procedures, the foundation of Fortran's OOP support.
  • Nested access chains like sim%bodies(3)%position(1) combine percent operators with array indexing to reach deeply composed data.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#FortranStudyNotes#DerivedTypes#Derived#Types#Type#Declaring#StudyNotes#SkillVeris#ExamPrep