Python Functions & Methods: "Like Two Peas in a Pod", How Similar or Different Are They?
Beginner Friendly
Functions and Methods and Functions and Methods...
We see them everywhere these days.
You probably must have used one or both of them before.
Often, people use them in their codes, they write about them, and if you have been following this blog, you'll notice they were used in creating a simple student grade calculator.
Well, if there is anything they have in common, it should be that their sole purpose is to perform an operation or a task. (Someone said they both have round brackets ()
behind them. Hmmm... ๐ค)
In this article, we will use a simple example to differentiate between a Method and a Function.
Let's get started.
Creating a Function
Let's create a function that can add two numbers :
def add(a, b):
return a + b
Explanation:
- We define the function
add()
using thedef
keyword. - We allow the function to take in two arguments
a
andb
i.edef add(a,b)
. - Then we tell the function to return the addition of
a
andb
i.ereturn a + b
.
Let's pass two numbers into the function and print out the result
print(add(5,5))
See the output below
>>> 10
That was easy, wasn't it?
We have successfully created a function that can add numbers.
Is it possible to also create a method that can add numbers?
Yes, it is.
But Methods can only be created from Classes. This means that we cannot create a method without creating a class first. Methods can not exist on their own.
So to create a method that can add numbers, we need to create a class to which the addition method will belong to.
Creating a Class: A Basic Arithmetic Class
Arithmetics in mathematics involves various operations that can be done with numbers.
Basic Arithmetic is a Class of (Arithmetic) simple operations that involve the addition, subtraction, multiplication, and division of numbers.
Representing the above statement in code will be similar to:
class Basic_Arithmetic:
def add(self, a, b):
return a + b
def subtract(self, a, b):
...
def multiply(self, a, b):
...
def divide(self, a, b):
...
The above block of codes means that from a Basic_Arithmetic
Class, we can .add()
, .subtract()
, .divide()
and .multiply numbers
.
Let's focus on doing addition with this Basic_Arithmetic
Class.
Create an empty Python file. Then copy and paste the code below into it
class Basic_Arithmetic:
def add(self, a, b):
return a + b
Now try to use the .add()
method to add two numbers.
Do something like this:
class Basic_Arithmetic:
def add(self, a, b):
return a + b
print(add(5,5))
When you run this code, this should be your output:
>>> NameError: name 'add' is not defined
Lol... Did you get an error? Of course, you did!
The error says that add is not defined. This is because add()
was defined inside the Basic_Arithmetic
Class so you can not call it without calling the Class first.
We call Classes by creating instances of it called Objects.
We do something like this:
arithmetic = Basic_Arithmetic()
Then we use this Object we just created, arithmetic
, to call the .add()
function and we pass in the numbers we want to add as arguments
arithmetic.add(5,5)
Let's wrap it in a print()
function.
Our entire code should look like this:
class Basic_Arithmetic:
def add(self, a, b):
return a + b
# we create an Object of the Basic_Arithmetic Class
arithmetic = Basic_Arithmetic()
# output the result
print(arithmetic.add(5,5))
Save your file and run it. This should be the output:
>>> 10
Phew! We did it! It worked! We got the same result as the add()
function.
Okay...
Now, we have created an add()
function and a .add()
method. Can you spot the differences between them?
Create a list of the differences you observed and compare it with the differences I observed which is listed below:
- We cannot create a Method outside a Class but a Function does not need a Class to be created.
This means that methods belong to a specific class and can only be used by Objects of the Class. Functions belong to no Class or Object and can be used independently.
- To call a Method, we have to call an Object of the Class the method belongs to followed by a dot (.) before calling the method.
This is why when referring to methods, we usually precede it with a
.
e.g.add()
,.append()
(a method belonging to a List),.split()
(also belongs to a List), etc - A Method requires
self
as its first argument i.edef add(self, a, b)
but a function does not.
In other not to make this article too lengthy, please find at the bottom section of this article, a tabulated difference between methods and functions.
Also find this and this as good references to gain more knowledge about methods and functions.
Doing computations with Classes is actually more preferred than writing individual functions. This is because, with Classes, we write can a well-structured code.
Using the
Basic_Arithmetic
Class, for example, we can create an Object with multiple capabilities. We can add, subtract, multiply, and divide, all with one Object.Complete the code of the
.subtract()
,.multiply()
and.divide()
methods in theBasic_Arithmetic
Class to improve its capabilities. It is pretty easy to do.Then test it out using the
arithmetic
object.
That's it. We are done.
Thank you for your time.
If you noticed any similarities between methods and functions, you can mention them in the comment section below. I'll do well to give you feedback