Quick—what kind of property is this?

a.femurToHeight

If you thought:

“That’s just reading a property from an instance of a structure.”

…you’re absolutely right.

And that’s the point: reading a computed property looks exactly the same as reading a stored property.

The difference is where the value comes from.

  • Stored property: a value saved inside the instance when it was created.
  • Computed property: a value calculated on demand from stored values each time you access it.

Stored properties

In a new Xcode Playground named ComputedProperties please copy this code and run it:

// Define the structure
struct Person {
	// MARK: Stored properties
    let heightCm: Double
    let armSpanCm: Double
    let femurCm: Double
    let headLengthCm: Double
}
 
// Sample instances (use realistic numbers or your own)
let a = Person(heightCm: 170, armSpanCm: 171, femurCm: 43, headLengthCm: 21)
let b = Person(heightCm: 162, armSpanCm: 160, femurCm: 40, headLengthCm: 19)
let c = Person(heightCm: 180, armSpanCm: 182, femurCm: 46, headLengthCm: 22)
 
// Query STORED properties (each line shows a result in the sidebar):
a.heightCm
a.armSpanCm
a.femurCm
a.headLengthCm
 
b.heightCm
b.armSpanCm
b.femurCm
b.headLengthCm
 
c.heightCm
c.armSpanCm
c.femurCm
c.headLengthCm

What to notice: each line simply reads a value that was stored inside a, b, or c when the instance was created.

Computed propertes

Now we will add computed properties to represent common human body ratios.

Common body‑ratio benchmarks (rough guides):

  • arm span : height ≈ 1.00
  • femur : height ≈ 0.25
  • head length : height ≈ 0.10–0.125 (1/10 to 1/8)

With computed properties Swift calculates these ratios from the stored values every time you access them.

Replace your Person structure with this version below (same stored properties + 3 computed properties).

NOTE

Note the explicit return used in each computed property.

// Define the structure
struct Person {
	// MARK: Stored properties
    let heightCm: Double
    let armSpanCm: Double
    let femurCm: Double
    let headLengthCm: Double
 
    // MARK: Computed properties
    var armSpanToHeight: Double {
        return armSpanCm / heightCm
    }
 
    var femurToHeight: Double {
        return femurCm / heightCm
    }
 
    var headToHeight: Double {
        return headLengthCm / heightCm
    }
}
 
// Re-create the same examples
let a = Person(heightCm: 170, armSpanCm: 171, femurCm: 43, headLengthCm: 21)
let b = Person(heightCm: 162, armSpanCm: 160, femurCm: 40, headLengthCm: 19)
let c = Person(heightCm: 180, armSpanCm: 182, femurCm: 46, headLengthCm: 22)
 
// Query COMPUTED properties
a.armSpanToHeight
a.femurToHeight
a.headToHeight
 
b.armSpanToHeight
b.femurToHeight
b.headToHeight
 
c.armSpanToHeight
c.femurToHeight
c.headToHeight

What is a computed property?

A computed property looks like any other property when you use it, but it doesn’t store a value inside the instance.

Instead, it calculates a value from other stored properties each time you access it.

var armSpanToHeight: Double {
    return armSpanCm / heightCm
}
  • There’s no memory in the computer that is used to store armSpanToHeight.
  • When the line a.armSpanToHeight runs, Swift executes the code inside { ... }, does the math, and produces a fresh value right then.
  • If the stored values changed (in a mutable instance), you’d get an updated result next time you read the computed property.

Key idea: Access looks the same (a.heightCm vs a.armSpanToHeight), but computed properties are derived on demand from the stored data.

What does return do here?

The body of a computed property contains instructions for producing its value.
The return keyword marks the result to hand back.

var femurToHeight: Double {
    return femurCm / heightCm
}

When Swift evaluates b.femurToHeight:

  1. It jumps into the braces { ... }.
  2. It evaluates femurCm / heightCm.
  3. It returns that number as the property’s value.
  4. The Results sidebar shows the value on the line where you wrote b.femurToHeight.

Think of return as the answer line: whatever expression follows it is what the outside world receives when the property is accessed.

Exercises

Using real data from the measurements we took earlier:

  1. Three classmates
    Create three new Person instances using real measurements from your class. For each person, query the stored values (heightCm, armSpanCm, femurCm, headLengthCm) and the computed ratios (armSpanToHeight, femurToHeight, headToHeight).
    Observe that accessing stored vs. computed looks identical—just a property name on a line.

  2. Closest to 1:1
    Compare each person’s armSpanToHeight value. Which one is closest to 1.00?

  3. Quarter‑height femur
    Compare femurToHeight for your people. Who is nearest 0.25? Note the value.

  4. Head proportion band
    For headToHeight, is each person closer to 0.10 (1/10) or 0.125 (1/8)? Write a short observation for each person.