Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master #20

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updated to Swift 3
Some changed made
  • Loading branch information
Christopher Bell authored and Christopher Bell committed Sep 27, 2016
commit 42ab4ea06900f9159f83ca8fc21c982bd08c015b
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion 6. Functions.playground/contents.xcplayground
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
<code source-file-name='section-1.swift'/>
</sections>
<timeline fileName='timeline.xctimeline'/>
</playground>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
51 changes: 27 additions & 24 deletions 6. Functions.playground/section-1.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ------------------------------------------------------------------------------------------------
// Checked and updated September 2016
// Things to know:
//
// * Like most other languages, functions contain units of code to perform a task, but there are
Expand All @@ -25,7 +26,7 @@ func sayHello(personName: String) -> String
}

// If we call the function, we'll receive the greeting
sayHello("Peter Parker")
sayHello(personName: "Peter Parker")

// Multiple input parameters are separated by a comma
func halfOpenRangeLength(start: Int, end: Int) -> Int
Expand Down Expand Up @@ -87,7 +88,7 @@ addSeventeen(toNumber: 42)
//
// The following declaration creates an internal parameter named "action" as well as an external
// parameter named "action":
func kangaroosCan(#action: String) -> String
func kangaroosCan(action: String) -> String
{
return "A Kangaroo can \(action)"
}
Expand All @@ -108,7 +109,7 @@ func addMul(firstAdder: Int, secondAdder: Int, multiplier: Int = 1) -> Int
}

// We can call with just two parameters to add them together
addMul(1, 2)
addMul(firstAdder: 1, secondAdder: 2)

// Default parameter values and external names
//
Expand All @@ -119,7 +120,7 @@ addMul(1, 2)
//
// Therefore, when calling the function and specifying a value for the defaulted parameter, we
// must provide the default parameter's external name:
addMul(1, 2, multiplier: 9)
addMul(firstAdder: 1, secondAdder: 2, multiplier: 9)

// We can opt out of the automatic external name for default parameter values by specify an
// external name of "_" like so:
Expand All @@ -129,10 +130,10 @@ func anotherAddMul(firstAdder: Int, secondAdder: Int, _ multiplier: Int = 1) ->
}

// Here, we call without the third parameter as before:
anotherAddMul(1, 2)
anotherAddMul(firstAdder: 1, secondAdder: 2)

// And now we can call with an un-named third parameter:
anotherAddMul(1, 2, 9)
anotherAddMul(firstAdder: 1, secondAdder: 2, 9)

// ------------------------------------------------------------------------------------------------
// Variadic Parameters
Expand All @@ -158,12 +159,12 @@ func arithmeticMean(numbers: Double...) -> Double
// Let's call it with a few parameter lengths. Note that we can call with no parameters, since that
// meets the criteria of a variadic parameter (zero or more).
arithmeticMean()
arithmeticMean(1)
arithmeticMean(1, 2)
arithmeticMean(1, 2, 3)
arithmeticMean(1, 2, 3, 4)
arithmeticMean(1, 2, 3, 4, 5)
arithmeticMean(1, 2, 3, 4, 5, 6)
arithmeticMean(numbers: 1)
arithmeticMean(numbers: 1, 2)
arithmeticMean(numbers: 1, 2, 3)
arithmeticMean(numbers: 1, 2, 3, 4)
arithmeticMean(numbers: 1, 2, 3, 4, 5)
arithmeticMean(numbers: 1, 2, 3, 4, 5, 6)

// If we want to use variadic parameters and default parameter values, we can do so by making sure
// that the default parameters come before the variadic, at the end of the parameter list:
Expand All @@ -185,11 +186,11 @@ anotherArithmeticMean()
// default parameter values. In this case, it helps us recognize where the defalt parameters leave
// off and the variadic parameters begin:
anotherArithmeticMean(initialTotal: 1)
anotherArithmeticMean(initialTotal: 1, 2)
anotherArithmeticMean(initialTotal: 1, 2, 3)
anotherArithmeticMean(initialTotal: 1, 2, 3, 4)
anotherArithmeticMean(initialTotal: 1, 2, 3, 4, 5)
anotherArithmeticMean(initialTotal: 1, 2, 3, 4, 5, 6)
anotherArithmeticMean(initialTotal: 1, numbers: 2)
anotherArithmeticMean(initialTotal: 1, numbers: 2, 3)
anotherArithmeticMean(initialTotal: 1, numbers: 2, 3, 4)
anotherArithmeticMean(initialTotal: 1, numbers: 2, 3, 4, 5)
anotherArithmeticMean(initialTotal: 1, numbers: 2, 3, 4, 5, 6)

// Variadic parameters with external parameter names only apply their external name to the first
// variadic parameter specified in the function call (if present.)
Expand Down Expand Up @@ -217,14 +218,16 @@ yetAnotherArithmeticMean(initialTotal: 1, values: 2, 3, 4, 5, 6)
// Constant and variable parameters
//
// All function parameters are constant by default. To make them variable, add the var introducer:
func padString(var str: String, pad: Character, count: Int) -> String
func padString(str: String, pad: Character, count: Int) -> String
{
str = Array(count: count, repeatedValue: pad) + str
var str = str
str = String(repeating: String(pad), count: count) + str

return str
}

var paddedString = "padded with dots"
padString(paddedString, ".", 10)
padString(str: paddedString, pad: ".", count: 10)

// Note that the function does not modify the caller's copy of the string that was passed in
// because the value is still passed by value:
Expand All @@ -239,7 +242,7 @@ paddedString
// Note that inout parameters cannot be variadic or have default parameter values.
//
// We'll write a standard swap function to exercise this:
func swap(inout a: Int, inout b: Int)
func swap(a: inout Int, b: inout Int)
{
let tmp = a
a = b
Expand Down Expand Up @@ -298,10 +301,10 @@ doMul(4, 5)
//
// This additional syntactic decoration has a purpose, but it doesn't affect the underlying
// function type, which remains: (Int, Int) -> Int
let doAddMul: (a: Int, b: Int, Int) -> Int = addMul
let doAddMul: (_ a: Int, _ b: Int, Int) -> Int = addMul

// Calling the function now requires external names for the first two parameters
doAddMul(a: 4, b: 2, 55)
doAddMul(4, 2, 55)

// We can pass function types as parameters to funcions, too.
//
Expand All @@ -314,7 +317,7 @@ func doDoMul(doMulFunc: (Int, Int) -> Int, a: Int, b: Int) -> Int

// We can now pass the function (along with a couple parameters to call it with) to another
// function:
doDoMul(doMul, 5, 5)
doDoMul(doMulFunc: doMul, a: 5, b: 5)

// We can also return function types.
//
Expand Down
4 changes: 3 additions & 1 deletion 6. Functions.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=13141&amp;EndingColumnNumber=5&amp;EndingLineNumber=4&amp;StartingColumnNumber=4&amp;StartingLineNumber=3&amp;Timestamp=424365464.279531">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=13442&amp;EndingColumnNumber=5&amp;EndingLineNumber=5&amp;StartingColumnNumber=4&amp;StartingLineNumber=4&amp;Timestamp=496706989.211164"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>