๐ŸŽ iOS/Swift

[Swift] ๋งค๊ฐœ๋ณ€์ˆ˜/์ „๋‹ฌ์ธ์ž ๋ ˆ์ด๋ธ”/๊ฐ€๋ณ€ ๋งค๊ฐœ๋ณ€์ˆ˜

๊ธฐ๋ณธ ๋งค๊ฐœ๋ณ€์ˆ˜

  •  ๊ธฐ๋ณธ๊ฐ’์„ ๊ฐ–๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ชฉ๋ก ์ค‘์— ๋’ค์ชฝ์— ์œ„์น˜ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
func greeting(friend: String, me: String: "Finit"){

	print("Hello \(friend)! I'm \(me)")
    
}

 

  • ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ธฐ๋ณธ๊ฐ’์„ ๊ฐ–๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.
greeting(friend: "finit")

 

์ „๋‹ฌ์ธ์ž ๋ ˆ์ด๋ธ”

  • ์ „๋‹ฌ์ธ์ž ๋ ˆ์ด๋ธ”์€ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์—ญํ• ์„ ์ข€ ๋” ๋ช…ํ™•ํ•˜๊ฒŒ ํ•˜๊ฑฐ๋‚˜ ํ•จ์ˆ˜ ์‚ฌ์šฉ์ž์˜ ์ž…์žฅ์—์„œ ํ‘œํ˜„ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์ „๋‹ฌ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•œ๋‹ค.
func greeting(to friend: String, from me: String){

	print("Hello \(friend)! I'm \(me).")
    
}

 

  • ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋Š” ์ „๋‹ฌ์ธ์ž ๋ ˆ์ด๋ธ”์„ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.
greeting(to: "hana", from: "finit")

 

๊ฐ€๋ณ€ ๋งค๊ฐœ๋ณ€์ˆ˜

  • ์ „๋‹ฌ ๋ฐ›์„ ๊ฐ’์˜ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๊ธฐ ์–ด๋ ค์šธ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๊ฐ€๋ณ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜ ๋‹น ํ•˜๋‚˜๋งŒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
func sayHelloToFriends(me: String, friends: String...) -> String{

	return "Hello \(friends)! I'm \(me).")
    
}

print(sayHelloToFriends(me: "finit", friends: "a","b","c"))
// Hello ["a","b","c"]! I'm finit.

print(sayHelloToFriends(me: "finit"))
// Hello []! I'm finit.