Functions
@November 23, 2022
If a group of statements is repeatedly required then it is not recommended to write
these
statements everytime seperately.
We have to define these statements as a single unit and
we can call that unit any number of times based on our requirement without
rewriting.
This unit is nothing but function.
The main advantage of functions is code Reusability.
Note: In other languages functions are known as methods,procedures,subroutines
etc
Python supports 2 types of functions
1. Built in Functions
2. User Defined Functions
Built in Functions:
The functions which are coming along with Python software automatically,are called
built
in functions or pre defined functions
id()
type()
input()
eval()
sum()
User Defined Functions:
Functions 1
The functions which are developed by programmer explicitly according to business
requirements ,are called user defined functions.
Syntax to create user defined functions:
#-------------------FUNCTIONS ---------------------
def function_name(parameters) :
def hello(name):
print("Hello ",name)
hello("Yashwanth")
Parameters
Parameters are inputs to the function. If a function contains parameters,then at the
time
of calling,compulsory we should provide values otherwise,otherwise we will get error.
Return Statement:
Function can take input values as parameters and executes business logic, and
returns
output to the caller with return statement
Types of arguments
def f1 (a,b):
f1(10,20)
a,b are formal arguments where as 10,20 are actual arguments
There are 4 types are actual arguments are allowed in Python.
1. positional arguments
2. keyword arguments
Functions 2
3. default arguments
4. Variable length argument
positional arguments:
These are the arguments passed to function in correct positional order.
#Positional Argument
def add(a,b):
print("Sum of",a,"&",b,'is',a+b)
add(2,3)
The number of arguments and position of arguments must be matched. If we change the
order then result may be changed.
If we change the number of arguments then we will get error
2. keyword arguments:
We can pass argument values by keyword i.e by parameter name
#Keyword Arguments
def bday (name,wish):
print(year,wish,name)
bday(wish="Happy Birthday ",name="Yashwanth")
#Keyword Arguments combined with Positional Argument
def bday (year,name,wish):
print(year,wish,name)
bday("2022",wish="Happy Birthday ",name="Yashwanth")
Here the order of arguments is not important but number of arguments must be matched.
Note:
We can use both positional and keyword arguments simultaneously. But first we have to
take positional arguments and then keyword arguments,otherwise we will get
syntaxerror.
Functions 3
3. Default Arguments:
Sometimes we can provide default values for our positional arguments.
#Default Arguments
def names (name="Yahswanth"):
print(name)
names(name=123)
If we are not passing any name then only default value will be considered.
4. Variable length arguments:
Sometimes we can pass variable number of arguments to our function,such type of
arguments are called variable length arguments.
We can declare a variable length argument with * symbol as follows
def f1(*n):
We can call this function by passing any number of arguments including zero number.
Internally all these values represented in the form of tuple.
#Variable Length Arguments
def add(*n):
addition=0
for i in n :
addition=addition+i
print("Sum of the Given Numbers is :",addition)
add(1,2,3435,5654,56546,6456456,4565465)
Note:
We can mix variable length arguments with positional arguments
Note: After variable length argument,if we are taking any other arguments then we
should provide values as keyword arguments.
------------------------------
Type 2 in Variable Length Arguments
Note: We can declare key word variable length arguments also.
For this we have to use **.
def f1(**n):
Functions 4
We can call this function by passing any number of keyword arguments. Internally these
keyword arguments will be stored inside a dictionary.
def display(**kwargs):
for k,v in kwargs.items():
print(k,"=",v)
display(n1=10,n2=20,n3=30)
n1=10
n2=20
n3=30
Handson
1. Declare a function add_two_numbers. It takes two parameters and it returns a
sum.
2. Area of a circle is calculated as follows: area = π x r x r. Write a function that
calculates area_of_circle.
3. Write a function called add_all_nums which takes arbitrary number of arguments
and sums all the arguments. Check if all the list items are number types. If not
do give a reasonable feedback.
4. Temperature in °C can be converted to °F using this formula: °F = (°C x 9/5) +
32. Write a function which converts °C to °F, convert_celsius_to-fahrenheit.
5. Declare a function named print_list. It takes a list as a parameter and it prints
out each element of the list.
print(reverse_list([1, 2, 3, 4, 5]))
# [5, 4, 3, 2, 1]
print(reverse_list1(["A", "B", "C"]))
# ["C", "B", "A"]
Functions 5