Any answer that limits this to a particular language is inherently erroneous. In addition, you must also deal effectively with static methods and routines.
Computer science began with the term subroutine . Small sections of repeatable code that can be executed arbitrarily to perform a common action. Examples can be found in early programming languages such as BASIC.
Functions are the evolution of routines. They accept arguments and may or may not return a value. They take some concepts from mathematics - input, are translated to a given conclusion.
With objects, we should be able to call actions on objects, and we do this by providing methods . Like functions, they take arguments and may or may not return a value.
Static methods are designed to work with all possible objects of the class.
The problem is that pure object-oriented programming leaves no room for defining functions (or indeed routines). And languages that evolve to become object oriented often retain the syntax of functions for implementing methods.
In Java, we use the 'Utility' classes to provide functions as public static . The Math class in JavaScript is another example of this.
In PHP, we allow the use of the word function to define methods.
In C ++, we see both functions and methods that are not differentiated. In fact, C ++ does not refer to methods, calling them member functions.
source share