”;
In Swift, functions and methods both are fundamental building blocks for optimizing and encapsulating code. It might be confusing to understand the difference between function and method, but we might think they are both the same. That”s not true. The methods are used in class, struct, and enum.
They are called on the instances of that type and access and modify the properties of the instance. Whereas functions are defined independently or globally without creating a class or structure. Or we can say that a method is a function while a function is not a method.
Swift Functions
Functions are used to perform specific actions and are called by their names. It is very simple to pass the variables or constants as an argument while calling a function. Also, we can return a value from a function of any type. A function that works independently of a file can also be defined outside of it.
Syntax
Following is the syntax of a function −
func functionName(parameters...) -> returnType { //Write statements here to perform actions }
Example
Swift program to demonstrate how to create and call a function.
// Function without return value func show(msg: String) { print("Welcome to, (msg)!") } // Function call show(msg: "Tutorials Point") // Function with return value func sumOfNumbers(_ nums: [Int]) -> Int { var sum = 0 // Calculating the sum of the numbers present in the array for n in nums { sum += n } return sum } // Function call with return value let inputNumbers = sumOfNumbers([3, 5, 6, 7, 1]) print("Sum = (inputNumbers)")
Output
It will produce the following output −
Welcome to, Tutorials Point! Sum = 22
Swift Methods
A method is a function and can be defined in classes, structures, and enumerations. Using methods, we can perform certain actions according to your requirements. As per our needs, we can define more than one method to perform different tasks.
To access the defined methods in a class or structure we have to create an instance of the associated type. Using that object, we can call methods to perform tasks. Just like functions, we can also pass arguments while calling the method. Always remember that without creating an object of that class or struct we cannot access the methods.
In the Swift language, there are two types of methods like the following −
-
Instance Methods − As per their names, these are the instance-specific methods that can be called through the instances. The compiler will give us an error if we will try to access the methods without an instance of that class or struct. Also, we cannot call the methods of a class (for eg. Class Student) from another class”s instance.
-
Type Methods − In Swift, we can define the type methods with prefix keywords like static or class. There is no need to create an instance of a class or struct to call type methods. They can be called directly from the type name.
Syntax
Following is the syntax of a method −
func methodName(parameters...) -> returnType { //Write statements here to perform actions }
Example
Swift program to demonstrate how to create and call a method.
// Defining a structure struct Student { // Properties let name: String let grade: Int // Instance method func displayInfo() { print("name: (name)") print("grade: (grade)") } // Type method static func dummyStudents() -> [Student] { return [Student(name: "Ray Sin", grade: 4), Student(name: "Cherry Blossom", grade: 2), Student(name: "Perry Scope", grade: 7)] } } // Creating instance of the structure let anne = Student(name: "Anne Teak", grade: 5) // Calling instance method from object anne anne.displayInfo() // Calling type method let students = Student.dummyStudents() print("number of students: ", students.count)
Output
It will produce the following output −
name: Anne Teak grade: 5 number of students: 3
Visual Identification Between Function and Method
The Swift language shows how a function and method are denoted. It is visible that both are denoted as follows −
-
The function has an icon like ƒ.
-
The method has an icon of M.
Difference Between Methods and Functions in Swift
The following table will show the major difference between the methods and functions in Swift −
Function | Method |
---|---|
The function is defined as independent. So that, we can define functions outside of the class. | They are connected with a class or struct itself. Outside the scope, you can not define the methods. |
Functions are independent properties of structured languages like C/C++. | Methods works within the scope of an object similar to Object-oriented languages like C#, Java, Swift, etc. |
Functions don”t capture any reference variables or constants. | Methods are called using reference variables (or instances) only. |
While functions do not belong to classes, they perform individually. | Methods are defined to manipulate the particular instance of that class or struct. |
No need to create objects to call them. | An object is required to access them. |
Simply, every function is not a method. | While every method is a function. |
”;