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 conditionalfuncgreet(name: String) ->String {if name =="Taylor Swift" {return"Oh wow!" } else {return"Hello, \(name)" }}print(greet(name:"Sedat"))print("-------------------------------------------------------------------")// Ternary Conditionalfuncgreet2(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 functionfuncgreet3(name: String) {if name =="Taylor Swift" {"Oh wow!" } else {"Hello, \(name)" }}print(greet3(name:"Sedat"))
When copying functions parameters will not come externally??? #learn
Removing Parameter Name
// With parameter namefuncclimbMountain0(name: String) {print("I'm going to climb \(name).")}climbMountain0(name:"Everest")/*print:I'm going to climb Everest.*/print("-------------------------------------------------------------------")// Without parameter namefuncclimbMountain1(_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*/funcprintTimesTables(fornumber: Int, end: Int=3) {for i in1...end {print("\(i) x \(number) is \(i * number)") }}//printTimesTables(for: 5, end: 20)printTimesTables(for:8)/*print:1 x 8 is 82 x 8 is 163 x 8 is 24*/
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.
enumPasswordError:Error{case short, obvious}funccheckPassword(_password: String) throws->String {if password.count<5 {throw PasswordError.short }if password =="12345" {throw PasswordError.obvious }if password.count<8 {return"OK" } elseif password.count<10 {return"Good" } else {return"Excellent" }}let string ="12345"do {let result =trycheckPassword(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!*/
funcdoImportantWork(first: () ->Void, second: () ->Void, third: () ->Void) {print("About to start first work")// output: About to start first workfirst()print("About to start second work")// output: About to start second worksecond()print("About to start third work")// output: About to start third workthird()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 workThis is the first workAbout to start second workThis is the second workAbout to start third workThis is the third workDone!*/
var payCash = {print("Here's the money.")}funcbuyClothes(item: String, usingpayment: () ->Void) {print("I'll take this \(item).")payment()}buyClothes(item:"jacket", using: payCash)/*print:I'll take this jacket.Here's the money.*/
funcsquare(numbers: [Int]) {for number in numbers {let squared = number * numberprint("\(number) squared is \(squared).") }}square(numbers: [2, 3, 4])/*print:2 squared is 4.3 squared is 9.4 squared is 16.*/