Python Functions, Python Function Call, Parameters in Python, Types of functions in Python.

Join the best data science courses

In this blog, I am going to discuss Functions in python. Defining a Function in any programming language is good as this will give you code reusability. Functions are a block of code where you grouped related instruction and give a name to this Instruction. Let me give you a quick example say, you want to find the area of a circle.

Note: Please read my previous python blogs before starting the function and I am assuming that you are already familiar with loop and if-else statements in python.

# Code Before Functions
pie=3.14
r=float(input("enter radius"))
area=pie*r**2
print("area is=",area)

You can see in the above code we have 4 line of code and these lines or instructions are not grouped in any block or even these lines have not any name so now let us do the same task by using functions.

Parameters in Python

def Calculate(pie,r):
   area=pie*r**2
   print("area is=",area)

def is a keyword used to create a function in python and Calculate is the function name having 2 lines or instructions, so now these 2 lines are part of Calculate function and have two parameters pie and r.

Key-Point: Please Note this is just a function definition this code will not execute so you need to call a function whenever you want to execute its code. using functions in python, you can use the same functionality again and again.

#function calling
pie=3.14
r=float(input("enter radius"))
Calculate(pie,r)

So at the time of function call, I am passing 2 values inside the function parameter that is 3.14 and r value. Now, these values will be received by Calculate function and then the function will perform its task accordingly.

Q-Create a function in python to find out even and odd numbers inside a list.

def evenodd(li):
   for item in li:
       if item%2==0:
          print(item," is even")
       else:
          print(item," is odd")

def is Keyword to create a function and evenodd is a function name that has one parameter “li” so if you call this function, this li parameter will receive a list as explained below.

Python Function Call

li=[1,2,3,4,5,6,7,81,22,3,3,4,4]
evenodd(li)

Creating a list with a couple of values and sending this list into function parameter and function will extract values one by one from this list to check whether a number is even or odd.

Q- Create a function in python to add two List elements and both list Lengths should be the same.

def addList(l1,l2):
    l3=[]
    for i in range(0,len(l1)):
        l3.append(l1[i]+l2[i])
    print(l3)

Python Function Call

l1=[1,2,3,4]
l2=[4,3,2,1]
addList(l1,l2)

Ans:[5,5,5,5]

Q- Create a function in python to Swap two variables.

def swap(var1,var2):
      print("before swap var1=",var1," var2=",var2)
      var1,var2=var2,var1
      print("after swap var1=",var1," var2=",var2)

Python Function Call

var1=21
var2=19
swap(var1,var2)

Types of functions in Python

In the above examples we have seen python Function Call and Parameters in Python now let us move to the 2nd type of function that is “python functions” with the return type. In my previous examples functions are not returning any value now we are going to create a function that will return a value and we have to receive value for further use so let us take a quick demo.

Q- Create a function in python to add two list elements and return a final list.?

# Python Function Return Type
def addList(list1,list2):
      list3=[]
      for i in range(0,len(list1):
           list3.append(list1[i]+list2[i])
      return list3
       

Python Function Call

list1=[1,2,3,4]
list2=[4,3,2,1]
finalList=addList(list1,list2)
print(finalList)

Function Default arguments.

Default arguments Functions take a default value if no argument value is passed when you call the function. let us take a quick demo.

def Logic(a,b = 2):
  return a**b
  
# Call Logic with a parameter
Logic(a=3)

# Call Logic with a and b parameters
Logic(a=2, b=2)
Basics of Python
Basics of Python Programming. Python is an interpreted, interactive, and object-oriented and …
Python Loop Control Structure
Data Science Online Course Python Loop Control Structure plays a major role …

Jashika Bhatt

Jashika is a professional blogger, self-published author and speaker at techbook and passionate about digital marketing.

Leave a Reply

Your email address will not be published. Required fields are marked *