4

In several of my programming courses in the University, my teachers always told me the following:

A function and a procedure are basically the same thing: the only difference is that a function returns a value, and the procedure doesn't.

That means that this:

function sum($a, $b) {
    return $a + $b;
}

... is a function, and this:

function sum($a, $b) {
    echo $a + $b;
}

... is a procedure.

In the same train of thought, I've seen that a method is the equivalent of a function in the OOP world.

That means that this:

class Example {
    function sum($a, $b) {
        return $a + $b;
    }
}

Is a method — but how do you call this?

class Example {
    function sum($a, $b) {
        echo $a + $b;
    }
}

What's the equivalent name, or how do you call a method that doesn't returns anything?

7
  • 26
    It's called method, but you can call it whatever you like. Now stop worrying about how to call stuff, and go build stuff instead.
    – yannis
    Commented Sep 10, 2012 at 3:28
  • 4
    I was taught the difference was that functions (are pure and thus) don't have side affects while procedures do. To me what you are taking about is the difference between procedures/functions in pascal. But really you are worrying about abstract concepts that depend on the current language for exact definition. As long as people understand what you mean it should not make a difference. Commented Sep 10, 2012 at 3:34
  • -1: Are you gearing up for Trivial Pursuit (Programmer's Edition)? What problem are you trying to solve?
    – Jim G.
    Commented Sep 10, 2012 at 3:42
  • 1
    @YannisRizos while it's true that you can call things whatever you want, it turns out to be useful to have a shared terminology (or ubiquitous language) for discussing things with others.
    – user4051
    Commented Sep 10, 2012 at 8:33
  • 1
    How you call it is probably Example.sum(2, 5);. You want to know what you call it.
    – OrangeDog
    Commented Sep 10, 2012 at 8:50

5 Answers 5

22

Method as a concept encompasses both the function and procedure notations.

1
  • This might be, as you have voted, the most precise answer. I'm guessing that, as a whole, the programming community does not differenciate between a function and a procedure in the OOP world.
    – AeroCross
    Commented Sep 12, 2012 at 0:47
7

Imperative programming distinguishes value-returning blocks from merely code-executing blocks via names: function and procedure. OOP doesn't; as Jesse explained, both are called methods. This is one of the major problems in moving between programming (and, in fact, natural) languages: not only do they use different terms for the same things, they don't even agree about which things count as "the same" and which don't.

4

One of the Object-Oriented Design Principles set forth by Bertrand Meyer in his book Object-Oriented Software Construction is the Command / Query Segregation Principle. In it he suggests that every method should either be a Query, which he defines to be a pure, referentially transparent method that returns (a function of) some internal state and has no side-effects, or a Command which alters some internal state or performs a side-effect, and which has only side-effects and does not return a value.

So, following this terminology, you could call them Query Methods and Command Methods.

Another possibility that I have encountered is calling them Functional Methods or Procedural Methods.

Pure Methods and Impure Methods are also sometimes used.

Note that as @Loki Astari pointed out in his comment on your question that there does not exist a universally accepted name for a subroutine which has both side-effects and returns a value. Some people call that a procedure, some call it an impure function, C, JavaScript and Python just call it a function. The same applies to methods also.

2
  • There's also the added problem that quite a bunch of C procedures return an error code. That isn't the goal of the procedure; the goal is to do something. In cleaner languages, exceptions are used to separate true return values from error codes.
    – MSalters
    Commented Sep 10, 2012 at 12:48
  • This is really insightful and informative. Didn't know about the COmmand / Query Segregation Principle. I think it does explain really well the fact that they can be differenciated and recognized as different "things".
    – AeroCross
    Commented Sep 12, 2012 at 0:55
2

This distinction between functions and procedures can be traced back to Pascal and maybe even further. I would speculate, that that's an attempt to accomodate command-query separation in the language design, where commands would be carried out by procedures and queries are carried out by functions.
I believe the word function was chosen here to emphasize the fact, that a query should actually be a function in the mathematical meaning of the term, i.e. repeating the same call again and again should yield the same result - a quality often termed as referential transparency. This doesn't apply to subroutines in general and Pascal didn't actually tie this quality to the concept of a function, random being the canonical proof of that.

In regards to whether a subroutine returns a value or not, there's not much difference between a subroutine, that returns a value directly, or one that takes a variable by reference and stores its result in that variable. In some languages, especially dynamic ones, even subroutines that do not explicitly return a value, do so implicitly. For example JavaScript "procedures" return undefined in such cases.

There's many ways to define procedures and functions. For me, the sensible definition is, that functions are referentially transparent procedures. Such functions are often called pure functions. This can be used in an OOP context. You can also speak of commands and queries, or of mutators and accessors. But then again, a core idea of OOP is not to make assumptions about what's going on within the method owner. So at the abstraction level that "OOP is really about" the distinction doesn't exists. There's only methods.

2
  • The distinction between functions and procedures can be traced all the way back to FORTRAN II. Commented Sep 10, 2012 at 15:33
  • Perhaps the root of all evil in this question is that my teacher was a Pascal teacher. Although, my C teacher applied the same concept of procedure / function. +1 for the term referential transparency.
    – AeroCross
    Commented Sep 12, 2012 at 0:56
0

A function is a collection of computing statements. So is a procedure. In some very anal retentive languages, though, a function returns a value and a procedure doesn't. In such languages procedures are generally used for their side effects (like I/O) while functions are used for calculations and tend to avoid side effects. (This is the usage I tend to favour. Yes, I am that anal retentive.)

Most languages are not that anal retentive, however, and as a result people will use the terms "function" and "procedure" interchangeably, preferring one to the other based on their background. (Modula-* programmers will tend to use "procedure" while C/C++/Java/whatever will tend to use "function", for example.)

A method is just jargon for a function (or procedure) bound to a class. Indeed not all OOP languages use the term "method". In a typical (but not universal!) implementation, methods have an implied first parameter (called things like this or self or the like) for accessing the containing class. This is not, as I said, universal. Some languages make that first parameter explicit (and thus allow to be named anything you'd like) while in still others there's no magic first parameter at all.

class MyClass
{
  int memberVariable;

  void setMemberVariableProcedure(int v)
  {
    memberVariable = v;
  }

  int getMemberVariableFunction()
  {
    return memberVariable;
  }
};

void plainOldProcedure(int stuff)
{
  cout << stuff;
}

int plainOldFunction(int stuff)
{
  return 2 * stuff;
}

In this code getMemberVariableProcedure and getMemberVariableFunction are both methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.