Open In App

Swift – Difference Between Function and Method

Last Updated : 06 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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
  
// Function
func gfg() 
{
    print("Hello GeeksforGeeks")
}
  
  
// Calling function
gfg()


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 structure
struct 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 Car
let x = Geek(speed: 20)
  
// Instance Method
x.learn()
  
// Type Method
Geek.write()


Output:

learning at 20 MPH
write in a swift

Difference Between Function and Method

Function

Method

Functions have an independent existence. It can be defined outside of the class. Methods do not have an independent existence. It is always defined within a class, struct, or enum.
Functions do not have any reference variables.  Methods are called using reference variables.
Functions are called independently.  Methods are called using instances or objects of the class.
Functions are the properties of structured languages. Methods are the properties of Object-oriented language.
It is a self-describing piece of code.  It is used to manipulate the instance variable of a class.
A particular type should not be associated with the functions. A method is associated with a particular class, struct, or enum.
Every Function is not a method. Every method is a function.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads