FUNCTIONS IN PYTHON

HINA SAYYAD
4 min readJun 13, 2021

--

USER DEFINED FUNCTIONS(PART-1)

After getting to know how a python function looks like, let us now understand why functions by considering few examples.

Let us consider two operations of adding two numbers and dividing two numbers as shown below.

a = 10 b = 20 c = a+b print(c)

d = 100 e = 10 f=d/e print(f)

Above lines of codes will perform addition and division of two numbers, but if in case you want to perform addition of two numbers again, the only option is to copy the same lines of code and use them again as shown below:

a = 10, b = 20, c = a+b .print(c)

d = 100, e = 10, f=d/e. print(f)

But as a programmer this is not the right approach which one should follow, the efficient way of doing this is by following dry approach which stands for DO-NOT-REPEAT-YOURSELF

So, the solution is including the lines of code inside a block and give it a suitable name. That way you can use the same block of code multiple times by making use of name given to it.

Using the above approach, we can achieve:

Reusability refers to resuing the same block of codes multiple times.

Modularity refers to dividing the entire code into different logical blocks of code.

From object orientation perspective, the behaviour of an object is taken care by functions in python.

Types of Functions in python

Python consists of four types of functions:

1. User Defined Functions

2. Built-in-functions.

3. Lambda functions.

4. Recursive Functions.

User Defined Functions: These functions are defined or created by user.

Built-in-functions: These functions are already defined in Python libraries and we can call them directly.

Lambda functions: A lambda function can take any number of arguments, but can only have one expression.

Recursive Functions: A Python function which is defined to call itself during its execution is known as python recursive function.

User defined Functions :

  1. Function that takes no input and does not return any output.

2.Function that takes input and does not return any output.

Below is the code which consists of a function mul() which falls in the second category of user defined functions which is a function that takes input and returns no output.

While calling a method that requires arguments, one must pass number of arguments accepted by the methods as shown below where we are passing 10,20 while calling mul(10,20). 10 and 20 are now stored inside x and y.

3. Function that takes no input but returns output.

Below is the code which consists of a function mul() which falls in the third category of user defined functions which is a function that takes no input and returns output.One must make use of return statement inside the function to return a value.Once return statement gets executed,control goes to the statement which had called that function.

Now it is the choice of the caller to collect the value returned by function or not. In the below code the returned value is collected inside res which is created inside the stack frame of main() as shown in figure below.

4.Function that takes input and returns output.

Below is the code which consists of a function mul() which falls in the fourth category of user defined functions which is a function that takes two inputs x,y and returns output.Based on situations, we must make use of different functions.

For example, you need your bank account details and to do so you must give your details as input upon which your details will be returned to you.

This way, based on different scenarios, different types of functions should be used to achieve the required task.

QUIZ FOR YOU :

THANKS FOR READING :)

--

--

HINA SAYYAD

A vibrant mind with great personality.Always opine enigmatic thought about everything. Like to write about technology and lifestyle.