Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Practising Swift Functions

HI I wanted to practise some of the things that Ive learnt so far by creating a list of my favourite actors and actresses alongside their films and then pass a rating for each individual film in playground.

Ive tried creating something several times but it gets really confusing I started by creating a a dictionary containing the name of the actor and then then all their films.

e.g

var film = [
    "Actor": ["Film1", "Film2" ],
    "Actor 2": ["Film1" , "Film2", "Film3", "Film4" ]


]

The trouble Im having is not knowing what to use, i don't know how to implement what Ive learnt in to creating what I want. for example I want to be able to write the name of a film , with a rating for that film as well as a small list of the cast of that film. But I'm to sure how to use functions to help me to do that.

How do programmers know what methods and things to use when writing code.

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi, Alia! In all honesty, I think the best way to learn this is just to practice. I think your example might be done more easily as an array of objects. Swift is far from being the language I'm most proficient in so I'm going to tag someone who will likely be able to answer :smiley:

Michael Hulet ... care to chime in? :sparkles:

In any language, functions (or methods) are meant to do something specific. Each function should do only one specific thing. So, in your film example, you might have the following functions:

func addActor(actor: String)
func addFilmOfActor(film: String, actor: String)
func addRatingOfFilmOfActor(rating: Double, film: String, actor: String)
func addCastOfFilmOfActor(cast:[String], film: String, actor: String)

... etc ... and implement each function accordingly.

Then in your main program, you can simply call these single-minded functions to do all the those things you want to do.

Functions also help to eliminate duplication of code, because they encapsulate code functionality which you can reuse as often as required, rather than writing out the same lines of code again and again which do the same thing.

So, a good place to start when deciding when to create functions is to identify specific coding tasks which might be used in multiple places in your code, and those would be good candidates to encapsulate into functions.

You should totally read David Lin's brilliant answer, as it gives a very good explanation of what a function is. Here's the gist of it, though: Functions do things using data.

In your idea, there seems like there's a lot of data, but not many things to do. in fact, the only thing you're doing here is printing a string in a specific format. Thus, you'd only really need one function: a function that takes a Dictionary (or maybe a custom struct might be better?) and formats a String with the data in that Dictionary (or struct) and then prints it.

When I'm programming and I know I'm gonna have to do something a lot, that's when I write a function