Functions vs. methods
Shalu Pundir
Sr Software Developer at Softobiz Technologies Private Limited PHP | Phython | Fast Api | Nodejs | Nest Js | Angular | Postgres | MongoDb
A method is a specific kind of function - it behaves like a function and looks like a function, but differs in the way in which it acts, and in its invocation style.
A function doesn't belong to any data - it gets data, it may create new data and it (generally) produces a result
Note : A method is owned by the data it works for, while a function is owned by the whole code.
In general, a typical function invocation may look like this:
function takes an argument, does something, and returns a result
result = function(arg)
领英推荐
def my_function(arg1, arg2):
# Function body
return arg1 + arg2
result = my_function(3, 5) #call of function
A typical method invocation usually looks like this:
Method preceded by the name of the data which owns the method. Next, you add a dot, followed by the method name
result = data.method(arg)
class MyClass:
def my_method(self, arg1, arg2):
# Method body
return arg1 + arg2
obj = MyClass()
result = obj.my_method(3, 5) #preced with data using dot
Salesforce| 8x Salesforce Certified | Experience Cloud| | Service Cloud | Sales Cloud
9 个月Well described ??