I’ve heard that methods are more Object-Oriented 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?
I have taken 3 quarters of Java programming and functions have never been mentioned, I want to know the differences, strengths and weaknesses.
8
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.
7
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.
10
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?
3
In my opinion in Java there are no functions
. All we have are:
- Object’s methods
- 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.
3