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
6  
Java doesn't have standalone functions at all. Are you talking about static methods? – Mat Dec 26 '13 at 6:35
6  
"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
5  
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
1  
Voting to leave open. The answers and a few of the comments do an excellent job at explaining the difference between the two terms as well as when it's appropriate to use each term. – GlenH7 Dec 26 '13 at 17:22
2  
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
2  
The only language I've used that made a distinction between procedures and functions is VB – James Dec 26 '13 at 16:54
2  
... 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
1  
Getter methods returns a value. Aren't they functions ? – Tulains Córdova Dec 27 '13 at 12:36
1  
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
2  
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. That is, the method to call will be looked up at runtime, based on the actual type of the object. This is know as "dynamic method dispatch" or "virtual methods".

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").

In certain languages, such as Java and C#, you can define "static methods" that do not receive a this parameter. They work exactly like "standalone" functions and use the class as a namespace. Such namespacing may sometimes make sense, when a static method of a class is used to look up or create new instances of that class.

I hope you now have a better picture.

share|improve this answer
2  
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 ? – Tulains Córdova 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 foo.bar() 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

There are no real differences between methods, functions and procedures, they're all the same beast: a subroutine, a section of a program, often named doing a specific task.

A method is just a function by another name.

Even java has functions: in "a + b" the "+" operator is a function. Even if it goes by another name.

Imagine you would make a new language: JavaBlub, which is a verbatim copy of Java except for one thing: it hasn't got methods, it's got blubzorz. And blubzorz in JavaBlub do the same thing as methods in Java.

Would you take me seriously when I put on a straight face and say: JavaBlub is different from Java because it doesn't use methods?

share|improve this answer
    
I think Andrew's question was not pointing to so "low" level. I think he was asking in terms of Java. In terms of Java's block code. As we know Java is a High-level programming language what means (among many other things) an strong abstraction from the details of the computer (those you are mentioning). My answer was focusing in such "abstraction". The abstraction that Java does, where functions as he's thinking (in comparission with methods) doesn't exist. Of course under all Java's abstraction there's only logic operations, 1 and 0. But it was not the question :-) – Laiv Apr 29 at 12:08
    
@Laiv I'm not talking about levels of abstraction, just saying that methods and functions are the same thing by another name, one is not an abstraction of the other. – Pieter B Apr 29 at 12:14
    
I'm in agreement with you. Both are the same thing. So to his question I was wondering if someone could show me an example of a function and a method you say both are the same thing and I say There're no 2 things to be compared. Just methods (again, in terms of Java language). We are saying the very same from different points of view. May be I'm not capable to explain it as well as I would like – Laiv Apr 29 at 12:27

In my opinion in Java there are no functions. All we have are:

  1. Object's methods
  2. Class methods (the static ones)

Functions, as standalone functions, do not exist in Java.

I would like to support my brief and categorical answer, citing Java's Official Web Page

Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.

Here's the link (In case of someone wants to start with Java concepts before to start with the pure language)

Java Tutorial - What Is an Object?

I guess we often call functions to methods indistinctly. In my case is matter of legacy. My firt langauges were C and Visual Basic. I lernt to design algorithm with flow diagrams where processes (at diagram) were often called (by my teacher) functions or procedures. So I find myself (quite often) at job, saying function instead of methods.

When I was starting with Java (10 years ago), my teacher tried to tag methods in 2 groups:

Processors. Those methods that causes a change to the arguments recived or to the object's attribute (state change). Whether or not returns values (void). Or generate a new value in to the system. Whether or not is persisted.

Consultors are those that doesn't cause changes to the object (attributes) or class (static attributes)

At that time I found such catalogation really useful at designing time. Overall because these terms were familiar to me because C and V.B.

Some years later, and after so many bad practices and countless lines of "hilarius code", I would not dare to propose such labeling.

In practice, we end up mixing both concepts in methods that does both things. But that is matter of best practices (or bad, depends on your dead lines). I always encourage my job mates to be consequent with the functionality of their code and the way they define methods and their names.

This's my humble contribution to the question :-)

Hope It will help.

share|improve this answer
    
    
All of them are classes ... with methods. The concept of Function is modeled into OOP as classes and methods. But there's no functions like we understood when we were programming C or Visual Basic. This is what I tried to said. I don't dare to do long argumentations because my little english :-) – Laiv Apr 29 at 10:39
    
@laiv java.lang.math is nothing but a namespace containing functions disguised as class methods because, well because of OO.....The concept of functions does exist, just by another name: methods. They're the same thing. – Pieter B Apr 29 at 11:02

Your Answer

 
discard

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.