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 "Procedure", a "Method" and a "Function"?
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:
...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.
|show 1 more comment|
A function returns a value, but a procedure does not.
A method is similar to a function, but is
|show 3 more comments|
A function is something that takes a bunch of inputs and returns one or more values. If the returned values are entirely determined by the inputs, and the function doesn't have any side effects (logging, perhaps, or causing state changes outside itself), then it's called a pure function.
A procedure is a function that doesn't return a value. In particular, this means that a procedure can only cause side effects. (That might include mutating an input parameter!)
A method is a function that closes over a set of variables, that is, a closure. It takes zero or more input parameters, has access to this set of variables, and returns zero or more values.
In most mainstream OO languages, those closed-over variables are called the member fields, or instance variables, of an object. A method be a pure function, an impure function or a procedure.
The latter definition leads to the object = struct + closures correspondence.
Bruce has a good answer. I would add, semantically:
good detailed answers above; the short story is that they'll all flavors of subroutines; what is meant by each term will vary according to the programming language context
in general, functions return a value, but they don't have to
methods are a generic OOP terms at present
in SQL, stored procedures have outputs but typically only return an error code, while user-defined functions must return a value (which may be a result-set)
again, the precise difference between these terms depends on who you're talking to!
80% of proficiency is directly related to familiarity with nomenclature,
95% of productivity is the ability to identify what is useful at the moment despite the terms used to describe it
I pretty much prefer to call them all methods in c# except back when I used MSSQL we had sproc's, but of course now we use Postgres and they are called functions.