4

Are there official definitions for these terms in OOP? Or have they just evolved over time and depending on your former computer science education (or even your age) you use one or the other?

So far i found good definitions of method vs. function:

Difference between a method and a function

A function is a piece of code that is called by name. ... All data that is passed to a function is explicitly passed.

A method is a piece of code that is called by name that is associated with an object.

And function vs. procedure:

What is the difference between a "function" and a "procedure"?

A function returns a value and a procedure just executes commands.

A procedure is a set of command which can be executed in order.

In most programming languages, even functions can have a set of commands. Hence the difference is only in the returning a value part.

Even if there are subtle differences in the definition by the authors the main aspect seems to be: A method is always something which operates on an object in contrast to a function which gets all data passed to it by its parameters. If a function does not return a value it is called a procedure.

But how are subroutine and especially operation linked to these terms?

EDIT: Since this seems to be too broad here is an attempt to narrow it down: method, procedure and function are pretty clear from my former research. Also subroutine is not that vague anymore.

So the question is: What is an operation in the field of computer science?

thalm
  • 2,738
  • 2
  • 35
  • 49
  • Pick one language, **not** three unrelated ones. – Elliott Frisch Apr 10 '15 at 00:40
  • True, removed all. The question is not specific to any language. – thalm Apr 10 '15 at 00:42
  • Your definitions would seem to strongly imply that a non object oriented language cannot have methods. I do not believe that is true. I suspect your definitions (and terms) might have subtly different meanings depending on implementation language. And that doesn't even include things like signals or slots. – Elliott Frisch Apr 10 '15 at 00:44
  • @ThomasMatthews this question is a poor fit for Programmers - it would be quickly voted down and closed over there, see http://meta.programmers.stackexchange.com/questions/6483/why-was-my-question-closed-or-down-voted/6490#6490 Recommended reading: **[What goes on Programmers.SE? A guide for Stack Overflow](http://meta.programmers.stackexchange.com/q/7182/31260)** – gnat Apr 10 '15 at 11:39
  • @ElliottFrisch The question targets OOP, not procedural languages. I should make that more clear in the question. I only tagged it with oop. – thalm Apr 10 '15 at 15:59

1 Answers1

4

The following is my understanding. Note that these are all "soft" definitions - there is no official definition of any of these, and so the exact meaning may vary between languages. (For example, "static methods" in Java and C# don't fit my definition of "method", unless you consider classes to be objects)

  • A subroutine is "some code that you can call whenever". It's literally a routine that is called by other routines (hence a sub-routine). "Subroutine" and "routine" seem to be quite archaic terms.

    Think of BASIC's GOSUB instruction: (never mind that BASIC is a terrible language)

     10 PRINT "Hello"
     20 GOSUB 100
     30 PRINT "Goodbye"
     40 GOSUB 100
     50 END
    
    100 PRINT "World"
    110 RETURN
    

    This prints:

    Hello
    World
    Goodbye
    World
    

    In Visual Basic, a subroutine or Sub is a function which doesn't return anything.

  • A procedure is the same thing as a subroutine, but less archaic.

    A procedure can also mean a function which doesn't return anything.

  • A function is like a procedure, but instead of just being a list of commands, it can take parameters and return a value. You should be familiar with functions from many languages, but here's an example in C++:

    #include <iostream>
    using namespace std;
    
    int calculate(int a, int b) {
        return a + b - 2;
    }
    
    int main() {
        cout << calculate(5, 2) << endl;
        cout << calculate(100, 20) << endl;
    
        int i = calculate(5, 1);
        for(int k = 0; k < i; k++) // repeat the next line i times
            cout << "Hello" << endl;
    }
    

    This prints:

    5
    118
    Hello
    Hello
    Hello
    Hello
    

    Notice that the function calculate doesn't do the printing itself - instead, it returns a number which main can choose to print, or to do something else with. The third call to calculate returns 2, but the 2 isn't printed - instead, it determines how many times "Hello" is printed.

    A function with no arguments, that returns nothing, is equivalent to a procedure (or subroutine).

  • A method is a function which can be called on an object. You might be familiar with non-static methods from Java or C#. A method has access to the state of the object it was called on (which is called the receiver).

    Unlike with functions, methods are often polymorphic on the type of the receiver - it's not possible to directly see which sequence of commands will run when you make a method call.

    A simple example of methods in Java, demonstrating polymorphism:

    class Car {
        int getNumberOfWheels() {return 4;}
        void printNumberOfWheels() {
            System.out.println(getNumberOfWheels());
        }
    }
    
    class HondaCivic extends Car {
        // no code here
    }
    
    class ReliantRobin extends Car {
        int getNumberOfWheels() {return 3;}
    }
    
    class Main {
        public static void main(String[] args) {
            Car h = new HondaCivic();
            Car r = new ReliantRobin();
    
            h.printNumberOfWheels();
            r.printNumberOfWheels();
        }
    }
    

    This prints:

    4
    3
    

    even though you might think that printNumberOfWheels always prints 4 - subclasses can "intercept" the getNumberOfWheels method call.

user253751
  • 57,427
  • 7
  • 48
  • 90