Sanghyuk's Dev

[Swift] Function

iOS/Swift2018. 5. 18. 18:53


※ Function

- 일련의 작업을 수행하는 코드 묶음을 식별할 수 있는 특정한 이름을 부여하여 사용하는 것

- 유형

    - Input 과 Output 이 모두 있는 것 (Function)

    - Input 이 없고 Output 만 있는 것 (Generator)

    - Input 이 있고 Output 은 있는 것 (Consumer)

    - Input 과 Output 이 모두 없는 것


- Function Syntax

func functionName(parameterList) -> ReturnType {

statements

}


- Parameter Declaration Syntax

(name1: Type, name: Type)


func greet(person: String) -> String {

let greeting = "Hello, " + person + "!"

return greeting

}

greet(person: "Anna")

greet(person: "Brian")


- Combine the message creation and the return statement into one line

func greetAgain(preson: String) -> String {

return "Hello again, " + person + "!"

}

greetAgain(person: "Anna")


- Function with Multiple Parameters

func addNumbers(a: Int,, b: Int) > Int {

return a + b

}

addNumbers(a: 10, b:20)

addNumbers(a: 3, b:5)


※ Argument Label

- Argument Label Declaration Syntax

(label name: Type)

(name: Type)


- Calling Function with Argument Label

functionName(label: expression)


- Function Argument Labels and Parameter Names

By default, parameters use their parameter name as their argument label.

func someFunction(firstParameterName: Int, secondParameterName: Int) {

print(firstParameterName, secondParameterName)

}

someFunction(firstParameterName: 1, secondParameterName: 2)


- Omitting Argument Labels

func someFunction(_ firstParameterName: Int, secondParameterName: Int) {

print(firstParameterName, secondParameterName)

}

someFunction(1, secondParameterName: 2)


- Specifying Argument Labels

func someFunction(argumentLabel parameterName: Int) {

// parameterName refers to the argument value for that parameter

print(parameterName)

}

someFunction(argumentLabel: 10)


- Default Parameter Values

func functionWithDefault{paramWithoutDefault: Int, paramWithDefault: Int = 12) -> Int {

return paramWithDefault

}

functionWithDefault(paramWithoutDefault: 3, paramWithDefault: 6)

functionWithDefault(paramWithoutDefault: 4)


- Variadic Parameters

Variadic Parameters cannot be marked as inout.

func arithmeticMean(_ numbers: Double...) -> Double {

var total = 0.0

for number in numbers {

toral =+ number

}

return total / Double(numbers.count)

}

arithmeticMean(1, 2, 3, 4, 5)

arithmeticMean(3, 8.25, 18.75)


※ Argument vs Parameter

- Parameter

- 매개변수 = 인자 = Parameter

- 인수를 담는 변수의 한 종류로서 해당 함수 내부 스코프에만 영향

- 대부분 call by value 가 기본

- call by reference 는 호출할 때 사용한 전달인자에까지 영향

- Argument

- 전달인자 = 인수 = 실인수 = Argument

- 함수 호출 시 그에 필요한 데이터를 전달


- Functions Without Parameters

func sayHelloWorld() -> String {

return "Hello, world"

}

print(sayHelloWorld())

print("Hello, world")


- Functions without Return Values

func say(number: Int) {

print(number)

}

func say(word: String) -> Void {

print(word)

}

func say(something: String)  -> () {

print(something)

}

say(number: 1)

say(word: "1")

say(something: "1")


- Nested Functions

외부에는 숨기고 함수 내부에서만 사용할 함수를 중첩하여 사용 가능

func chooseStepFunction(backward: Bool, value: Int) -> Int {

func stepForward(input: Int) -> Int {

return input + 1

}

func stepBackward(input: Int) -> Int {

return input -1

}


if backward {

return stepBackward(input: value)

} else {

return stepForward(input: value)

}

}

var value = 4

chooseStepFunction(backward: true, value: value)

chooseStepFunction(backward: false, value: value)



'iOS > Swift' 카테고리의 다른 글

[Swift] Tuples  (0) 2018.05.23
[Swift] Loops  (0) 2018.05.23
[Swift] Conditional Statements  (0) 2018.05.23
[Swift] Basic Operators  (0) 2018.05.17
[Swift] 기본 문법  (0) 2018.05.14