Methods vs. Functions: What's the difference?
Tags: todayilearned, go
Published: 2023-04-20 14:44:28

---
title: Methods vs. Functions: What's the difference?
published: true
description: Explainer of how methods and functions differ from each other
tags: todayilearned, go
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2023-04-11 10:13 +0000
---

I've been working through the book _[Learn Go With Tests](https://quii.gitbook.io/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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11t9puzvywzczebqtvya.png)

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.

```Go
// declare a function
func FunctionName(params Type) returnType {}
```

For example, this `Add` function takes two input parameters, integers x and y, adds them together, and returns the result:
```Go
// For example ...
func Add(x, y int) int {
   return x + y
}
```

## 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.

```Go
// declare a method
func (receiverName ReceiverType) MethodName(params) returnType {}
```

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.

```go
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
}
```

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`. 

```go
func main {
  greenCar := Car{"VeryGreen", "E", 0.001}
  redCar := Car{"RedRover", "Classic", 2.9}
  greenCar.GetCarbonFootprint() // => 0.0025
  redCar.GetCarbonFootprint() // => 4.75
}
```
## 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.