What are the differences between functions and methods in Swift?

FunctionMethodsSwift

Function Problem Overview


I always thought functions and methods were the same, until I was learning Swift through the "Swift Programming Language" eBook. I found out that I cannot use greet("John", "Tuesday") to call a function that I declared inside a class, as shown in the eBook in the screen shot below:

function declaration in swift

I received a error saying that "Missing argument label 'day:' in call" as per this screen shot:

Error message in swift

Here is the code:-

import Foundation
import UIKit

class ViewController2: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //var dailyStatement = greet("John", "Tuesday")
        var dailyStatement = greet("John", day: "Tuesday")
        println(dailyStatement)
    }

    func greet(name: String, day: String) -> String {
        return "Hello \(name), today is \(day)."
    }
}

After some research, I found this post: https://stackoverflow.com/questions/155609/what-is-the-difference-between-a-method-and-a-function, and it seems to me that the function that I declared inside a class is actually called a method. So, the syntax that I use to call the method is different compared to the syntax that I use to call a function.

I never realized this difference when I was programming in Objective-C.

  1. What are the differences between functions and methods in Swift?

  2. When do we use functions and when do we use methods in Swift?

Function Solutions


Solution 1 - Function

After a few hours of reading and experimenting, here are the things that I found out:-

Functions in Swift

> Functions are self-contained chunks of code that perform a specific > task. You give a function a name that identifies what it does, and > this name is used to “call” the function to perform its task when > needed.

Resource: Official Apple Documentation on Functions in Swift

Function Parameter Names

> However, these parameter names are only used within the body of the > function itself, and cannot be used when calling the function. These > kinds of parameter names are known as local parameter names, because > they are only available for use within the function’s body.

It means that by default, all the parameters for Function are local parameters.

But, sometimes we want to indicate the purpose of each parameter. So, we can actually define an external parameter name for each parameter. Example Code:

func someFunction(externalParameterName localParameterName: Int) {
    // function body goes here, and can use localParameterName
    // to refer to the argument value for that parameter
}

Another way to make the external parameter name is using hash symbol (#) to shorten the name.

func someFunction(#localParameterName: Int) {
    // function body goes here, and can use localParameterName
    // to refer to the argument value for that parameter
}

To call the above functions with external parameter, you may use

someFunction(localParameterName:10)

Methods in Swift

> Methods are functions that are associated with a particular type. > Classes, structures, and enumerations can all define instance methods, > which encapsulate specific tasks and functionality for working with an > instance of a given type.

Resource: Official Apple Documentation on Methods in Swift

> However, the default behavior of local names and external names is > different for functions and methods. > > Specifically, Swift gives the first parameter name in a method a local > parameter name by default, and gives the second and subsequent > parameter names both local and external parameter names by default.

Code below shows the differences for default and non-default parameters for method in Swift.

import Foundation
import UIKit

class ViewController2: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //Default methods calling
        var dailyStatement = greet("Rick", day: "Tuesday")
        println(dailyStatement)
        
        //First parameter is also an external parameter
        var dailyStatement2 = greet2(name:"John", day: "Sunday")
        println(dailyStatement2)
    }

    //Default: First Parameter is the local parameter, the rest are external parameters
    func greet (name: String, day: String) -> String {
        return "Hello \(name), today is \(day)."
    }
    
    //Use Hash symbol to make the First parameter as external parameter
    func greet2 (#name: String, day: String) -> String {
        return "Hello \(name), today is \(day)."
    }
}

I might miss some important details. Hope someone can provide a better answer.

Solution 2 - Function

As you said yourself, methods are functions, but in a class. In objective-c you never realized this, because we were only coding in classes. Every function that we wrote was a method of a class (ViewController or some other class we created).

In Swift we have the ability to create functions that are not inside some class. The main reason for doing this is to write functions that are not tied to any class, and can be used wherever we need them. So if you have a function that is related to a class you write it inside the class and you can access is from every instance of the class:

class Square {
   var length: Double
   func area() -> Double {
      return length * length
   }
}

But if you need to access the function from everywhere, then you don't write it inside a class. For example:

func squared(number: Int) -> Int {
    return number * number
}

About your syntax issues between functions and methods: You guessed it right, methods and functions are called a little bit differently. That is because in Objective-C we had long method names and we liked them because we could read what the methods were doing and what the parameters were for. So the first parameter in a method is in most cases described by the function name itself. And the other parameters shouldn't only be some numbers or strings or instances, they should be described as well, so Swift writes the name of the variable automatically. If you want to describe it by yourself you can do that as well:

class Something {
    func desc(firstString string1: String, secondString string2:String) {...}
}

Solution 3 - Function

Mainly the names are used interchangeably without people having a real intent of distinguishing them. But ultimately they do have a difference.

someFile.swift:

func someFunc{
//some code
}

class someClass{

    func someMethod{
    //some code    
    }

}

Note: someClass != someFile

someMethod works only on its associated type which is 'someClass'. However the same can't be said for someFunc. someFunc is only in the someClass.Swift because semantically it is better suited to be written in that file. It could have been written in any other class as long as it's marked with private

And obviously the method can access self. With functions, there is no self.. For more see: What's the difference between a method and a function?

Solution 4 - Function

Well, @Ricky's answer says it pretty much. I was confused what exactly they are. So here is my thought:

> Functions could be defined outside of classes or inside of classes/structs/enums, while Methods have to be defined inside of and part of classes/structs/enums. > > We could define a Function outside of any Type's definition and could use it within Methods of any Type's definition.

Just my understanding and illustration here, hope this helps someone else or you may edit if you feel there is an improvement needed OR let me know if anything is wrong:

//This is a Function which prints a greeting message based on the category defined in an 'enum'
func greet(yourName name: String, category: GreetingsCategory) {
    switch  category {
        case .Person:
            print("Hello, " + name + " Today is Tuesday")
        case .Vehicle:
            print("Hello, " + name + " your Vehicle is a Car")
    }
}

//This is an 'enum' for greetings categories
enum GreetingsCategory: String {
    case Person
    case Vehicle
}

//Type: Person
class Person {

    //This is a method which acts only on Person type
    func personGreeting() {
        greet(yourName: "Santosh", category: .Person)
    }
}

//Type: Vehicle
class Vehicle {

    //This is a method which acts only on Vehicle type
    func vehicleGreeting() {
        greet(yourName: "Santosh", category: .Vehicle)
    }
}

//Now making use of our Function defined above by calling methods of defferent types.
let aPerson = Person()
aPerson.personGreeting()
//prints : Hello, Santosh Today is Tuesday

let aVehicle = Vehicle()
aVehicle.vehicleGreeting()
//prints: Hello, Santosh your Vehicle is a Car

//We can also call the above function directly
greet(yourName: "Santosh", category: .Person)

Solution 5 - Function

Here is a simple answer on the difference between functions and methods:

> Some folks use “function” and “method” interchangeably, but there’s a > small difference: both of them are reusable chunks of code, but > methods belong to classes, structs, and enums, whereas functions do > not.

So:

func thisIsAFunction() {
}

struct Person {
    func thisIsAMethod() {
    }
}

> Because methods always belong to a data type, they have a concept of > self that functions do not.

source: https://www.hackingwithswift.com/example-code/language/whats-the-difference-between-a-function-and-a-method

Solution 6 - Function

functional principle as a part of functional language

function is a first-class type (first-class citizen) in Swift. Higher order functions

  • assign to a variable
  • pass as an argument
  • return

Function

Function is a block of code that is created for executing some task. Function consists of name, optional parameters(name, type), optional return type, body.

func name(parameterName1: Int, parameterName2: String) -> Bool {
    //statements
    return true
}

Function type - function’s parameter type and return type[Java about]

//Function type for the sample above
(Int, String) -> Bool

Method

Method - is a function which is associated with a type - class, structure, enum [About]:

Instance method - method which belongs to instance

MyClass().foo()

Type method - method which belongs to type itself. class or static is used[About]

MyClass.foo()

Closure

As official doc says that Closure in Swift has three next forms:

  • global function(with name, without capturing) - is a function that is declared in a global scope(out of class scope). Usually it is defined as a first level of .swift file and does not have a big memory food print
  • nested function(with name, with capturing enclosing function variables) - function inside other function
  • closure expression(without name, with capturing enclosing context)

So function is a named closure

or

Closure(closure expression) - anonymous function - is a block of code(functionality). Closure is a type of function without name. Closure is a function in terms of Functional programming. It can support capturing concept. It is similar to block in Objective-C.

[Closure vs Lambda]

They can be used for:

  • non-escaping closure - sync operations - click events, sort...
  • escaping closure - async operations - e.g.completion handler - it is a callback/notification which is called when task is done
//declaration
{ (<parameters>) -> <return type> in
        //body
}

let someClosure:() -> () = {
//closure between {}   
}

//call
someClosure()

[non-escaping vs escaping closure]
[@autoclosure]

Solution 7 - Function

Lots of great answers, but let me use Xcode to show something visually from the UIKit module:

enter image description here

That is a function because it's written at the global level. It's not a method. Methods are scoped to a class.

Screenshot to show that it's at the global level.

enter image description here

The following function is at the global level:

public func UIApplicationMain(_ argc: Int32, _ argv: UnsafeMutablePointer<UnsafeMutablePointer<Int8>>!,
 _ principalClassName: String?, _ delegateClassName: String?) -> Int32

Icons for the different symbols. (Class, Method, Property, Protocol, Function, Extensions are all different symbols)

  • The function has an icon like 𝓯

  • The method has an icon of M

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRickyView Question on Stackoverflow
Solution 1 - FunctionRickyView Answer on Stackoverflow
Solution 2 - FunctionBenView Answer on Stackoverflow
Solution 3 - FunctionmfaaniView Answer on Stackoverflow
Solution 4 - FunctionSantoshView Answer on Stackoverflow
Solution 5 - FunctionSilentKView Answer on Stackoverflow
Solution 6 - FunctionyoAlex5View Answer on Stackoverflow
Solution 7 - FunctionmfaaniView Answer on Stackoverflow