# PYTHON - Functions

A function is a block of statements which executes only when it is called somewhere in the program. Function provides re-usability of same code for different inputs, hence saves time and resources. There are many in-built functions in Python and one of the common function is print(). A user can create their own function which is also termed as user-defined functions.

## Create Function

In Python, a function is defined using def keyword along with function's name followed by parenthesis containing function's parameter(s), if it has any.

## Call Function

After defining the function, it can be called anywhere in the program with it's name followed by parenthesis containing function's parameter(s), if it has any.

### Syntax

#Defining function
def function_name(parameters):
statements

#Calling function
function_name(parameters)


### Example: A function with no parameter

def MyFunction():
print("Welcome to Python Programming!.")

MyFunction()


Output

Welcome to Python Programming!.


## Parameter

A parameter (or also known as argument) is a variable which is used to pass information inside a function. In above example, the function does not has any parameter. But a user can create a function with single or multiple parameters. Value of a parameter can be further used by the function to achieve desired result.

### Example: A function with a parameter

def MyFunction(name):
print("Welcome to Python Programming! " + name +".")

MyFunction("John")
MyFunction("Marry")
MyFunction("Sam")


Output

Welcome to Python Programming! John.

Welcome to Python Programming! Marry.

Welcome to Python Programming! Sam.


## Default Parameter Value

Default value can be assigned to a parameter at the time of creating function. When the function is called without parameter then it uses default value.

### Example: A function with a default value parameter

def MyFunction(name = "Dude"):
print("Welcome to Python Programming! " + name +".")

MyFunction()
MyFunction("John")


Output

Welcome to Python Programming! Dude.

Welcome to Python Programming! John.


### Example: Passing iterable in a function

def MyFunction(num):
for i in num:
if (i % 2 == 0):
print(i ," is an even number.")
else:
print(i ," is an odd number.")

MyFunction(range(1,6))


Output

1  is an odd number.
2  is an even number.
3  is an odd number.
4  is an even number.
5  is an odd number.


## Function to Return Values

A function can be used to return values. Please see example below:

### Example: A function with a default value parameter

def MyFunction(num, multiplier = 2):
x = num * multiplier
return x

print(MyFunction(5, 3))
print(MyFunction(10))
print(MyFunction(25, 4))


Output

15

20

100


## Key Parameter

Parameter values can be passed in function with key = value syntax also. In this way, the order of parameter does not matter.

### Example:

def MyFunction(num1, num2):
print(num1/num2)

MyFunction(num2 = 2, num1 = 10)


Output

5.0


## Unknown number of Parameters

If there is no information about numbers of parameters that will be passed in the function, use * in front of parameter name. Adding *, makes that parameter an iterable and allows to store multiple values which can be iterated over or accessed using index number. Please see below example.

### Example: Function to multiply unknown number of parameters

def MyFunction(*num):
FinalValue = 1;
for i in num:
FinalValue = FinalValue * i
return FinalValue

print(MyFunction(2, 3))
print(MyFunction(2, 3, 5))


Output

6

30


## Recursive function

A function which can call itself is termed as recursive function. A recursive function generally ends with one or more boundary conditions.

### Example: A function for calculating factorial using recursion method

def factorial(num):
#checking whether the parameter value is positive integer or not
if(isinstance(num, int) and num >=0):
if(num > 0):
return num*factorial(num-1)
else:
return 1
else:
return "Number should be positive Integer."

print(factorial(5))
print(factorial(2.5))
print(factorial(-3))


Output

120

Number should be positive Integer.

Number should be positive Integer.