Breaking big chunks of code into small pieces, so that you read and follow the code easily
Reusability in other functions, therefore you can write clearer code
Ease of change
When used in multiple places and other big functions, you change the behavior of the function from a place with ease
Things to Consider
Not to add more than 6 parameters
Splitting up into smaller functions for sake of readability
Whether those parameters are related to each other
Throwing Data back
If you want to return your own value from a function, you need to do two things:
Write an arrow then a data type before your function’s opening brace, which tells Swift what kind of data will get sent back.
Use the return keyword to send back your data.
// Standart if-else conditional
func greet(name: String) -> String {
if name == "Taylor Swift" {
return "Oh wow!"
} else {
return "Hello, \(name)"
}
}
print(greet(name:"Sedat"))
print("-------------------------------------------------------------------")
// Ternary Conditional
func greet2(name: String) -> String {
name == "Taylor Swift" ? "Oh wow!" : "Hello, \(name)"
}
print(greet2(name:"Sedat"))
print("-------------------------------------------------------------------")
// without return, it does not output the result outside of the function
func greet3(name: String) {
if name == "Taylor Swift" {
"Oh wow!"
} else {
"Hello, \(name)"
}
}
print(greet3(name:"Sedat"))
Copying Functions
When you copy a function you do not use parenthesis ()
greetUser()
var greetCopy = greetUser
greetCopy()
When copying functions parameters will not come externally??? #learn
Removing Parameter Name
// With parameter name
func climbMountain0(name: String) {
print("I'm going to climb \(name).")
}
climbMountain0(name:"Everest")
/*
print:
I'm going to climb Everest.
*/
print("-------------------------------------------------------------------")
// Without parameter name
func climbMountain1(_ name: String) {
print("I'm going to climb \(name).")
}
climbMountain1("Everest")
/*
print:
I'm going to climb Everest.
*/
No difference in result at all.
Defining a Default Value for a Parameter
/*
In "end: Int = 3" 3 is the default value.
If nothing is defined for the "end" value so 3 is the value
*/
func printTimesTables(for number: Int, end: Int = 3) {
for i in 1...end {
print("\(i) x \(number) is \(i * number)")
}
}
//printTimesTables(for: 5, end: 20)
printTimesTables(for: 8)
/*
print:
1 x 8 is 8
2 x 8 is 16
3 x 8 is 24
*/
Checking Errors
If a Function may throw an error, you must add error handling both to prevent crashing the app and to clarify the cause.
But keep in mind that adding a throw to function may cause many other problems. So it is better not to add them at the beginning. By the time you become familiar with the app itself and the throw mechanism, you will ad them carefully.
enum PasswordError: Error {
case short, obvious
}
func checkPassword(_ password: String) throws -> String {
if password.count < 5 {
throw PasswordError.short
}
if password == "12345" {
throw PasswordError.obvious
}
if password.count < 8 {
return "OK"
} else if password.count < 10 {
return "Good"
} else {
return "Excellent"
}
}
let string = "12345"
do {
let result = try checkPassword(string)
print("Password rating: \(result)")
} catch PasswordError.short {
print("Please use a longer password.")
} catch PasswordError.obvious {
print("I have the same combination on my luggage!")
} catch {
print("There was an error: \(error.localizedDescription)")
}
/*
print:
I have the same combination on my luggage!
*/
Handling Function Failure with Optionals
If you only care about whether the function works or not, try may help. Otherwise do not use it.
If you want to know what those error are, and handle them uniquely, do not use "try?", but use do-catch-try block instead.
Methods
When Functions belong to Structs, they become Methods.
Sources
Mutating Function
// But why to change it? If we need to change it afterwards why we not define it as a var? #learn
Example 1: Resetting the the value set to its defaults
struct Canvas {
var backgroundColor: Color?
var foregroundColor: Color?
var shapes = [Shape]()
var images = [Image]()
mutating func reset() {
self = Canvas()
}
}
Sources
Videos
Articles / Documents
Functions as Parameters
func makeArray(size: Int, using generator: () -> Int) -> [Int] {
var numbers = [Int]()
for _ in 0..<size {
let newNumber = generator()
numbers.append(newNumber)
}
return numbers // output: [14, 17, 10, 11, 14]
}
let rolls = makeArray(size: 5) { // output: [14, 17, 10, 11, 14]
Int.random(in: 1...20)
}
print(rolls)
// print: [14, 17, 10, 11, 14]
func doImportantWork(first: () -> Void, second: () -> Void, third: () -> Void) {
print("About to start first work") // output: About to start first work
first()
print("About to start second work") // output: About to start second work
second()
print("About to start third work") // output: About to start third work
third()
print("Done!") // output: Done!
}
doImportantWork {
print("This is the first work")
} second: {
print("This is the second work")
} third: {
print("This is the third work")
}
/*
print:
About to start first work
This is the first work
About to start second work
This is the second work
About to start third work
This is the third work
Done!
*/
var payCash = {
print("Here's the money.")
}
func buyClothes(item: String, using payment: () -> Void) {
print("I'll take this \(item).")
payment()
}
buyClothes(item: "jacket", using: payCash)
/*
print:
I'll take this jacket.
Here's the money.
*/
Sample Codes
func square(numbers: [Int]) {
for number in numbers {
let squared = number * number
print("\(number) squared is \(squared).")
}
}
square(numbers: [2, 3, 4])
/*
print:
2 squared is 4.
3 squared is 9.
4 squared is 16.
*/