# Functions in Python

Function is similar to a program that consists of a group of statements that are intended to perform a specific task. The main purpose of a function is to perform a specific task or work. Thus when there are several tasks to be performed, the programmer will write several functions. There are several 'built-in' functions in Python to perform various tasks. For example, to display output, Python has print() function. Similarly, to calculate square root value, there is sqrt() function. Similar to these functions, a programmer can also create his own functions which are called **'user-defined' **
functions.

- Functions are important in programming because they are used to process data, make calculations or perform any task which is required in the software development.
- Once a function is written, it can be reused as and when required. So functions are also called reusable code. Because of this reusability, the programmer can avoid code redundancy. It means it is possible to avoid writing the same code again and again.
- When there is an error in the software, the corresponding function can be modified without disturbing the other functions in the software. Thus code debugging will become easy.
- The use of functions in a program will reduce the length of the program.

## Difference between a Function and a Method

We have discussed that a function contains a group of statements and performs a specific task. A function can be written individually in a Python program. **A function is called using its name**
. When a **function is written inside a class, it becomes a 'method'**
. A method is called using one of the following ways:

**objectname.methodname() **

**Classname.methodname()**

So, please remember that a function and a method are same except their placement and the way they are called.

We have discussed that a function contains a group of statements and performs a specific task. A function can be written individually in a Python program. **A function is called using its name**
. When a **function is written inside a class, it becomes a 'method'**
. A method is called using one of the following ways:

**objectname.methodname() **

**Classname.methodname()**

So, please remember that a function and a method are same except their placement and the way they are called.

## Defining a Function

We can define a function using the keyword **def**
followed by function name. After the function name, we should write **parentheses () **
which may contain parameters. Consider the syntax of function, as shown below

we can write a function to add two values as:

**def sum(a, b):**

Here, '**def**
' represents the starting of function definition. 'sum' is the name of the function. After this name, **parentheses () **
are compulsory as they denote that it is a function and not a variable or something else. In the parentheses, we wrote two **variables 'a' and 'b'**
. These variables are called '**parameters**
'. A parameter is a variable that receives data from outside into a function. So, this function can receive two values from outside and those values are stored in the variables 'a' and 'b'.

After parentheses, we put a **colon (:) **
that represents the beginning of the function body. The function body contains a group of statements called 'suite'.

## Calling a Function

A function cannot run on its own. It runs only when we call it. So, the next step is to call the function using its name. While calling the function, we should pass the necessary values to the function in the parentheses as:

**sum(10, 15)**

Here, we are calling the 'sum' function and passing two values 10 and 15 to that function. When this statement is executed, the Python interpreter jumps to the function definition and copies the values 10 and 15 into the parameters 'a' and 'b' respectively.

A function that accepts two values and finds their sum.

def sum(a, b): c = a+b print('Sum=', c) sum(10, 15) sum(1.5, 10.75)

## Returning Results from a Function

We can return the result or output from the function using a 'return' statement in the body of the function. For example,

**return c #returns c value out of function **

**return 100 #returns 100 **

**return lst #return the list that contains values **

**return x, y, c #returns 3 values**

A Python program to find the sum of two numbers and return the result from the function.

def sum(a, b): c = a+b return c x = sum(10, 15) print('The sum is:', x) y = sum(1.5, 10.75) print('The sum is:', y)

A function to test whether a number is even or odd.

def even_odd(num): if num% 2 == 0: print(num," is even") else: print(num," is odd") even_odd(12) even_odd(13)

A Python program to calculate factorial values of numbers.

def fact(n): prod=1 while n>=1: prod*=n n-=1 return prod for i in range(1, 11): print('Factorial of {} is {}'.format(i, fact(i)))

## Returning Multiple Values from a Function

A function returns a single value in the programming languages like C or Java. But in Python, a function can return multiple values. When a function calculates multiple results and wants to return the results, we can use the return statement as:

**return a, b, c **

Here, three values which are in 'a', 'b', and 'c' are returned. These values are returned by the function as a tuple. Please remember a tuple is like a list that contains a group of elements. To grab these values, we can use three variables at the time of calling the function as:

**x, y, z = function()**

**def sum_sub(a, b): **

** c = a + b **

** d = a - b **

** return c, d**

A function that returns the results of addition, subtraction, multiplication and division.

def sum_sub_mul_div(a, b): c = a + b d = a - b e = a * b f = a/ b return c, d, e, f t = sum_sub_mul_div(10, 5) print('The results are:') for i in t: print(i, end='\t')

A function that returns the results of (a+b)2, (a-b)2, (a+b)3, (a-b)3.

def calc(a,b): w=(a+b)**2 x=(a-b)**2 y=(a+b)**3 z=(a-b)**3 return w,x,y,z a=int(input("Enter value of 'a' : ")) b=int(input("Enter value of 'b' : ")) w,x,y,z=calc(a,b) print("(a+b)^2 = :",w) print("(a+b)^3 = :",y) print("(a-b)^2 = :",x) print("(a-b)^3 = :",z)