Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I know that methods are more OOP than functions.

I was wondering if someone could show me an example of a function and a method and explain the differences between methods and functions to me?

I have taken 3 quarters of Java programming and functions have never been mentioned, I want to know the differences, strengths and weaknesses.

share|improve this question
Java doesn't have standalone functions at all. Are you talking about static methods? – Mat Dec 26 '13 at 6:35
"function" is not a useful differentiation in this context. An object's method is just a special case of "function" attached to an object. Many languages, like JavaScript, even use the keyword function when writing methods. – DougM Dec 26 '13 at 6:54
It is sort of unclear as to what you desire in an answer. Are you asking about the relative merits between OOP and functional programming (if so, in what context)? At current your question seems quite broad. – Kaya Dec 26 '13 at 7:00
A function is a block of code with no state. A closure (function with captured variables) is a function with state and is like an object with exactly one method. An object is like a collection of closures capturing common variables (the object's state). So, you use functions when you need no state, and closures or objects when you do need state. Functions can be thought of as stateless methods, and methods as statefull functions. Once you understand this, you can pick the corresponding constructs in your language of choice. In Java you can use static methods to represent functions. – Giorgio Dec 26 '13 at 10:35
I agree to @GlenH7 that your question should not be closed for the reason he gave, but I downvoted it because it is so very unclear what you really want to know. Please consider to edit it to make it clearer. – Doc Brown Dec 26 '13 at 18:41

Speaking strictly, a procedure is a subroutine that is executed purely for its side effects (like printing something to the screen) and returns no values. A function is a subroutine that always returns the same value given the same inputs and has no side effects. A method is a procedure or function that is associated with a class or object.

The confusing part is when people use these terms, they're not always referring to the pure definitions. For the sake of convenience and consistency, programming languages don't always make a distinction between functions, procedures, and methods. They have one or two ways to declare a subroutine, and whether it's technically a function, procedure, or method depends on how the programmer is using it.

In Java, for example, a procedure is created by having a void return type on a method. A function is a method with a return type and no side effects, like:

int add(int x, int y) {
    return x + y;

People whose only programming experience is in a language like Java often don't even realize there's a difference, because in Java it usually doesn't matter in a practical sense. In a java-only context, programmers often refer to any subroutine as a function, even by those who know the difference, and they mostly go uncorrected except by the very pedantic.

Contrast that with a functional programming language where the difference between a procedure and a function is critical and enforced by the compiler. In that context you will be pilloried for using one term when you mean the other.

Functional programming has plenty of advocates who will tout its advantages. Just search for "advantages of functional programming." In a nutshell, pure functions are easy to compose, execute lazily or out of order, and parallelize.

The disadvantage is a lot of real-world programs deal largely with state, and most programmers find it easier to reason about state in an imperative manner.

share|improve this answer
The only language I've used that made a distinction between procedures and functions is VB – James Dec 26 '13 at 16:54
... and Pascal/Delphi. Noteworthy detail that Prism/Oxygene uses method. BTW, the OPs question was about function/method, not procedure/function. ;-) – JensG Dec 26 '13 at 17:41
Getter methods returns a value. Aren't they functions ? – user61852 Dec 27 '13 at 12:36
Getter methods aren't usually functions in the pure sense, because they potentially return a different value different times you call them. Getters of immutable objects are functions though. – Karl Bielefeldt Dec 27 '13 at 13:26
While the distinction between [pure] functions and [effectful] procedures is important, I don't see what does it have to do with the distinction of either from methods. You can easily have standalone procedures that return void and produce side effects; stdlib.h declares a few. You can have immutable objects with all methods being pure, see java.lang.String for an example. It's passing of an object instance in a special way that makes a method; semantically things like stream.write(data) and write(stream, data) may be equivalent. – 9000 Dec 27 '13 at 15:37

There are no functions per se in Java. All you've got is methods. To imitate functions, Java generally uses static methods (as in java.lang.Math).

There is a number of object-oriented languages that provide free-standing functions, though: among them Python, Ruby, JavaScript, C++, Object Pascal, and even (yuck) PHP. There's a good reason behind that.

A method is basically a function with one extra parameter (invisible in Java). You refer to it as this. That this thing allows you to access the object whose method is being called, so you can think that the entire object is always an implicit parameter to a method, additionally to parameters you normally define.

A method makes sense if it makes use of its object: calls other methods and/or accesses data members. For example, a list can have a getLength() method that knows how to calculate list's length, e.g. by scanning each member. It obviously uses the implicit this object, that is, the list. This is why it needs no explicit parameters.

Else, a function is enough. For instance, to compute a cosine of an angle you only need the angle, and no other state, so cos(float angle) could be a function, and only depend on the explicit angle parameter.

Another important thing is method overriding. (To my ming, this is a dubious practice, but Java uses it very widely.) You declare a certain class (call it Z) a subclass of another class (call it A), and change implementation of some of its methods (suppose we overrode method foo()).

The subclass works like the base class (it is said to provide the same interface) but does it by different means. According to Liskov substitution principle, you can declare a variable of type A, assign to it an instance of type Z, and invoke method foo(); what will be invoked is Z's implementation of foo(), not A's. This is know as "dynamic method dispatch".

What method overriding provides automatically is not easy to directly emulate with functions. (With functions, similar things are usually done with "callbacks" or "higher-order functions").

I hope you now have a better picture.

share|improve this answer
It's worth noting that C# lets you explicitly declare functions with the this parameter outside of the object itself. This allows for what they call "extension methods", which allow you to transparently add functionality to an existing class. – Bobson Dec 26 '13 at 21:27
@Bobson: yes, partial classes is a very nice feature of C#. Even more explicit approach is seen in Go, where you cannot declare methods within a class, because classes do not exist, there are only structs and interfaces. You declare an explicit "receiver" parameter in every function you want to work as a method. – 9000 Dec 26 '13 at 21:31
Since @Bobson bought up .NET. In VB.NET, there is a clear difference between subroutine and function syntax, although both come under the umbrella of "Methods". – James Dec 26 '13 at 22:18
Getter methods returns a value. Aren't they functions ? – user61852 Dec 27 '13 at 12:36
@user61852: Many methods return values. What makes a method a method is the fact that an implicit object instance is passed to it and is used inside it: in that foo is also a parameter to bar() invocation. Obviously a getter needs the instance to get something from, so it's strictly a method. In certain languages one could make cosine a method, too: imagine something like (0.71).cos(). – 9000 Dec 27 '13 at 15:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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