What is the difference between a method and a function?

What is the difference between a method and a function? Does this mean that the method returns a value, but the function does not work?

+46
php
Jan 30 2018-11-11T00:
source share
7 answers

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

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

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



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



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

+123
Jan 30 2018-11-11T00:
source share

Words do not contradict each other, but rather describe two possible aspects of the subroutine. An attempt to define the words follows:

Subprogram: A set of instructions that can be used several times in one program.

Function: A routine that returns a value. It is made from functions in mathematics ( wikipedia ).

Method:. A subprogram belonging to an object or class. May be a function.

I usually use the word “function” for each subprogram that has no side effects, but returns one clear value and the word “method” for each subprogram that has a side effect.

+6
Apr 03 2018-12-12T00:
source share

The difference between the expressions “method” and “function” is that “method” is a member function of a class, while an autonomous function does not exist, and an autonomous function usually exists in a global context.

+5
Jan 30 '11 at 7:00
source share

Both are used interchangeably, but a function is the terminology used in structural languages, and a method is a terminology used in object-oriented lengauages. There are also methods in objects, while functions can exist without objects.

+2
Jan 30 '11 at 7:01
source share

A function is a general term that will be used in procedural programming, where a method is a term that will be used in object-oriented programming to define a class property.

+2
Jul 25 2018-12-12T00:
source share

We define a method inside a class, define a function from a side class, the function is not part of the class

+1
Apr 20 '13 at
source share

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

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

}

0
Jan 30 2018-11-11T00:
source share



All Articles