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 Swift Enums and Structs Enums and their Methods Initializer

Martina Vonkomerova
Martina Vonkomerova
7,790 Points

Method Vs Function

Can somebody tell me the different between a method and a function in Swift ?

3 Answers

Brandon Mowat
Brandon Mowat
8,186 Points

A method is a special type of function that is tied to an object. A regular old function is global and can be called wherever.

Methods are a subset of functions.

Gleb Linnik
Gleb Linnik
Courses Plus Student 2,558 Points

Hmmmm. I thought that methods are functions in Enum, Struct and Class

Brandon Mowat
Brandon Mowat
8,186 Points

Those are all examples of objects.

Daniel Botta
Daniel Botta
17,956 Points

Yes they are, but they make for a more specific answer in the Swift language.

Daniel Botta
Daniel Botta
17,956 Points

In Swift...

A function is created or called in the global scope (not within anything).

A method is a function that is found within an class, enum, or struct.

Essentially they are the same thing, but think of a method as a function that is stored within something.

Daniel Botta
Daniel Botta
17,956 Points

Yes, but I was trying to be more specific for Martina. Your answer was not very specific. Her questions was in Swift.... Most all programming languages use "objects", but Swift specifically uses Classes, Enums, and Structs.

Brandon Mowat
Brandon Mowat
8,186 Points

Those are all examples of objects though. Swift is an "Object oriented" language, meaning it makes use of the object data type (class, struct ect.). Python and javascript also both use classes (which are objects). C, for example is not object oriented because it has no object data structure.

Brandon Mowat
Brandon Mowat
8,186 Points

I see your point when you say it's " a more specific answer in the Swift language" but it doesn't just apply to the Swift language. It's a universal concept and applies to all OO languages.

Kyaw Kyaw Soe
Kyaw Kyaw Soe
7,528 Points

Function and Method are same

We call method when it's in the Enum