Python Chapter 4 Functions - User Defined Fun
Python Chapter 4 Functions - User Defined Fun
Class: TYCM-I
Mrs. M. P. Bhosale 1
Subject: Programming with Python (PWP-22616)
Class: TYCM-I
Mrs. M. P. Bhosale 2
4.2 User defined functions:
Syntax of Function:
def function_name(parameters):
‘‘‘docstring’’’
statement(s)
return [Expression]
Optional documentation string (docstring) to
describe what the function does.
Return statement is also optional and a return
statement with no arguments is the same as return
None.
Mrs. M. P. Bhosale 5
4.2 User defined functions:
Function Calling:
Once we have defined a function, we can call it from another
function, program or even the Python prompt.
To call a function we simply type the function name with
appropriate parameters.
E.g. Sample Function:
#Function Definition
def show():
print("Hello All")
#Function Calling
show()
Mrs. M. P. Bhosale 6
4.2 User defined functions:
Parameterized Function:
Information can be passed to functions as
parameter.
Parameters are specified after the function name,
inside the parentheses.
You can add as many parameters as you want, just
separate them with a comma.
Mrs. M. P. Bhosale 7
4.2 User defined functions:
Mrs. M. P. Bhosale 8
Parameterized functions:
Function Arguments
You can call a function by using the following types of
formal arguments −
Required arguments
Keyword arguments
Default arguments
Variable-length arguments
Mrs. M. P. Bhosale 9
Parameterized functions:
Required arguments
arguments passed to a function in correct positional order.
Here, the number of arguments in the function call should
match exactly with the function definition.
def add(n1,n2):
return n1+n2
add(23)
O/P:
add(23)
TypeError: add() missing 1 required positional
argument: 'n2'
Mrs. M. P. Bhosale 10
Parameterized functions:
Keyword arguments
Keyword arguments are related to the function calls. When you
use keyword arguments in a function call, the caller identifies
the arguments by the parameter name.
This allows you to skip arguments or place them out of order
because the Python interpreter is able to use the keywords
provided to match the values with parameters.
def printinfo( name, age ):
print(name, ":", age)
printinfo(age=36,name="KKWP").
O/P:
KKWP : 36
Mrs. M. P. Bhosale 11
Parameterized functions:
Default arguments
A default argument is an argument that assumes a default value
if a value is not provided in the function call for that argument.
def add(n1,n2=23): def add(n1=23,n2):
print(n1+n2) print(n1+n2)
add(85) add(85)
O/P:
108 O/P:
def add(n1=23,n2):
SyntaxError: non-default argument
follows default argument
Mrs. M. P. Bhosale 12
Parameterized functions:
Variable-length arguments
You may need to process a function for more arguments than you
specified while defining the function.
These arguments are called variable-length arguments and are not
named in the function definition, unlike required and default
arguments
def functionname([formal_args,] *var_args_tuple ):
"function_docstring"
function_suite
return [expression]
An asterisk (*) is placed before the variable name that holds the values of
all nonkeyword variable arguments. This tuple remains empty if no
additional arguments are specified during the function call
Mrs. M. P. Bhosale 13
Parameterized functions:
Variable-length arguments: Example
def printlist(*arg2):
print(arg2)
printlist()
printlist(47)
printlist(10,20,30,40)
O/P:
()
(47,)
(10, 20, 30, 40)
Mrs. M. P. Bhosale 14
Return Statement:
The statement return [expression] exits a
function, optionally passing back an
expression to the caller. A return statement
with no arguments is the same as return
None.
def add(n1,n2):
return n1+n2
sum=add(23,45)
print("Addition is:",sum)
O/P:
Addition is: 68
Mrs. M. P. Bhosale 15
Scope of Variables:
All variables in a program may not be accessible at
all locations in that program.
This depends on where you have declared a
variable.
The scope of a variable determines the portion of
the program where you can access a particular
identifier.
There are two basic scope of variables in Python −
Global variables
Local variables
Mrs. M. P. Bhosale 16
Global vs. Local variables:
Variables that are defined inside a function body
have a local scope, and those defined outside have a
global scope.
This means that local variables can be accessed only
inside the function in which they are declared,
whereas global variables can be accessed
throughout the program body by all functions.
When you call a function, the variables declared
inside it are brought into scope.
Mrs. M. P. Bhosale 17
Global vs. Local variables:
sum=0
def add(n1,n2):
sum=n1+n2
print("Inside",sum)
add(23,45)
print("Addition is:",sum)
O/P:
Inside 68
Addition is: 0
Mrs. M. P. Bhosale 18
Anonymous Functions:
These functions are called anonymous because they are not
declared in the standard manner by using the def keyword.
You can use the lambda keyword to create small anonymous
functions.
Lambda forms can take any number of arguments but return
just one value in the form of an expression.
They cannot contain commands or multiple expressions.
An anonymous function cannot be a direct call to print
because lambda requires an expression
Lambda functions have their own local namespace and
cannot access variables other than those in their parameter
list and those in the global namespace.
Mrs. M. P. Bhosale 19
Anonymous Functions:
Syntax
lambda [arg1 [,arg2,.....,argn] ] :expression
Example:
cube=lambda no: no**3
print("cube of is:",cube(5))
O/P:
cube of is: 125
Mrs. M. P. Bhosale 20