Sitemap
Dipen Sherpa

Dipen Sherpa

Python Developer and web scraper exploring data extraction and automation tools

Functions in python

2 min readNov 30, 2024

Why use Function?

Function in python is a block of code which is use to perform specific tasks. When we pass something it return something. It is use for code reusability and achieve the main objective of programmer i.e. DRY(Don’t Repeat Yourself). Other uses are it’s increase the code readability, divide the programs into chunks which makes easier to maintain.

Structure of Function:

Press enter or click to view image in full size
#function use to find the cube
def find(num):
cube = num**3
print(cube)

find(3)

Explanation:

a. def: def is a keyword use to start the built the function in python

Get Dipen Sherpa’s stories in your inbox

Join Medium for free to get updates from this writer.

b. Function Name: Function name is keep to built the function. Note that function name should match with the work of the functions. Here ‘find’ is a function name.

c. Parameter: Parameter is something that you should pass the value when you use the function. Here ‘num’ is a parameter

d. statement: It is a function body where we write the logic. Here, cube = num**3 is a statement

e. return expression: It is a result of a function. Here, it returns none

Call the function: We call the function by it’s name by passing the argument i.e. find(3). Here, 3 is a argument. Here, we can use this function as many times to find the cube of it’s number

How it works?

When the function is call, then 3 is passes to the parameter i.e. ‘num’, which goes to the function body and print the result.

Hope! This simple explanation makes it easy to understand function.

--

--

Dipen Sherpa
Dipen Sherpa

Written by Dipen Sherpa

Python Developer and web scraper exploring data extraction and automation tools