what's the differwnce btween functions and methods? | SoloLearn: Learn to code for FREE!

+3

what's the differwnce btween functions and methods?

4/9/2020 1:25:52 AM

Mohammed Fakhereldinn

3 Answers

New Answer

+3

A function is a piece of code that is called by name. It can be passed data to operate on (i.e. the parameters) and can optionally return data (the return value). All data that is passed to a function is explicitly passed. A method is a piece of code that is called by a name that is associated with an object. In most respects it is identical to a function except for two key differences: A method is implicitly passed the object on which it was called. A method is able to operate on data that is contained within the class (remembering that an object is an instance of a class - the class is the definition, the object is an instance of that data). (this is a simplified explanation, ignoring issues of scope,  etc.)

+4

What is the difference between amethod and a function?  Method and afunction are the same, with different terms. A method is a procedure orfunction in object-orientedprogramming. A function is a group ofreusable code which can be called anywhere in your program.

+2

Technically method is a function that is also an object property. So: imObject.imMethod() While function would be just: imFunction() another example : A function is a set of codes to do some tasks in JavaScript. A method is also a function that is used as a property in Object. Ex: Function: below is the function to add two numbers. var func = function(a,b){ var sum = a + b; return sum; } Method: var obj = { name: “Amod”, empId: “A1122”, age: function(presentYear,DOB){ return presentYear - DOB} } Using a function as property inside an object is a method. In fact, both are functions.