Functions vs Methods - Detailed Notes with Examples
1. What is a Function?
A function is an independent, reusable block of code that performs a specific task. It does
not belong to any object. You simply call it using its name.
Characteristics of Functions:
• Functions are defined using the 'def' keyword.
• They work on values (arguments) passed to them.
• They are not tied to any object.
• Examples include len(), print(), type(), sorted().
Example of a Function:
Example:
def add(a, b):
return a + b
result = add(10, 5)
print(result)
Explanation:
The function 'add' takes two arguments and returns their sum. It is called directly using its
name 'add()', without any object.
2. What is a Method?
A method is a function that is associated with an object. You must call it using dot notation
([Link]()). Methods work on the object they belong to.
Characteristics of Methods:
• Methods belong to objects (strings, lists, dictionaries, etc.).
• They receive the object itself as their first argument.
• They cannot be called without an object.
• Examples: upper(), split(), append(), keys().
Example of a Method:
Example:
name = "disha"
result = [Link]()
print(result)
Explanation:
'upper()' is a method. It belongs to the string object 'name'. It converts the string into
uppercase. You must call it using [Link]().
3. Difference Between Function and Method
A clear comparison is shown in the table below:
Feature Function Method
Belongs to an object? No Yes
Called using object? No (len(x)) Yes ([Link]())
Definition using 'def' Inside a class
Works on Arguments passed The object
Example len('abc') 'abc'.upper()
4. Memory Trick:
Function = Independent
Method = Function inside an object