Something I see routinely in tech writing is confusion between the terms method and function. And that confusion is understandable: the difference is subtle, some programming languages favor one or the other, and everyone uses the word function generically in the sense of capability (“my flip phone has a calculator function”). I’m going to try to illustrate the difference in a way that will probably help you choose the right word most of the time. I realize there are other probably more technically robust explanations out there, but this is how I keep them straight myself.
Consider this little add()
function:
function add(a, b) {
return a + b;
}
It takes two arguments, a
and b
, and returns their sum. Call add(1, 2)
and get 3
.
Some functions don’t take input from the caller. Suppose we call some function getCurrentTime()
that returns a string containing the computer’s time. That’s still a function (though we’re starting to depart from the word function’s mathematical origins).
Likewise, some functions do things that change the state of the program or system. They are said to have side effects but may not return anything. Suppose we call some function deleteFile('/path/to/my/file.txt')
, which deletes a file from the system but returns nothing (or the programming language’s equivalent, such as an explicit null value). That’s a function too.
All methods are functions (though not all functions are methods). You typically hear the term method in the context of object-oriented programming. A method is a function with a twist: it is attached (also known as “bound”) to some other data or behavior. Let’s look at an example:
Number = {
value = 3,
function add(secondNumber) {
sum = value + secondNumber;
value = sum;
}
}
This Number
object has a property, value
. We can access it like this:
Number.value
→ 3
Number has a surprising behavior, however: we can’t use the +
(plus) operator to add its value to another number. Instead, we have to use the add
method, which is part of Number
. The add
method takes a number as its argument and it uses that number to change Number
’s value
property, like this:
Number.value
→ 3
Number.add(1)
→ undefined (no output)
Number.value
→ 4
A method doesn’t have to change the state of the object it’s attached to like this, but it’s common. Like any other function, methods can have a range of behavior: they can accept arguments (or not), they can have side effects (or not), and they can return values (or not).
Look at the add
method again:
function add(secondNumber) {
sum = value + secondNumber;
value = sum;
}
You’ll notice that this add
method (also a function!) cannot do anything meaningful without value
. The function is meaningless outside the parent object.
Many languages make that relationship more explicit in some way. For instance, JavaScript uses the this
keyword inside methods to reference the method’s context. Python uses a hidden first parameter, conventionally named self
, to refer to the object, like this:
class Number:
def __init__(self, num):
self.value = num
def add(self, second_number):
sum = self.value + second_number
self.value = sum
three = Number(3)
three.add(1)
three.value
→ 4
A word of caution, however: some languages confuse the issue a little with something called a static method. Static methods are methods because they’re attached to a class. But they’re often used like plain functions because they don’t depend on an instance of that class to do interesting things. For example, JavaScript’s Math
object has a bunch of static methods that act only on the arguments to those methods. Here’s an example with Math.abs
, which finds the absolute value of a number:
Math.abs(-5)
→ 5
Math.abs(3)
→ 3
There you have them: functions, methods, and static methods. Now you’re just as much an expert as I am.