Swift – Difference Between Function and Method

06 Dec, 2021
Some folks use function and method interchangeably and they think function and method are the same in swift. But, function and method both are different things and both have their advantages. In the code, Both function and method can be used again but methods are one of these: classes, structs, and enums part, whereas functions do not belong to any classes, structs, and enums. In short, a method is a function that is associated with a type, that is, a class, a struct, or an enum. We can also think in this: Every method is also a function, but not every function is also a method. Swift uses the “func” keyword for both methods and functions. This is a bit complicated but we will see the clear difference between function and method in this post.
Functions: Functions are chunks of code that do a specific task. There is always a name associated with a function and to call the function we use this name to do some work. Or we can say that a function is a piece of code that is called by name and does a specific task. We can pass data to function as an argument and the function can also return data that is the return value of the function. A particular type should not be associated with the functions. Files that are outside of a particular type can also get this function if it is a global function. We pass all data to function is explicitly passed. 
Syntax:
func function_name(parameters) -> returnType{
    // Body of the function
}
Example:
Swift
// Swift program to illustrate function  // Functionfunc gfg() {    print("Hello GeeksforGeeks")}    // Calling functiongfg()
Output:
Hello GeeksforGeeks
Methods: Method is the function that is associated with a particular type. The instance method can be defined by structures, enumerations, and classes. It encapsulates particular work and functionality for running with an instance of a given particular type. We call the method from the name which is associated with its object. we can determine the work required to gradually improve essential components or the whole system code using the swift method. It is also used to examine the big and complicated legacy system. Below are the two types of methods: 
Instance methods
Type Methods
Swift supports Instance methods and Type Methods. Instance methods can only be invoked by an instance of the type. If we try to access the instance method with different objects, then the compiler will give an error. A type method is prefixed with the static or class keyword in swift. A type method is also the type itself.
However, the behavior of local names and external names in swift is not the same for methods and functions. In a method, the first parameter name is given by swift by default. And by default, gives the second and following next parameters are used for both local and external parameter names. 
Syntax:
func method_name(parameters){
    // Body of the method
}
Example:
In the below code, we create two methods. Here, learn() is the example of the instance method and write() is the example of the type method. In the type method, we use the static or class keyword as a prefix. Here we use the static prefix for the write() method. And, you can see we call it using struct name. We call the instance method using the instance of struct Geek(). 
Swift
// Swift program to illustrate method  // Geeks structurestruct Geek{    let speed: Int      // Instance method    func learn()     {        print("learning at \(speed) MPH")    }          // Type method    static func write()    {        print("write in a swift")    }  }  // Driver code  // Create Carlet x = Geek(speed: 20)  // Instance Methodx.learn()  // Type MethodGeek.write()
Output:
learning at 20 MPH
write in a swift
Function
Method
Picked
Difference Between
Swift
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Difference between Compile-time and Run-time Polymorphism in Java
Similarities and Difference between Java and C++
Difference between Internal and External fragmentation
Swift - Convert String to Int Swift
Swift - Difference Between Sets and Arrays
Swift - Hello World Program
Swift - Properties and its Different Types
Swift - Iterate Arrays and Dictionaries