Friday, 3 April 2020

Difference between Method and Function in python with example

Function:

A function is a set of code or block of code which process data and return or create a output for the given input.

Eg:
def addValues(xy):
    return x+y

x=int(input("Enter your x value:"))
y=int(input("Enter your y value:"))
print(addValues(x, y))

When you consider the above python code, addValues() is a function.

Method:

A method is similar to function where has a set of code or a block of code, which is represented by a name. When the name is called the entire block of code will be executed. 

Eg:
class mathis:
    def addValues(xy):
        return x+y

x=int(input("Enter your x value:"))
y=int(input("Enter your y value:"))
print(mathis.addValues(x, y))

In the above python code, the same addValues() is defined into a class which is referred as a method.

Here the difference between method and function is:

  • If a function is defined inside a class then it is called as method.
(note: Method and function has similar properties where both is used to perform an operation with block of code.)

No comments:

Post a Comment

Expense Handler Application with advance technologies

Budget Planner  One of the application developed with Ionic 4 and Python-Flask.  Ionic 4 code:  https://github.com/logeshbuiltin/Expense...