Hello Swift 3/5 - Function

37
Welcome to Swift 3/5 func sayHello(name name: String) { println("Hello \(name)") } 1

description

Function in Swift Programming Language

Transcript of Hello Swift 3/5 - Function

Page 1: Hello Swift 3/5 - Function

Welcome to Swift 3/5

func sayHello(name name: String) { println("Hello \(name)")}

1

Page 2: Hello Swift 3/5 - Function

•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

Page 3: Hello Swift 3/5 - Function

Function’s format

func helloFunc(param:String)->String { let hello = "hello "+param return hello}

3

Page 4: Hello Swift 3/5 - Function

Function’s format

func helloFunc(param:String)->String { let hello = "hello "+param return hello}

// declare function

4

Page 5: Hello Swift 3/5 - Function

Function’s format

func helloFunc(param:String)->String { let hello = "hello "+param return hello}

// function name

5

Page 6: Hello Swift 3/5 - Function

Function’s format

func helloFunc(param:String)->String { let hello = "hello "+param return hello}

// parameter’s name and parameter’s type

6

Page 7: Hello Swift 3/5 - Function

Function’s format

func helloFunc(param:String)->String { let hello = "hello "+param return hello}

// function’s return type

7

Page 8: Hello Swift 3/5 - Function

Function’s format

func helloFunc(param:String)->String { let hello = "hello "+param return hello}

//functions’s scope

8

Page 9: Hello Swift 3/5 - Function

Function’s format

func helloFunc(param:String)->String { let hello = "hello "+param return hello}

//function’s logic

9

Page 10: Hello Swift 3/5 - Function

Function’s format

func helloFunc(param:String)->String { let hello = "hello "+param return hello}

//return value in logic

10

Page 11: Hello Swift 3/5 - Function

Using the function

var returnValue = helloFunc("Cody")

11

Page 12: Hello Swift 3/5 - Function

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

Page 13: Hello Swift 3/5 - Function

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

Page 14: Hello Swift 3/5 - Function

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

Page 15: Hello Swift 3/5 - Function

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

Page 16: Hello Swift 3/5 - Function

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

Page 17: Hello Swift 3/5 - Function

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

Page 18: Hello Swift 3/5 - Function

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

Page 19: Hello Swift 3/5 - Function

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

Page 20: Hello Swift 3/5 - Function

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

Page 21: Hello Swift 3/5 - Function

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

Page 22: Hello Swift 3/5 - Function

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

Page 23: Hello Swift 3/5 - Function

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

Page 24: Hello Swift 3/5 - Function

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

Page 25: Hello Swift 3/5 - Function

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

Page 26: Hello Swift 3/5 - Function

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

Page 27: Hello Swift 3/5 - Function

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

Page 28: Hello Swift 3/5 - Function

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

Page 29: Hello Swift 3/5 - Function

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

Page 30: Hello Swift 3/5 - Function

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

Page 31: Hello Swift 3/5 - Function

Function types

func addTwoInts(a: Int, b: Int) -> Int { return a + b}

var mathFunction: (Int, Int) -> Int = addTwoInts

mathFunction(10,20)

31

Page 32: Hello Swift 3/5 - Function

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

Page 33: Hello Swift 3/5 - Function

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

Page 34: Hello Swift 3/5 - Function

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

Page 35: Hello Swift 3/5 - Function

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

Page 36: Hello Swift 3/5 - Function

내일은?

•Enumeration

•Closures

•Structures and Classes

- Initializers and Deinitialization in Classes

- Properties

- Subscripts

- Inheritance

- Subclassing

- Overriding and Preventing Overrides

36

Page 37: Hello Swift 3/5 - Function

감사합니다.

let writer = ["Cody":"Yun"]

37