Methods vs Functionw

A method is a procedure in object-oriented programming. A function can be refers as a cluster of reusable codes that can be called anywhere in your program.

Methods vs Functionw

Functions

The function is a block of code that is also called by its name. (independent)

The function can have different parameters or may not have any at all.

if any data(parameters)are passed, they are passed explicitly.

It may or may not return any data.

The function does not deal with Class and its instance concept.

Functions Example:

def add(a,b):
    return(a+b)
print(add(10,12))
 =================OUTPUT=================
22

Methods

    • Method is called by its name,but it is associated to an object(dependent).
    • A method is implicitly passed the object on which it is invoked.
    • It may or may not return any data.
    • A method can operate on the data (instance variables)that is contained by the corresponding class

Methods Example:

class Add:
    def add_method(self,a,b):
        self.a=a
        self.b=b
        print(a+b)
opr=Add()
opr.add_method(10,12)
=================OUTPUT======================
22