-
[Swift] ํจ์(Func) - ๊ธฐ๋ณธiOS/Swift 2022. 7. 25. 17:26
๐กํจ์ ์ ์ธ์ ๊ธฐ๋ณธ ํํ
func ํจ์์ด๋ฆ(๋งค๊ฐ๋ณ์์ด๋ฆ1: ๋งค๊ฐ๋ณ์1ํ์ , ๋งค๊ฐ๋ณ์์ด๋ฆ2: ๋งค๊ฐ๋ณ์2ํ์ ...) -> ๋ฐํํ์ { ํจ์ ๊ตฌํ๋ถ return ๋ฐํ๊ฐ } func sum(a: Int, b: Int) -> Int { return a + b }
๐กํจ์์ ํธ์ถ
sum(a:3, b:5) //8 print (sum(a:3, b:5))
์์ค์ฝ๋
// // Function.swift // iOS study // // Created by yeonsu on 2022/07/25. // import Swift // ================ < ํจ์์ ์ ์ธ > ================ // [ํจ์ ์ ์ธ์ ๊ธฐ๋ณธ ํํ] /* func ํจ์์ด๋ฆ(๋งค๊ฐ๋ณ์์ด๋ฆ1: ๋งค๊ฐ๋ณ์1ํ์ , ๋งค๊ฐ๋ณ์์ด๋ฆ2: ๋งค๊ฐ๋ณ์2ํ์ ...) -> ๋ฐํํ์ { ํจ์ ๊ตฌํ๋ถ return ๋ฐํ๊ฐ } */ func sum(a: Int, b: Int) -> Int { return a + b } // [๋ฐํ ๊ฐ์ด ์๋ ํจ์] /* func ํจ์์ด๋ฆ(๋งค๊ฐ๋ณ์์ด๋ฆ1: ๋งค๊ฐ๋ณ์1ํ์ , ๋งค๊ฐ๋ณ์์ด๋ฆ2: ๋งค๊ฐ๋ณ์2ํ์ ...) -> Void { ํจ์ ๊ตฌํ๋ถ return ๋ฐํ๊ฐ } */ func printMyNmae(name: String) -> Void { // ์์ Void๋ฅผ ์๋ตํ๋ ๊ฒ๋ ๊ฐ๋ฅ print(name) } // [๋งค๊ฐ๋ณ์์ ๋ฐํ๊ฐ์ด ์๋ ํจ์] /* func ํจ์์ด๋ฆ() -> Void { //Void ์๋ต ๊ฐ๋ฅ ํจ์ ๊ตฌํ๋ถ return ๋ฐํ๊ฐ } */ func hello() { print ("hello")} // ================ < ํจ์์ ํธ์ถ > ================ sum(a:3, b:5) //8 print (sum(a:3, b:5)) printMyNmae(name: "yeonsu") hello()
์ถ๋ ฅ๊ฐ
'iOS > Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] ์ต์ ๋(Optional) (0) 2022.07.26 [Swift] ์กฐ๊ฑด๋ฌธ(Conditional), ๋ฐ๋ณต๋ฌธ(Loop) (0) 2022.07.26 [Swift] ํจ์(Func) - ๊ณ ๊ธ (0) 2022.07.25 [Swift] ์ปฌ๋ ์ ํ์ (Array, Dictionary, Set) (0) 2022.07.25 [Swift] ๊ธฐ๋ณธ ๋ฌธ๋ฒ ์ ๋ฆฌ (0) 2022.07.24