Programming in Swift: Functions & Types

Jan 4 2022 · Swift 5.5, iOS 15, Xcode 13

Part 1: Functions

03. Challenge: Functions

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 02. Review Functions Next episode: 04. Overloading

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 03. Challenge: Functions

Update Notes: This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.

Transcript: 03. Challenge: Functions

How about a quick challenge to test everything we reviewed in the last episode? You’ll find one in the next page of the Playground for this part of the course. Pause the video, give it a try, and then come back to see what I did with it. Good luck!

This challenge was a bit open ended, so I wrote two functions that met the criteria. First, a twitter handle generator!

func generateTwitterHandle(

It takes two Strings, and name and another word

(name: String, anotherWord word: String

…and the second parameter has a default value of “Meow”, in case you can’t decide on a second word to use.

String = "Meow"

This function returns a String

 -> String {
 
 }

which is a combination of the two parameters.

name.lowercased() + word

I lowercased the first one, so if someone uses the default value, they’ll end up with a twitter handle styled like mine and Jessy’s, even if they capitalized their name!

generateTwitterHandle("Ozma")

My second solution is a german compound word maker called deutschify!

func deutschify(

It also takes two Strings.

func deutschify(_ word1: String, _ word2: String = "katzen")

Neither of them have an argument label, because honestly the output will be nonsensical no matter what someone passes in to this function. But one of them does have a default value of “katzen”, the german word for “cats”. Aaaand then it returns a String.

func deutschify(_ word1: String, _ word2: String = "katzen") -> String {

}

To generate some completely grammatically incorrect german compound words, I’ll create two arrays of german words, and pick a random element from each one.

...
  let adjective = ["Frölich", "Rund", "Salzig", "Schwarze"].randomElement()!
  let ending = ["schule", "keit", "maler", "maschine"].randomElement()!
}

Then, to satisfy the last requirement, I’ll add those to the two parameters and return the result.

...  
  return adjective + word1 + word2 + ending
}

Now I can some terrible unpronounceable fake words like…

deutschify("swifty")

this one that I think is a painter of round, swifty cats. And if I get fancy and provide two arguments I can get something like…

deutschify("arctic", "tree")

this “word” that describes the happiness of arctic trees. And there you go!

I hope that went well for you, and I’m curious what you came up with. Meet me in the next episode to learn more about functions!