Unit 2 Chapter 1 Functions
Unit 2 Chapter 1 Functions
(23EN1202)
CHAPTER 2- FUNCTIONS
Function
Function are subprograms which are used to compute a value or perform a
task.
Type of Functions:-
• Built-in Function
Ex: - print( ), upper( ) etc
• User-defined Function
Chapter 2
Advantage of Function
• Write once and use it as many time as you need. This provides code
reusability.
• Function facilitates ease of code maintenance.
• Divide Large task into many small task so it will help you to debug code
• You can remove or add new feature to a function anytime.
Function Definition
We can define a function using def keyword followed by function name with parentheses.
This is also called as Creating a Function, Writing a Function, Defining a Function.
Syntax : -
def Function_name ( ):
Local Variable
block of statement Function Body
return (variable or expression)
Syntax : -
def Function_name (para1, para2, …):
Local Variable
block of statement Function Body
return (variable or expression)
Note – Need to maintain proper indentation
parentheses describes this is a
def represents starting function not variable or other object
of function definition
: describes beginning
of function body
def add () :
x = 10
Local Variable
Name of Function y = 20
Function Body
c=x+y
Statements
print(c)
parentheses describes this is a
def represents starting function not variable or other object
of function definition
Parameter : describes beginning
of function body
def add (y) :
x = 10 Local Variable
Name of Function c=x+y Function Body
Statements
print(c)
Calling a function
A Function runs only when we call it, function can not run on its own.
Syntax:-
function_name ( )
function_name (arg1, arg2, …)
Ex: -
a = 10
add ( ) add(a)
add (20)
add(10.56)
item(“DSU”)
How Function Work
def add () : def add (y) :
x = 10 x = 10
y = 20 c=x+y
c=x+y print(c)
print(c) add(20)
add()
The parameter y do not know which type of value they are about to receive till the value
is passed at the time of calling the function. It means the type of data is determined only
during runtime not at compile time this is called Dynamic Typing.
Return Statement
Return statements can be used to return something from the function. In
Python, it is possible to return one or more variables/values.
Syntax : -
return (variable or expression);
Ex: - def add_sub (y) :
def add (y) : def add (y) :
return 50 x = 10 x = 10 x = 10
return (50) c=x+y return x + y c=x+y
return (x + y) return c sum = add (20) d=y-x
sum = add (20) print(sum) return c, d
return (y)
print(sum) sum, sub = add (20)
return (2, 4) print(sum)
return (x, y) print(sub)
Nested Function
When we define one function inside another function, it is known as Nested
Function or Function Nesting.
Ex:-
def disp():
def show():
print(“Show Function”)
print(“Disp Function”)
show()
disp( )
Pass a Function as Parameter
We can pass a function as parameter to another function.
Ex:-
def disp(sh):
print("Disp Function" + sh())
def show():
return " Show Function"
disp(show)
Function return another Function
A function can return another function.
Ex:-
def disp(sh):
def disp():
print("Disp Function")
def show():
return sh
return "Show Function"
print("Disp Function")
def show():
return show
return "Show Function"
r_sh = disp()
r_sh = disp(show)
print(r_sh())
print(r_sh())
Actual and Formal Argument
• Formal Argument - Function definition parameters are called as formal
arguments
• Actual Argument - Function call arguments are actual arguments
Formal Arguments
add(10, 20)
Actual Arguments
Type of Actual Arguments
• Positional Arguments
• Keyword Arguments
• Default Arguments
• Variable Length Arguments
• Keyword Variable Length Arguments
Positional Arguments
These arguments are passed to the function in correct positional order.
The number of arguments and their positions in the function definition should
be equal to the number and position of the argument in the function call.
show(age=62, name=“Geek”)
Default Arguments
Sometime we mention default value to the formal argument in function
definition and we may not required to provide actual argument, In this case
default argument will be used by formal argument.
If we do not provide actual argument for formal argument explicitly while
calling the function then formal argument will use default value on the other
hand if we provide actual argument then it will use provided value
add(5, 2, 4) add(5, 2, 4)
Keyword Variable Length Arguments
Keyword Variable length argument is an argument that can accept any number of
values provided in the form of key-value pair.
The keyword variable length argument is written with ** symbol.
It stores all the value in a dictionary in the form of key-value pair.
show()
print(“x:”, x) Using Local Variable outside function, it will show error
When we create a new object inside function then it will not be available outside function
Recursion
A function calling itself again and again to compute a value is referred to
Recursive Function or Recursion.
•A complicated function can be split down into smaller sub-problems utilizing recursion.
•Sequence creation is simpler through recursion than utilizing any nested iteration.
•Recursive functions render the code look simple and effective.
Disadvantages of using recursion
•A lot of memory and time is taken through recursive calls which makes it expensive
for use.
•Recursive functions are challenging to debug.
•The reasoning behind recursion can sometimes be tough to think through.
Factorial of a Number
GCD of 2 no’s
EXERCISE
• Print multiplication table of 12 using recursion.
• Write a function to calculate area and perimeter of a
rectangle.
• Write a function to calculate area and circumference of a
circle.
• Write a function to calculate power of a number raised to
other.
• Write a Python function to find the maximum of three
numbers.
• Write a Python program to convert degrees to radians.
Expression vs Statement
Expression/Expression Statements
Expression statements are used to compute and write a value, or to call a
procedure.
Ex:- Operators like Addition, Subtraction, Function Call etc
Statement
Statements on the other hand, are everything that can make up a line or several
lines of Python code. Expressions are also statements.
Ex:- if statement, assignment statement, loop
Anonymous Function or Lambdas
A function without a name is called as Anonymous Function. It is also known
as Lambda Function.
Anonymous Function are not defined using def keyword rather they are
defined using lambda keyword.
Syntax:-
lambda argument_list : expression
Ex:-
lambda x : print(x)
lambda x, y : x + y
Creating a Lambda Function
Syntax:-
lambda argument_list : expression
Ex:- : represents beginning of the function
Argument List Expression
lambda x, y : x + y
add = lambda x, y : x + y
add(5, 2)
Anonymous Function or Lambdas
• Lambda Function doesn’t have any Name
Ex:- lambda x : print(x)
• Lambda function can take zero or any number of argument but contains only one expression
Ex:- lambda x, y : x + y
def show(a):
print(a(8))
show(lambda x: x)
Returning lambda Function
We can return a lambda function from function.
def add():
y = 20
return (lambda x : x+y)
a =add()
print(a(10))
Immediately Invoked Function Expressions (IIFE)
sum = lambda x : x + 1
sum(5)
(lambda x : x + 1)(5)
add = lambda x, y : x + y
add(5, 2)
(lambda x, y : x + y)(5, 2)
Function Decorator
A Decorator function is a function that accepts a function as parameter and
returns a function.
A decorator takes the result of a function, modifies the result and returns it.
In Decorators, functions are taken as the argument into another function and
then called inside the wrapper function.
We use @function_name to specify a decorator to be applied on another
function.