Main Content

What is the difference between a function and a method?

Archive - Originally posted on "The Horse's Mouth" - 2015-03-04 22:37:58 - Graham Ellis

I strongly encourage delagates on our courses to divide their code into managable, understandable, testable, re-usable chunks rather than write it all into a single block. And the adjectives I've used (managable, understandable, testable, re-usable) explain why.

You can process data within these chunks - encapsulating the logic in there ... and indeed you can retain the data needed to be used by different chunks within the area of code that's included in the chunks so that after initially passing it in the programmer who makes use of the chunks needed worry about managing that data, not indeed will (s)he have to understand what can be passed in and out where.

Let's see an example (in Python) ... here's some sample data being set up

  numbers = [10,8,7,2]

Passinng the data into a series of named chunks of code - functions - where each performs a task. A good way to do things, but still the need to pass the data in to each of the named bloacks (functions).

  avg = average(numbers)
  r = ageRange(numbers)
  print avg,r


If you pass the data in to a named set up chunk of code - a constructor method which stored the data under some sort of handle (an instance variable), you can then access it as many times as you need though other methods without having to keep passing in / managing the data on an ongoing basis.

  andy = children(numbers)
  avg2 = andy.getAverage()
  r2 = andy.getRange()
  print avg2,r2


In my first example code, I refrerred to the variable numbers two times and if I had required to perform 10 operations, that would have been ten references back to my original data. But in the second exanple I only referred to my data one time and if I had been required to perform 10 operations, I would still only have had one reference back to my original data. That saves me code, makes for clearer code and provides me with an environment in which its far harder to make silly logic errors.

• A name piece of code is called a function
• A named piece of code that runs on a piece of stored data is called a method

The complete code for the example above (showing definition and use of the function and method approach within the same source file) is [here] - written to explain the difference on yesterday's learning to program in Python course.

There are other advantages of using methods rather than functions too ...

• methods are limited to work on the right type of data, so you can have lots of methods all with the same name working on different data types. A big advantage when you put multiple programmer's code together and it turns out that two of you have used the same name.

• if you intentionally use the same name for the same type of operation on different (but similar) data types, you can write common code to handle both data types at a higher level and your program will automatically select the right one to run at the right time. This is known as polymorphism and saves a lot of coding using switch and case and if elseif elseif elseif else type structures.

There is an initial overhead in setting up objects, but in languages like Python this has been reduced toa tiny overhead and advise to new programmers / for new applications must be to use methods (which are excellent practise) rather than functions (which are good practise) most of the time.