what's the differwnce btween functions and methods? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

what's the differwnce btween functions and methods?

9th Apr 2020, 1:25 AM
Mohammed Fakhereldinn
Mohammed Fakhereldinn - avatar
3 Answers
+ 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.)
9th Apr 2020, 1:50 AM
Ayush Dwivedi
Ayush Dwivedi - avatar
+ 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.
9th Apr 2020, 1:36 AM
Ayush Kumar
Ayush Kumar - avatar
+ 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.
9th Apr 2020, 4:55 AM
Ishan Shah
Ishan Shah - avatar