Our Blog

Nov 21 2014

Functions vs. Methods: Know the Difference

Jason Brown

I know grammar nerds who gleefully correct others for using accept when they should be using except. And while I’m not usually one to correct others, I do occasionally hear colleagues using function and method interchangeably. I think it’s important to point out that the two concepts, while very similar, do not mean the same thing.

This post provides a few examples to illustrate the subtle differences (using Python syntax).

Functions

A “function” is a snippet of code that runs when called upon by name. It receives a set of arguments as inputs, performs an operation and then returns. Below is an example of a function that has both an object and a string as parameters:

#———————————————————————————–
def Eat(animal, food):
print “{} ate the {}.”.format(animal.name, food)

class Cat(object):
def __init__(self, name):
self.name = name

def main(argv=None):
Spot = Cat(‘Spot’)
Eat(Spot, “cat food”)
#———————————————————————————–

In this example, we have created class Cat that has a single member – name. The main function creates an instance of class Cat and gives it the name Spot. The function Eat() is called, passing in Spot and the message “cat food” as arguments. The output when running this script will be:

Spot ate the cat food.

Methods

Just as all squares are rectangles, all methods are functions. A “method” is a function that belongs to an object. Since it is a member of the object, it can operate on other members of that class. Let’s expand our example to demonstrate:

#———————————————————————————–
def Eat(animal, food):
print “{} ate the {}.”.format(animal.name, food)

class Cat(object):
def __init__(self, name):
self.name = name

def Eat(self, food):
print “{} ate the {}.”.format(self.name, food)

def main(argv=None):
Spot = Cat(‘Spot’)

Eat(Spot, “cat food”)
Spot.Eat(“cat food”)
#———————————————————————————–

The output when running this script will be:

Spot ate the cat food.
Spot ate the cat food.

Calling the Eat() function and the Eat() method provides the exact same result. Notice, however, the way we invoke them. The Eat() method does not need to send the Spot object to the function as the animal parameter, since it already has access to the members of the Spot object. Instead of printing the name using animal.name, the method uses self.name. Why is this important? Let’s see how we can make the method more useful by complicating our example a little more:

#———————————————————————————–
def Eat(animal, food):
animal.Eat(food)

class Cat(object):
def __init__(self, name):
self.name = name

def Eat(self, food):
if(food == ‘dog food’):
print “{} won’t eat the {}.”.format(self.name, food)
else:
print “{} ate the {}.”.format(self.name, food)

class Dog(object):
def __init__(self, name):
self.name = name

def Eat(self, food):
print “{} ate the {}.”.format(self.name, food)

def main(argv=None):
Spot = Cat(‘Spot’)
Porthos = Dog(‘Porthos’)

Eat(Spot, “dog food”)
Eat(Porthos, “dog food”)
#———————————————————————————–

The output when running this script will be:
Spot won’t eat the dog food.
Porthos ate the dog food.

Notice how we have changed the Eat() function. Instead of printing the message itself, it calls the Eat() method of the animal object that is passed to it. What this means is that within the context of the function, it isn’t necessary to know the type of the animal object – the Eat() method will run and execute according to how it is defined inside of the class. In other words, the Eat() function doesn’t care what kind of animal is eating, it feeds the food to it anyway.

Also notice that since the Eat() method for Dog and the Eat() method for Cat are different, they produced different results – each type of animal eats in its own way.

Differences at a Glance:

  • Functions are defined outside of classes while methods are defined inside classes.
  • Methods can operate on other members of the class that it belongs to, while functions cannot directly access the members of a class.
  • Methods with the same name that belong to different classes can be called upon the same way but execute different code, whereas two functions cannot have the same name.

Hopefully this clarifies things a bit. What terms have you heard fellow programmers misuse or confuse? Please tell us below…

One thought on “Functions vs. Methods: Know the Difference”

  1. Jake North says:

    I know RBN isn’t a school, but I love when you guys take time to talk about programming basics.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to RightBrain

Recent Posts

Categories

Recent Comments

Archives

Tags