top of page

My First Swift Function


- What is a function? Block of code that you use again and again.

- What is input parameters?

- What is an argument?

Let's create a function:

func printAHaiku() //Function name be descriptive!!

{ print(" Input and output,") print(" This function needs neither one") print(" To print a haiku") }

- Let's use for loop and run it 3 times

- func invite(guest: String) // ALWAYS use declare data type in function. NO INFERENCE!

- guest = argument label

- Let's write an invitation function:

----

Dear [],

I would love for you to come to my party this Saturday at 6PM.

Love,

Banana

----

- Now let's invite all friends:

func invite(guest: String) {

print("Dear \(guest),")

print("I would love for you to come to my party.")

print("Love, \nBanana")

print("\n \n")

}

func invite(allGuests: [String]) {

for guest in allGuests {

invite(guest:guest)

}

}

var friends = ["Natalie", "Megan", "Stehanie", "Jolene", "Bao", "Ashton", "Nhi"]

invite(guest: "Megan")

invite(allGuests: friends)

---

Homework:

Imagine your party is coming FAST and you need to send a message to your friends.

If rsvp, say,

Dear [name],

I am excited to see you!

Love, Banana

if not rsvp, say

Dear [name]

Please reply by tomorrow!

Love, Banana

Requirements:

- Create a function named sendMessage that takes 2 parameters:

1. Name (use String type)

2. A boolean value

- The rest is up to you.

- Be creative and have fun!


Featured Posts
Recent Posts
Archive
Search By Tags
No tags yet.
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page