62

What is the difference between a method and a function? Is it that a method returns a value and a function doesn't?

2
  • 1
    There really is no technical difference within php. But in my mind, a function is a more mathematical thing; it doesn't change state, only returns a value (like f(x) = 2x). A method modifies state (like outputing "hello world") and may return a value. This is basically a dupe of: stackoverflow.com/questions/43777/… Hopefully that answers your question though.
    – Tyler
    Jan 30, 2011 at 6:58
  • Function has a meaning in most programming languages unrelated to its meaning in mathematics. May 16, 2013 at 17:56

8 Answers 8

164

Method is actually a function used in the context of a class/object.

When you create a function outside of a class/object, you can call it a function but when you create a function inside a class, you can call it a method.

class foo {
   public function bar() { // a method
     ........
   }
}

function bar() {  // a function not part of an object
}

So an object can have methods (functions) and properties (variables).

1
  • 6
    Methods have to be called like so $this->bar() if within that class, otherwise you must first instantiate the class and then call it using the class instance (object), like so: $foo->bar(). For functions it's simply bar(). Nov 3, 2012 at 20:51
8

The words are not opposed to each other but rather describes two possible aspects of a subroutine. An attempt to define the words follows:

Subroutine: A set of instructions that can be used several times in the same program.

Function: A subroutine that returns a value. Derived from functions in mathematics (wikipedia).

Method: A subroutine that belongs to an object or a class. Could be a function.

I tend to use the word "function" for every subroutine that has no side effects but returns one clear value and the word "method" for every subroutine that has a side effect.

5
  • These are the definitions of subroutine and function as used in Fortran, one of the first high-level languages. That was a very long time ago. I can't think of a contemporary language that uses "subroutine" as a keyword. The C language used "function" for both, and that's the way most other languages use the word. (You're OK for method.) May 16, 2013 at 17:58
  • But the question says nothing about keywords. It is a terminology question about how to describe code in spoken language. I can recommend the wikipedia article en.wikipedia.org/wiki/Subroutine
    – Jonatan
    May 24, 2013 at 9:55
  • @MarcRochkind Doesn't (Visual) Basic define functions using the keyword Sub?
    – Byson
    Oct 8, 2014 at 10:34
  • Basic is far from a contemporary language. It is over 45 years old. Oct 8, 2014 at 13:41
  • 1
    i find these archaic terms useful even in 2019.
    – Randy L
    Jun 6, 2019 at 17:21
6

The difference between the expressions "method" and "function" is that a "method" is a member function of a class, whereas a standalone function does not, and a standalone function usually exists in global context.

2

Both are used interchangeably, but function is the terminology used in structural languages and method is the terminology used in Object Oriented Langauages. Also methods exists within objects while functions can exist without objects as well.

2

Function is a generic term to be used in procedural programming approach where as Method is a term to be used in Object oriented programming approach to define a class property.

1

We define method inside class , we define function out side class, function is not part of class

0

In one line, a method is a function but a function is not necessarily a method. The difference is that a method is used to describe functions defined in classes that are used with instances of those classes.

package {class Example {
  public function iAmAMethod():void {
     addEventListener("listenerFunctionIsNotAMethod", function(event:Event):void {
        trace("inline function, yay!");
     });
  }

}

0

Method and function are same things but their context are different. A function inside a class called a method. like:

class foo{
   function bar(){ //code }       

}

in this case bar() is a method. to execute this method you must create an object of class foo and call it like $foo->bar();

You can write a function out of class .

bar(){ //code }

you can call it bar()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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