How to pass a nested struct to a function in Swift

1 Answer

0 votes
import Foundation

struct Address {
    let city: String
    let zipCode: String
}

struct Person {
    let name: String
    let age: Int
    let address: Address
}

func printPersonInfo(_ person: Person) {
    print("Name: \(person.name)")
    print("Age: \(person.age)")
    print("City: \(person.address.city)")
    print("Zip Code: \(person.address.zipCode)")
}

let home = Address(city: "Nebula Vortex", zipCode: "345645.09604.8471651.172561")

let p = Person(name: "Axton ", age: 37, address: home)

printPersonInfo(p)



/*
run:

Name: Axton 
Age: 37
City: Nebula Vortex
Zip Code: 345645.09604.8471651.172561

*/

 



answered Nov 1, 2025 by avibootz
edited Nov 1, 2025 by avibootz

Related questions

...