DEV Community

ruthmoog
ruthmoog

Posted on

Methods vs. Functions: What's the difference?

I've been working through the book Learn Go With Tests and have been thinking about the difference between methods and functions.

Here's a quote from Learn Go With Tests...

A method is a function with a receiver. A method declaration binds an identifier, the method name, to a method, and associates the method with the receiver's base type.
Methods are very similar to functions but they are called by invoking them on an instance of a particular type. Where you can just call functions wherever you like, such as Area(rectangle) you can only call methods on "things".

  • Learn Go With Tests, by Chris James

example, string.length, where string is the identifier of a string instance of type String, and length is the method name associated to the type, String

In Go, functions and methods are declared in a similar way to each other.

A function is defined using the func keyword, followed by the function name and parentheses containing any input parameters.

// declare a function
func FunctionName(params Type) returnType {}
Enter fullscreen mode Exit fullscreen mode

For example, this Add function takes two input parameters, integers x and y, adds them together, and returns the result:

// For example ...
func Add(x, y int) int {
   return x + y
}
Enter fullscreen mode Exit fullscreen mode

What is a function?

A function is a code block that performs one task (but, it might have many many lines of code and do all sorts of actions under the responsibility of that task.) It can take input arguments or not, does something with them, and can return a value or not.

Functions are standalone; they can be called from anywhere in the code without affecting the rest of the program. As a result, functions can help modularise your code, making it easier maintain and understand.

What is a method?

A method is also defined using the func keyword, followed by the receiver type, and the method name. The receiver type is the type that the method is associated with, and it is specified by placing the type name in parentheses before the method name.

// declare a method
func (receiverName ReceiverType) MethodName(params) returnType {}
Enter fullscreen mode Exit fullscreen mode

For example, we could define a type, Car, which has three fields, Make, Model and FuelConsumption. We then define a method called GetCarbonFootprint that calculates the CO2 emitted for the fuel the car used.

type Car struct {
    Make            string
    Model           string
    FuelConsumption float64
}
func (c Car) GetCarbonFootprint() float64 {
    // Type Car emits 2.5kg of CO2 per litre
    return c.FuelConsumption * 2.5
}
Enter fullscreen mode Exit fullscreen mode

By convention in Go, the receiver variable is the first letter of the type. The method takes a Car, c, as its receiver, and returns a carbon footprint as a float64 type.

When your method is called on a variable of that type, you get your reference to its data via the receiverName variable. In many other programming languages this is done implicitly and you access the receiver via this.

To call a method we use the dot notation on a variable of the appropriate type, c.

func main {
  greenCar := Car{"VeryGreen", "E", 0.001}
  redCar := Car{"RedRover", "Classic", 2.9}
  greenCar.GetCarbonFootprint() // => 0.0025
  redCar.GetCarbonFootprint() // => 4.75
}
Enter fullscreen mode Exit fullscreen mode

Summary

Methods are functions that are associated with a particular type. They are different from non-method functions in that they are defined within the scope of a type, and can access and modify the fields of that type.
non-method functions can be called from anywhere in the code.

Top comments (0)