Read Python Interview Questions and Answers for Beginners and Experienced

Python Function with examples | How to create Python function

Introduction to Python Function

In this post, we will study about Python functions. how to use Python function and  How they are declared? Before proceeding with Python function, first understand what is function?

 

 

What is Function?

In nutshell, Function is block of statements group together to perform some task, now question is why to use functions? Answer is functions are used to perform some particular task , main reason to create and use the function is reduce code duplicate, means write once use anywhere.  functions are written once in program and we can use them as many time as we want into the program just by calling function name. e.g. math function max(), min() etc. are example of functions, which are used to find the maximum and minimum values.

 

Python function

Python programming language also give flexibility to create and use functions. below we will see How to define Python function, how to declare Python function and how to call Python function

 

Types of Python functions

There are two type of Python function:

Python in-built function

Python user define function

 

What is Python in-built function

Python in-built functions are those functions which are by default provided by Python library. There are so many python in-built functions, we will discuss later in this tutorial of python function.

What is Python user define function

Python user defined functions are those functions which are not provided by Python, user create itself in the python programming language for the ease of code. below we will discuss user defined Python functions.

 

Syntax of user defined Python function

 def function_name(parameters)  :

 statements_of_function

 return value

 

Observations

  • def keyword is used to create a python function means its starting point of function
  • function_name is name of the function.
  • parameters are arguments which are passed into the function.
  • statements_of_function are statements of functions which are executed inside function to perform task.
  • return value is used to returning any value from function, its up to the function need whether to return the value or not.

 

How to create a python function

 

let’s understand Python function  by taking example


#creating a function
def greetings(name) :

# body of function
print ("hi",name)

#calling a function
greetings("Dummy")

 

Output

hi Dummy

 

Explanation:

  • Python function is created by using def keyword in Python.
  • def keyword indicates start of the function (here greetings is function name) with round brackets ().
  • After round brackets () , we will put colon (:) which indicate the start of function.
  • After colon (:) body of function (lines of statements which is to be execute inside function).
  • Python functions can be called anywhere in program by using function name. in above example greetings() is called to execute python function.

 

How to call a Python function

Python function is called by function name with round bracket.

function_name(parameters)

e.g. In above example function greeting(name) is called by simply

greeting("Joey")

Passing arguments in Python function

In Python function, we can pass arguments in functions.

 

 

Syntax of passing arguments in python function

def function_name(arg 1,arg 2...arg n) :

list of statements

arg 1,arg 2…arg n are list of arguments passed into the python function.

 

Example

let’s understand by taking one example


#creating a function
def greetings(name,age,) :

# body of function
print ("hello",name)
print("you are",age,"year old.")

#calling a function
greetings("Dummy",27)

 

output

hello Dummy
you are 27 year old.

 

Explanation: in above example, name and age are two parameters or arguments passed into the function.

return keyword in Python function

return keyword can be used in python function. There are couple of use of return keyword in Python function.

 

return keyword is used  to exit the program

Let’s understand it by taking example


#creating a function
def checkValue(number1,number2) :

# body of function

if(number2==0):
print("divide by zero")
return
else :
print("division of two numbers is: ",number1/number2)

#calling a function
checkValue(10,0)
print("************")
checkValue(10,2)

 

Output

 

divide by zero
************
division of two numbers is: 5.0

 

Explanation:

  • if number2 is 0, it will throw divide by zero exception, so we are checking and returning from the function.
  • if number2 is non zero, then it will divide with number1.

 

return the value from function

we can also return some value from Python function. Let’s understand it by taking example:


#creating a function
def sum(number1,number2) :

# body of function
return number1+number2

#calling a function
print(sum(3,27))

 

Output

30

 

Explanation:

  • Here sum is user defined function taking two parameters.
  • function sum is doing addition of two numbers and its returning the value after addition.
  • When we call the function it return sum of the two number as shown above.