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 trialMartina Vonkomerova
7,790 PointsMethod Vs Function
Can somebody tell me the different between a method and a function in Swift ?
3 Answers
Brandon Mowat
8,186 PointsA 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.
Daniel Botta
17,956 PointsIn 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.
Brandon Mowat
8,186 PointsYep. That's what I said.
Daniel Botta
17,956 PointsYes, 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
8,186 PointsThose 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
8,186 PointsI 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
7,528 PointsFunction and Method are same
We call method when it's in the Enum
Gleb Linnik
Courses Plus Student 2,558 PointsGleb Linnik
Courses Plus Student 2,558 PointsHmmmm. I thought that methods are functions in
Enum
,Struct
andClass
Brandon Mowat
8,186 PointsBrandon Mowat
8,186 PointsThose are all examples of objects.
Daniel Botta
17,956 PointsDaniel Botta
17,956 PointsYes they are, but they make for a more specific answer in the Swift language.