0
0
Swiftprogramming~10 mins

Adding methods in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Adding methods
Define struct/class
Add method inside
Create instance
Call method on instance
Execute method code
Return or print result
This flow shows how to add a method inside a struct or class, create an instance, and call the method to run its code.
Execution Sample
Swift
struct Greeter {
    func sayHello() {
        print("Hello, friend!")
    }
}

let greeter = Greeter()
greeter.sayHello()
Defines a struct with a method that prints a greeting, then creates an instance and calls the method.
Execution Table
StepActionEvaluationResult
1Define struct Greeter with method sayHello()Method sayHello() readyNo output
2Create instance greeter = Greeter()Instance createdNo output
3Call greeter.sayHello()Method runsPrints: Hello, friend!
4End of programNo more codeProgram stops
💡 Program ends after method call completes
Variable Tracker
VariableStartAfter Step 2After Step 3Final
greeterundefinedGreeter instanceGreeter instanceGreeter instance
Key Moments - 2 Insights
Why do we need to create an instance before calling the method?
Because the method sayHello() is inside the struct, it belongs to instances. Step 2 creates the instance, so step 3 can call the method on it.
What happens if we call sayHello() without an instance?
The code would not compile because sayHello() is not a static method. The execution_table shows the method call only after instance creation.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed when greeter.sayHello() is called?
ANothing is printed
B"Goodbye!"
C"Hello, friend!"
DAn error message
💡 Hint
Check Step 3 in the execution_table where the method runs and prints output
At which step is the instance greeter created?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the variable_tracker and execution_table Step 2 where greeter is assigned
If sayHello() was static, what would change in the execution?
AWe would need to create two instances
BWe could call sayHello() without creating an instance
CThe method would print a different message
DThe program would not compile
💡 Hint
Static methods belong to the type, so no instance needed; see key_moments explanation
Concept Snapshot
Adding methods in Swift:
- Define method inside struct/class
- Create instance of struct/class
- Call method on instance
- Method runs code (e.g., print)
- Instance needed unless method is static
Full Transcript
This example shows how to add a method inside a Swift struct. First, we define a struct named Greeter with a method sayHello() that prints a greeting. Then, we create an instance of Greeter called greeter. Next, we call greeter.sayHello(), which runs the method and prints "Hello, friend!". The execution table traces each step: defining the struct, creating the instance, calling the method, and ending the program. The variable tracker shows the greeter instance is created at step 2 and used at step 3. Key moments clarify why an instance is needed to call the method and what happens if we try to call it without one. The visual quiz tests understanding of the printed output, instance creation step, and the effect of static methods. This helps beginners see clearly how methods are added and used in Swift.