hello swift 3/5 - function
DESCRIPTION
Function in Swift Programming LanguageTRANSCRIPT
Welcome to Swift 3/5
func sayHello(name name: String) { println("Hello \(name)")}
1
•Function’s format•Using the function•Function with Tuple
•External Parameter Names•Default Parameter Value
•Shorthand External Parameter Names•Multiple Parameter•In-Out Parameter
•Function type in Parameter and Return Type
다룰 내용
2
Function’s format
func helloFunc(param:String)->String { let hello = "hello "+param return hello}
3
Function’s format
func helloFunc(param:String)->String { let hello = "hello "+param return hello}
// declare function
4
Function’s format
func helloFunc(param:String)->String { let hello = "hello "+param return hello}
// function name
5
Function’s format
func helloFunc(param:String)->String { let hello = "hello "+param return hello}
// parameter’s name and parameter’s type
6
Function’s format
func helloFunc(param:String)->String { let hello = "hello "+param return hello}
// function’s return type
7
Function’s format
func helloFunc(param:String)->String { let hello = "hello "+param return hello}
//functions’s scope
8
Function’s format
func helloFunc(param:String)->String { let hello = "hello "+param return hello}
//function’s logic
9
Function’s format
func helloFunc(param:String)->String { let hello = "hello "+param return hello}
//return value in logic
10
Using the function
var returnValue = helloFunc("Cody")
11
Function with Tuple
import Foundation
func randomFunc(paramName:String)->(msg:String,random:Int) { let hello = "hello "+paramName let random = Int(arc4random()%8) return (hello,random)}
12
Function with Tuple
import Foundation
func randomFunc(paramName:String)->(msg:String,random:Int) { let hello = "hello "+paramName let random = Int(arc4random()%8) return (hello,random)}
13
Function with Tuple
import Foundation
func randomFunc(paramName:String)->(msg:String,random:Int) { let hello = "hello "+paramName let random = Int(arc4random()%8) return (hello,random)}
let tupleResult = randomFunc("Cody")tupleResult.randomtupleResult.msg
14
Function with external parameter names
func join(s1: String, s2: String, s3: String) -> String { return s1 + s3 + s2}
func join(header s1: String, tail s2: String, joiner s3: String) -> String { return s1 + s3 + s2}
// What is different?
15
Function with external parameter names
func join(s1: String, s2: String, s3: String) -> String { return s1 + s3 + s2}
func join(header s1: String, tail s2: String, joiner s3: String) -> String { return s1 + s3 + s2}
// What is different?
16
Function with external parameter names
func join(s1: String, s2: String, s3: String) -> String { return s1 + s3 + s2}
func join(header s1: String, tail s2: String, joiner s3: String) -> String { return s1 + s3 + s2}
// Header, tail and join will use for calling the function
17
Function with external parameter names
func join(s1: String, s2: String, s3: String) -> String { return s1 + s3 + s2}
func join(header s1: String, tail s2: String, joiner s3: String) -> String { return s1 + s3 + s2}
// Header, tail and join will use for calling the function
join(header: "hello", tail: "world", joiner: ", ")
18
Function with external parameter names
func join(s1: String, s2: String, s3: String) -> String { return s1 + s3 + s2}
func join(header s1: String, tail s2: String, joiner s3: String) -> String { return s1 + s3 + s2}
// Header, tail and join will use for calling the function
join(header: "hello", tail: "world", joiner: ", ")
// When call function without external paramter namesjoin("hello","world",", ")Missing argument labels 'header:tail:joiner:' in call
19
Function with shorthand external parameter names
func join(header s1: String, tail s2: String, joiner s3: String) -> String { return s1 + s3 + s2}
func join(#header: String, #tail: String, #joiner: String) -> String { return header + joiner + tail}
// What is different?
20
Function with shorthand external parameter names
func join(header s1: String, tail s2: String, joiner s3: String) -> String { return s1 + s3 + s2}
func join(#header: String, #tail: String, #joiner: String) -> String { return header + joiner + tail}
// What is different?
// header s1 > #header// tail s2 > #tail// joiner s3 > #joiner// External parameter name is equal with local parameter name.
21
Function with default parameter value
func join(header s1: String, tail s2: String, joiner s3: String=" ") -> String { return s1 + s3 + s2}
join(s1: "hello", s2: "world", s3: ", ")join(s1: "hello", s2: "world")
22
Function with constant value
func sayHello(let param:String)->String { param = "test"
let hello = "hello "+param return hello}
let name = "Cody"var returnValue = sayHello(name)
23
Function with constant value
func helloFunc(let param:String)->String { param = "test"
let hello = "hello "+param return hello}
let name = "Cody"var returnValue = helloFunc(name)
Cannot assign to ‘let’ value ‘param’
24
Function with multiple parameter
func sum(numbers: Int...) -> Int { var total: Int = 0 for number in numbers { total += number } return total}
sum(1, 2, 3, 4, 5)
25
Function with multiple parameter
func sum(numbers: Int...) -> Int { var total: Int = 0 for number in numbers { total += number } return total}
sum(1, 2, 3, 4, 5)
26
Function with in-out parameter
func swapTwoInt(inout a: Int, inout b: Int) { let temp = a a = b b = temp}
var numA = 1var numB = 2swapTwoInts(&numA,&numB)numAnumB
// In C++ called “call by reference”
27
Function with in-out parameter
// 물론 in-out parameter를 shorthands external parameter name과// 함께 사용할 수도 있습니다.
func swapTwoInt(inout #a: Int, inout #b: Int) { let temp = a a = b b = temp}
var numA = 1var numB = 2swapTwoInts(a:&numA,b:&numB)numAnumB
28
Function with in-out parameter
func swapTwoInt(inout a: Int, inout b: Int) { let temp = a a = b b = temp}
var numA = 1var numB = 2swapTwoInts(&numA,&numB)numAnumB
// In C++ called “call by reference”
29
Function with in-out parameter
// 물론 in-out parameter를 shorthands external parameter name과// 함께 사용할 수도 있습니다.
func swapTwoInt(inout #a: Int, inout #b: Int) { let temp = a a = b b = temp}
var numA = 1var numB = 2swapTwoInts(a:&numA,b:&numB)numAnumB
30
Function types
func addTwoInts(a: Int, b: Int) -> Int { return a + b}
var mathFunction: (Int, Int) -> Int = addTwoInts
mathFunction(10,20)
31
Function types in parameter
func printFunctionResult(mathFunction: (Int, Int) -> Int) { println("Result is \(mathFunction(10,10))")}
func addTwoInts(a: Int, b: Int) -> Int { return a + b}
var mathFunction: (Int, Int) -> Int = addTwoInts
printFunctionResult(mathFunction)
32
Function types in parameter
// 사실 function을 변수에 assign해서 넘길 필요가 없어요func printFunctionResult(mathFunction: (Int, Int) -> Int) { println("Result is \(mathFunction(10,10))")}
func addTwoInts(a: Int, b: Int) -> Int { return a + b}
printFunctionResult(addTwoInts)
33
Function types in return type
func stepForward(input: Int) -> Int { return input + 1}func stepBackward(input: Int) -> Int { return input - 1}
func chooseStepFunction(backwards: Bool) -> (Int) -> Int { return backwards ? stepBackward : stepForward}
34
Nested function
func chooseStepFunction(backwards: Bool) -> (Int) -> Int { func stepForward(input: Int) -> Int { return input + 1 } func stepBackward(input: Int) -> Int { return input - 1 } return backwards ? stepBackward : stepForward}
35
내일은?
•Enumeration
•Closures
•Structures and Classes
- Initializers and Deinitialization in Classes
- Properties
- Subscripts
- Inheritance
- Subclassing
- Overriding and Preventing Overrides
36
감사합니다.
let writer = ["Cody":"Yun"]
37