83
Difference between function and method in Swift
Some people think functions and methods are same and use the terms interchangeably. But, these two are slightly different. “Methods belong to classes, structs, and enums, whereas functions do not.”
Example:
Let’s see some differences between a function and a method:
Function | Method |
---|---|
Functions have independent existence. You can define them outside of the class. | Methods do not have independent existence. They are always defined within a class, struct, or enum. |
Functions are the properties of structured languages like C, C++, Pascal and object based language like JavaScript. Note: There is no concept of function in Java. | Methods are the properties of Object-oriented language like C#, Java, Swift etc. |
Functions don’t have any reference variables. | Methods are called using reference variables. |
Functions are a self describing piece of code. | Methods are used to manipulate instance variable of a class. |
Functions are called independently. | Methods are called using instance or object. |
Next TopicTimers in Swift