é¢æ°ã®å®£è¨
ãé¢æ°ã¯æ¬¡ã®ãããªæ§æã§å®£è¨ããã¾ãã
func é¢æ°å (å¼æ°) -> è¿ãå¤ã®å { æ return è¿ãå¤ }
ãè¿ãå¤ãVoidï¼ãã®å®ä½ã¯ç©ºã¿ãã«()ã®ã·ã³ã¿ãã¯ã¹ã·ã¥ã¬ã¼ï¼ã®ã¨ãã¯ãè¿ãå¤ãçç¥ãã¦æ¬¡ã®ããã«æ¸ããã¨ãã§ãã¾ãã
func é¢æ°å (å¼æ°) { æ //returnãçãã }
ãã·ã³ã¿ãã¯ã¹ã·ã¥ã¬ã¼ï¼æ§æç³è¡£ï¼ã¨ã¯ãæ§æã®èªã¿æ¸ããç°¡åã«ããããã«æä¾ãããç¥è¨æ³ã®ãã¨ã§ããå é¨ã§ã¯ããã§ã«å®ç¾©ããããã¨ãã¨ã®æ§æã«ç½®ãæãããã¾ãã
ãé¢æ°ãå®ç¾©ããä¾ãè¦ã¦ã¿ã¾ããããUIntã¯éè² æ´æ°åã表ãã¾ãã
func makeStrLower(str: String) -> String { return str.lowercaseString; } var lowerStr = makeStrLower("Hello, World!") // hello, world! func multiprint(content: String, times: UInt) { for _ in 1...times { print(content) } } multiprint("Good!! ", 10) // Good!! Good!! Good!! Good!! Good!! Good!! Good!! Good!! Good!! Good!!
ãmultiprinté¢æ°å ã«ããforæã®ã_ãã¯ã¯ã¤ã«ãã«ã¼ãã¨å¼ã°ããä»£å ¥ã®éãä»å¾ãã®å¤ã使ããªããã¨ãæ示ããããã®è¨æ³ã§ããä¾ãã°ããã®foræã®ä¸ã§ã¯ä½åprintãè¡ããã«ã®ã¿é¢å¿ããããã¬ã³ã¸ããåå¾ã§ããåã ã®å¤ã¯ä½¿ããªããããã¯ã¤ã«ãã«ã¼ãã«ã¬ã³ã¸ã®å¤ãä»£å ¥ãã¦ãã¾ãã
ãObjecive-Cã«ã¯ãªãç¹å¾´ã¨ãã¦ãè¤æ°ã®æ°ãã¿ãã«ã«ãããã§å¼ã³åºãå ã¸è¿ãããã¨ãæãããã¾ãã
func countwhite(str: String) -> (charCount: Int, whiteCount: Int) { var charCount = 0, whiteCount = 0 for char in str { if char == " " || char == "\t" { whiteCount++ } charCount++ } return (charCount, whiteCount) } countwhite("If you want to make enemies, try to change something.") // (53, 9)
ããã®countwhiteé¢æ°ã¯ãå¼æ°ã¨ãã¦æååãä¸ããã¨ãè¿ãå¤ã¨ãã¦1ã¤ã®ã¿ãã«ãè¿ãã¾ããã¿ãã«ã®1ã¤ç®ã®è¦ç´ ã«ã¯Intã¨ãã¦æåç·æ°ãã2ã¤ç®ã®è¦ç´ ã«ã¯Intã¨ãã¦ã¹ãã¼ã¹ã»ã¿ãæåã®ç·æ°ãå ¥ã£ã¦ãã¾ãã