SlideShare a Scribd company logo
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.random
tupleResult.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 names
join("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 = 1
var numB = 2
swapTwoInts(&numA,&numB)
numA
numB
// 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 = 1
var numB = 2
swapTwoInts(a:&numA,b:&numB)
numA
numB
28
Function with in-out parameter
func swapTwoInt(inout a: Int, inout b: Int) {
let temp = a
a = b
b = temp
}
var numA = 1
var numB = 2
swapTwoInts(&numA,&numB)
numA
numB
// 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 = 1
var numB = 2
swapTwoInts(a:&numA,b:&numB)
numA
numB
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

More Related Content

PDF
Swift the implicit parts
PDF
GeoGebra JavaScript CheatSheet
PDF
Implementing virtual machines in go & c 2018 redux
PDF
Imugi: Compiler made with Python
PDF
Swiftの関数型っぽい部分
PDF
A swift introduction to Swift
PDF
Introduction to Swift programming language.
ODP
Scala 2 + 2 > 4
Swift the implicit parts
GeoGebra JavaScript CheatSheet
Implementing virtual machines in go & c 2018 redux
Imugi: Compiler made with Python
Swiftの関数型っぽい部分
A swift introduction to Swift
Introduction to Swift programming language.
Scala 2 + 2 > 4

What's hot (20)

PDF
The Ring programming language version 1.4.1 book - Part 22 of 31
PDF
The Ring programming language version 1.6 book - Part 84 of 189
PDF
Python meetup: coroutines, event loops, and non-blocking I/O
PPT
About Go
PPTX
A Few Interesting Things in Apple's Swift Programming Language
PDF
Hacking Parse.y with ujihisa
PDF
6. Generics. Collections. Streams
PDF
Go a crash course
PDF
Code Generation in PHP - PHPConf 2015
PDF
Swift Programming Language
PDF
Are we ready to Go?
PPTX
Load-time Hacking using LD_PRELOAD
PDF
Implementing Software Machines in C and Go
PDF
8 arrays and pointers
PDF
PHP 8.1: Enums
PDF
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
PDF
All I know about rsc.io/c2go
PPTX
A Functional Guide to Cat Herding with PHP Generators
PDF
An introduction to functional programming with go
The Ring programming language version 1.4.1 book - Part 22 of 31
The Ring programming language version 1.6 book - Part 84 of 189
Python meetup: coroutines, event loops, and non-blocking I/O
About Go
A Few Interesting Things in Apple's Swift Programming Language
Hacking Parse.y with ujihisa
6. Generics. Collections. Streams
Go a crash course
Code Generation in PHP - PHPConf 2015
Swift Programming Language
Are we ready to Go?
Load-time Hacking using LD_PRELOAD
Implementing Software Machines in C and Go
8 arrays and pointers
PHP 8.1: Enums
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
All I know about rsc.io/c2go
A Functional Guide to Cat Herding with PHP Generators
An introduction to functional programming with go
Ad

Similar to Hello Swift 3/5 - Function (20)

PDF
Swift Study #2
PDF
Swift 5.1 Language Guide Notes.pdf
PDF
10. funtions and closures IN SWIFT PROGRAMMING
PDF
Workshop Swift
PDF
Functions
PDF
Swift Programming
PDF
Swift Introduction
PDF
Swift - the future of iOS app development
PDF
Function, Class
PPTX
Lecture 4: Functions
PDF
Swift Programming Language
PDF
Pointers & functions
PDF
InterConnect: Server Side Swift for Java Developers
PDF
An introduction to functional programming with Swift
PDF
Quick swift tour
PDF
Intro to JavaScript - Week 2: Function
PDF
Fun with functions
PPTX
F sharp _vs2010_beta2
PPTX
Java script functions
PPSX
Function in c
Swift Study #2
Swift 5.1 Language Guide Notes.pdf
10. funtions and closures IN SWIFT PROGRAMMING
Workshop Swift
Functions
Swift Programming
Swift Introduction
Swift - the future of iOS app development
Function, Class
Lecture 4: Functions
Swift Programming Language
Pointers & functions
InterConnect: Server Side Swift for Java Developers
An introduction to functional programming with Swift
Quick swift tour
Intro to JavaScript - Week 2: Function
Fun with functions
F sharp _vs2010_beta2
Java script functions
Function in c
Ad

More from Cody Yun (9)

PDF
Hello Swift Final
PDF
Hello Swift Final 5/5 - Structures and Classes
PDF
Hello Swift 4/5 : Closure and Enum
PDF
Hello Swift 2/5 - Basic2
PDF
Hello Swift 1/5 - Basic1
KEY
Unity3D Developer Network 4th
PDF
Unity3D Developer Network Study 3rd
KEY
Unity3D - 툴 사용법
PDF
Unity3D Developer Network Study Chapter.2
Hello Swift Final
Hello Swift Final 5/5 - Structures and Classes
Hello Swift 4/5 : Closure and Enum
Hello Swift 2/5 - Basic2
Hello Swift 1/5 - Basic1
Unity3D Developer Network 4th
Unity3D Developer Network Study 3rd
Unity3D - 툴 사용법
Unity3D Developer Network Study Chapter.2

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
medical staffing services at VALiNTRY
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
Essential Infomation Tech presentation.pptx
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Introduction to Artificial Intelligence
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
AI in Product Development-omnex systems
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Digital Strategies for Manufacturing Companies
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Odoo Companies in India – Driving Business Transformation.pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
medical staffing services at VALiNTRY
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Essential Infomation Tech presentation.pptx
2025 Textile ERP Trends: SAP, Odoo & Oracle
Introduction to Artificial Intelligence
Design an Analysis of Algorithms II-SECS-1021-03
Odoo POS Development Services by CandidRoot Solutions
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Design an Analysis of Algorithms I-SECS-1021-03
Which alternative to Crystal Reports is best for small or large businesses.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
AI in Product Development-omnex systems
CHAPTER 2 - PM Management and IT Context
Digital Strategies for Manufacturing Companies

Hello Swift 3/5 - Function

  • 1. Welcome to Swift 3/5 func sayHello(name name: String) { println("Hello (name)") } 1
  • 2. •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
  • 3. Function’s format func helloFunc(param:String)->String { let hello = "hello "+param return hello } 3
  • 4. Function’s format func helloFunc(param:String)->String { let hello = "hello "+param return hello } // declare function 4
  • 5. Function’s format func helloFunc(param:String)->String { let hello = "hello "+param return hello } // function name 5
  • 6. Function’s format func helloFunc(param:String)->String { let hello = "hello "+param return hello } // parameter’s name and parameter’s type 6
  • 7. Function’s format func helloFunc(param:String)->String { let hello = "hello "+param return hello } // function’s return type 7
  • 8. Function’s format func helloFunc(param:String)->String { let hello = "hello "+param return hello } //functions’s scope 8
  • 9. Function’s format func helloFunc(param:String)->String { let hello = "hello "+param return hello } //function’s logic 9
  • 10. Function’s format func helloFunc(param:String)->String { let hello = "hello "+param return hello } //return value in logic 10
  • 11. Using the function var returnValue = helloFunc("Cody") 11
  • 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) } 12
  • 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) } 13
  • 14. 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.random tupleResult.msg 14
  • 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? 15
  • 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 } // What is different? 16
  • 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 17
  • 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: ", ") 18
  • 19. 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 names join("hello","world",", ") Missing argument labels 'header:tail:joiner:' in call 19
  • 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? 20
  • 21. 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
  • 22. 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
  • 23. 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
  • 24. 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
  • 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) 25
  • 26. 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
  • 27. Function with in-out parameter func swapTwoInt(inout a: Int, inout b: Int) { let temp = a a = b b = temp } var numA = 1 var numB = 2 swapTwoInts(&numA,&numB) numA numB // In C++ called “call by reference” 27
  • 28. 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 = 1 var numB = 2 swapTwoInts(a:&numA,b:&numB) numA numB 28
  • 29. Function with in-out parameter func swapTwoInt(inout a: Int, inout b: Int) { let temp = a a = b b = temp } var numA = 1 var numB = 2 swapTwoInts(&numA,&numB) numA numB // In C++ called “call by reference” 29
  • 30. 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 = 1 var numB = 2 swapTwoInts(a:&numA,b:&numB) numA numB 30
  • 31. Function types func addTwoInts(a: Int, b: Int) -> Int { return a + b } var mathFunction: (Int, Int) -> Int = addTwoInts mathFunction(10,20) 31
  • 32. 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
  • 33. 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
  • 34. 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
  • 35. 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
  • 36. 내일은? • Enumeration • Closures • Structures and Classes - Initializers and Deinitialization in Classes - Properties - Subscripts - Inheritance - Subclassing - Overriding and Preventing Overrides 36
  • 37. 감사합니다. let writer = ["Cody":"Yun"] 37