0
0
Swiftprogramming~10 mins

Methods in structs in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Methods in structs
Define struct with properties
Add method inside struct
Create struct instance
Call method on instance
Method uses properties to compute or act
Return or print result
End
This flow shows how you define a struct with properties and methods, create an instance, call the method, and get the result.
Execution Sample
Swift
struct Rectangle {
    var width: Int
    var height: Int
    func area() -> Int {
        return width * height
    }
}
let rect = Rectangle(width: 3, height: 4)
print(rect.area())
This code defines a Rectangle struct with width and height, adds a method to calculate area, creates an instance, and prints the area.
Execution Table
StepActionProperties StateMethod CalledMethod ResultOutput
1Define struct Rectanglewidth: -, height: -No--
2Add method area()width: -, height: -No--
3Create instance rectwidth: 3, height: 4No--
4Call rect.area()width: 3, height: 4Yes3 * 4 = 12-
5Print resultwidth: 3, height: 4No1212
6Endwidth: 3, height: 4No--
💡 Method called once, result printed, program ends.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
rect.width-333
rect.height-444
area result--1212
Key Moments - 3 Insights
Why does the method area() use 'width' and 'height' without parameters?
Because methods inside structs can access the struct's own properties directly, as shown in step 4 of the execution_table.
Does calling rect.area() change the properties width or height?
No, the method only reads the properties to calculate area; it does not modify them, as seen in variable_tracker where width and height stay the same.
Why do we need to create an instance before calling the method?
Because methods in structs work on specific instances with their own property values, shown in step 3 creating rect before calling area in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the method result of rect.area()?
A7
B12
C0
DError
💡 Hint
Check the 'Method Result' column at step 4 in the execution_table.
At which step is the struct instance 'rect' created?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column in the execution_table for instance creation.
If we change rect.width to 5 before calling area(), what will the method result be at step 4?
A20
B7
C12
DError
💡 Hint
Recall area() returns width * height; changing width to 5 and height is 4 means 5*4.
Concept Snapshot
struct StructName {
  var property1: Type
  var property2: Type
  func methodName() -> ReturnType {
    // use properties to compute or act
  }
}
Create instance: let obj = StructName(...)
Call method: obj.methodName()
Full Transcript
This example shows how to define a struct with properties and a method in Swift. The method can use the struct's properties directly without needing parameters. We create an instance of the struct with specific values, then call the method on that instance. The method calculates and returns a value based on the instance's properties. The execution table traces each step: defining the struct, adding the method, creating an instance, calling the method, and printing the result. The variable tracker shows how the properties and method result change or stay the same. Key moments clarify common confusions about method access to properties, immutability during method calls, and the need for an instance. The quiz tests understanding of method results, instance creation, and effects of changing property values before method calls.