Functions vs. methods

Functions vs. methods

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 
        


Paras Chauhan

Salesforce| 8x Salesforce Certified | Experience Cloud| | Service Cloud | Sales Cloud

9 个月

Well described ??

要查看或添加评论,请登录

Shalu Pundir的更多文章

社区洞察

其他会员也浏览了