Method vs Function vs Procedure

  • Simple question, but I often hear these three terms defined with such ferocity, but which have been known to me to mean different things over the years.

    What are the "correct" definitions of "Procedures", "Methods", "Function", "Subroutines", etc?

    you forgo "routine"

    @teresko: I think "subroutine" is more common.

  • I'm going with a different answer here: practically speaking, there's really no difference, with the slight exception that "method" usually refers to a subroutine associated with an object in OO languages.

    The terms "procedure, function, subroutine, subprogram, and method" all really mean the same thing: a callable sub-program within a larger program. But it's difficult to come up with a definition that captures all variant usages of these terms, because they are not used consistently across programming languages or paradigms.

    You might say a function returns a value. Well, the following C function doesn't return a value:

    void f() { return; }
    

    ...but I doubt you'd find anyone who would call it a procedure.

    Sure, in Pascal, procedures don't return values and functions return values, but that's merely a reflection of how Pascal was designed. In Fortran, a function returns a value, and a subroutine returns multiple values. Yet none of this really allows us to come up with a "universal" definition for these terms.

    In fact, the term "procedural programming" refers to a whole class of languages, including C, Fortran and Pascal, only one of which actually uses the term "procedure" to mean anything.

    So none of this is really consistent. The only exception is probably "method", which seems to be used almost entirely with OO languages, referring to a function that is associated with an object. Although, even this is not always consistent. C++, for example, usually uses the term "member function" rather than method, (even though the term "method" has crept into the C++ vernacular among programmers.)

    The point is, none of this is really consistent. It simply reflects the terminology employed by whatever languages are en vogue at the time.

    That's precisely what I thought the answer was. (I should have added "subroutine" as another variant in hindsight.) Can I ask: Why wouldn't you find anyone who call that C function a "Procedure"? Because it's technically incorrect, or because the term "procedure" is currently out of vogue?

    C programmers use the term "function" simply because the designers of C used that term.

    Let's not throw the baby out with the bathwater. Just because the terminology is not used with full consistency, doesn't mean that the different terms don't have different meanings. @Bruce's and @Frank's definitions are widely recognized, not idiosyncratic. The fact that the meanings are not universal is important, but it doesn't justify the leap to "practically speaking, there's really no difference". (@Django)

    Kind of dual to C++, which calls methods "member functions", Java and C# call functions "static methods".

    Bruce's answer is definitely the one you should go for if you're new to programming. His definitions will be absolutely correct, 99% of the time. But I was looking for more of a technical/theoretical answer. Sometimes newer programmers know only their own domain, and insist that is all there is. In reality there are programmers working today who still use older languages, and who are not "wrong" for using different definitions. That was what I was most interested in.

    Actually the term `procedure` has a strict defined meaning in Fortran. It means a `function` or a `subroutine` (for example, there are `procedure pointers`). Also subroutine does not have to return anything, or more accurately it does not return anything. It may, but doesn't have to, alter its arguments.

    You don't know _me_. I might just put that into production ;)

    I would call it a procedure.

    And the slight exception that (pure) functions don't use external state as input.

    A method has an implicit `this` parameter that holds a reference to the object instance the method is associated with. That's the key distinguishing factor between a method and the function/procedure.

    nice answer, what is missing imho is that within each language those terms usually do have a quite clear definition (just across languages those are not necesarily consistent) and sometimes there really is a difference made between the terms

    In Ruby, Smalltalk etc. other Pure OO languages, there's no function except module functions in Ruby. They are not the same thing.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM