Functions
Functions
----------------------
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 called function.The main advantage of functions is
code Reusability.
Note: In other languages functions are known as methods,procedures,subroutines etc
i) Built in Functions: The functions which are coming along with Python software
automatically, are called built in functions or pre defined functions.
Ex: id(), type(), input(), eval() etc..
ii) User Defined Functions: The functions which are developed by programmer
explicitly according to business requirements, are called user defined functions.
Syntax to Create User defined Functions:
def function_name(parameters) :
----
-----
return value
parameters: are the inputs to the function. If a function contains arguments, then
at the time of calling, compulsory we should provide values, otherwise we will get
error.
Q) Write a function to take name of the student as input and print wish message by
name.
Q) Write a function to take number as input and print its square value.
return Statement: Function can take input values as parameters and executes
business logic, and returns output to the caller with return statement.
Note:If we are not writing return statement then default return value is None.
1) def f1():
2) print("Hello")
3) f1()
4) print(f1())
Note: Returning Multiple Values from a Function: In other languages like C,C++ and
Java, function can return atmost one value. But in Python, a function can return
any number of values.
Types of Arguments:
def f1(a,b):
------
------
f1(10,20)
Keyword Arguments: We can pass argument values by keyword i.e by parameter name.
1) def wish(name,msg):
2) print("Hello",name,msg)
3) wish(name='Ram',msg="Good Morning")
4) wish(msg="Good Morning",name="Krishna")
Note: 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.
1) def wish(name,msg):
2) print("Hello",name,msg)
3) wish('Ram',"Good Morning") #Valid
4) wish('Ram',msg="Good Morning") # Valid
5) wish(name='Ram',"Good Morning") # Invalid
#SyntaxError: positional argument follows keyword argument
Default Arguments: Sometimes we can provide default values for our positional
arguments.
1) def wish(name="Guest"):
2) print("Hello",name,"Good Morning")
3) wish("Python")
4) wish()
Note: If we are not passing any name then only default value will be considered.
Case Study:
def f(arg1,arg2,arg3=4,arg4=8):
print(arg1,arg2,arg3,arg4)
f(3,2)
f(10,20,30,40)
f(25,50,arg4=100)
f(arg4=2,arg1=3,arg2=4)
f() #Invalid TypeError: f() missing 2 required positional arguments: 'arg1' and
'arg2'
f(arg3=10, arg4=20, 30, 40) #Invalid SyntaxError: positional argument follows
keyword argument [After keyword arguments we should not take positional arguments]
f(4, 5, arg2 = 6) #Invalid TypeError: f() got multiple values for argument 'arg2'
f(4, 5, arg3 = 5, arg5 = 6) #Invalid TypeError: f() got an unexpected keyword
argument 'arg5'