Skip to content

Functions Labs #490

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
89 changes: 68 additions & 21 deletions MyPlayground.playground/Pages/main.xcplaygroundpage/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
*/
// write your code here

func frozen ()
{
print("Let it go!")
}


frozen()



Expand All @@ -27,7 +31,13 @@
*/
// write your code here

func frozenAgain ()
{
let message = "Let it go"
print(message)
}

frozenAgain()



Expand All @@ -38,8 +48,11 @@
*/
// write your code here



func charName (name: String)
{
print("My favorite character is \(name)")
}
charName(name: "daffy")



Expand All @@ -49,6 +62,14 @@
*/
// write your code here

let cName = "Mickey"
charName(name: cName)

var cNameVar = "Mini"
charName(name: cNameVar)

cNameVar = "Superman"
charName(name: cNameVar)



Expand All @@ -60,8 +81,12 @@
*/
// write your code here

func problems (numProblems: Int)
{
print ("I got \(numProblems) problems but Swift ain't one")
}


problems(numProblems: 3)



Expand All @@ -71,20 +96,23 @@
### 6. Write a function that takes two arguments, the name of a band (a `String`) and a number (an `Int`). It should print the message "My #<number> favorite band is <band>." to the console.
*/
// write your code here
func favBand (number: Int, isBandName: String)
{
print("My #\(number) favorite band is \(isBandName)")
}



favBand(number: 2, isBandName: "Envogue")



/*: question7
### 7. The code below is broken. Can you identify which line has an error and fix it so that it works again? Uncomment the code below before starting.
*/
//func badFavoriteBand(bandName: String, position: Int) {
// print("My #\(position) favorite band is \(bandName).")
//}
//
//badFavoriteBand("The Beatles", 2)
func badFavoriteBand(bandName: String, position: Int) {
print("My #\(position) favorite band is \(bandName).")
}

badFavoriteBand(bandName: "The Beatles", position: 2)



Expand All @@ -95,11 +123,11 @@
/*: question8
### 8. This code is broken, too. Assume the call to the function is correct. What's broken about the function definition? Can you fix it? Uncomment the code below before starting.
*/
//func alsoBadFavoriteBand(bandName: String, position: String) {
// print("My #\(position) favorite band is \(bandName)")
//}
//
//alsoBadFavoriteBand(bandName: "Blink-182", position: 42)
func alsoBadFavoriteBand(bandName: String, position: Int) {
print("My #\(position) favorite band is \(bandName)")
}

alsoBadFavoriteBand(bandName: "Blink-182", position: 42)



Expand All @@ -111,9 +139,12 @@
### 9. Let's play Mad Libs! Create a function called `madLib`. It should take three parameters: A character name, a noun, and a preposition, and print out the line "To <noun> and <preposition>, <character name>!" to the console. Don't forget to call your function to test it out!
*/
// write your code here
func madLibs (crName: String, noun: String, preposition: String)
{
print("To \(noun) and \(preposition), \(charName)")
}



madLibs (crName: "Scooby", noun: "cow", preposition: "upon")



Expand All @@ -123,7 +154,11 @@
*/
// write your code here


func buzz () -> String
{
let lightYear = "Buzz LightYear to the rescue!"
return lightYear
}



Expand All @@ -134,16 +169,28 @@
*/
// write your code here

func retNumber () -> Int
{
let num: Int = 43

return num

}


retNumber ()



/*: question12
### 12. Create a function that takes in a characters name. This function will return back a `String` as follows: "To infinity and beyond, <character name>!". The character name should be returned uppercased.
*/
// write your code here

func charName2 (name: String) -> String
{
name.uppercased()
let newString = "To infinity and beyone, \(name)!"
return newString
}



Expand Down