In this post, you will learn what is a function in python, how to define a function, how to call a function, functions with parameters, default parameters, built-in functions, and so on.

So let’s start learning all the things one by one:

## What is a function in python?

Function is a block of code that performs a specific task when it is called and functions are re-useable means you can write a function once and use it as many times you want.

Another definition:

Function is a block of code that write once and run as many as time you want to run in the program.

## Types of function

there are two types of function and they are as follows.

1. built-in function.
2. user define function.

## 1. Built-in Function

Built-in Functions are predefined functions in python.

there are many built-in functions in python few are listed below.

## 2. User define function

A function defined by the user is called the user-define function and it performs its assigned task after calling that.

For Function two things are important.

1. Function definition.
2. Function call.

## Define a function

`def` keyword is used to define a function in python.

Example:

``````# define a function
c = a+ b
return c

#calling function``````

Output:

``8``

## Function call

Calling a function is very easy just write a function name with a set of parenthesis.

example: addtwo(3,5) (as shown in above program)

Below figure help you to understand all the terms related to function.

let’s do some programs using a function for better understanding.

## Example for Practice

### Write a Python program to check even and odd numbers by using a function.

Source code:

``````def odd_even(num):
if num % 2 == 0:
return 'Even'
else:
return 'Odd'

print(odd_even(3))
print(odd_even(4))
print(odd_even(6))
print(odd_even(5))
print(odd_even(7))``````

Output:

``````Odd
Even
Even
Odd
Odd``````

### Write a python program to find the greatest number from three numbers

Source code:

``````def greatest(a,b,c):
if a>b and a>c:
return a
elif b>c:
return b
else:
return c

print(f"{greatest(45,35,53)} is greatest")``````

Output:

``53 is greatest``

### Write a python program to check for palindrome.

Source code:

``````# check your name is same in reverse order or not

def name_check(n):
if n == n[::-1]:
return True
else:
return False
name = 'Saurabh'
name2 = 'naman'
print(f"{name} is palindrome :{name_check(name)}")
print(f"{name2} is palindrome : {name_check(name2)}")``````

Output:

``````Saurabh is palindrome :False
naman is palindrome : True``````

## Call a function inside a function

Source code:

``````def greater(a,b):
if a>b:
return a
else:
return b

def greatest(a,b,c):
bigger = greater(a,b) # call greater function
return greater(bigger,c)

num1 = 75
num2 = 85
num3 = 35
print(f"{greatest(num1,num2,num3)} is greatest")``````

Output:

``85 is greater``

## Default parameters

Below figure help you to understand, what are default parameters.

if values are not passed to default parameters then default values are printed and if values pass to default parameters then default values replace with passed values (or passed argument).

Example:

``````def default(name = 'unknown',surname = 'unknown',age = None):
print(f"your name is {name} ")
print(f"your surname is {surname} ")
print(f"your age is {age} ")

print('calling function with no arguments')
default()

# all default values printed because we are not pass any arguments
``````

Output:

``````calling function with no arguments
your name is unknown
your surname is unknown
your age is None``````

When arguments pass in function then default parameters replace with passed arguments.

Example:

``````def default(name = 'unknown',surname = 'unknown',age = None):
print(name)
print(surname)
print(age)

default('saurabh','tiwari')

#default values replace with passed arguments``````

Output:

``````saurabh
tiwari
None``````

Here default values of `name` & `surname` variables replace with passed arguments.

NOTE: Always make the last parameter as a default parameter otherwise you will get an error (this thing is valid In case of using only default parameter and normal parameter at the same time).

Hope this post adds some value to your life -thank you.

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.