Devashish Kumar
Faculty-IT
iNurture
FUNCTIONS
 A function is a block of organized, reusable code that is
used to perform a single, related action.
 Functions provide better modularity for the applications.
 Functions provide a high degree of code reusing.
RULES FOR DEFINING FUNCTION IN PYTHON
 Function blocks begin with the keyword def followed by the function name and
parentheses ( ( ) ).
 Any input parameters or arguments should be placed within these parentheses.
We also define parameters inside these parentheses.
 The code block within every function starts with a colon (:) and is indented.
 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.
Function Syntax
The keyword
def
introduces a
function
definition.
Input Parameter is placed within the
parenthesis() and also define
parameter inside the parenthesis.
Return statement exits a
function block. And we can also
use return with no argument.
The code block
within every
function starts
with a colon(:) .
PASSING ARGUMENTS TO FUNCTIONS
 In programming, there are two ways in which arguments can be passed to functions :-
 Pass by Value:
ļ‚§ Function creates a copy of the variable(Object in Python) passed to it as an argument.
ļ‚§ The actual object is not affected.
ļ‚§ Object is of immutable type,because immutable objects cannot be modified.
 Pass by Reference:
ļ‚§ The actual object is passed to the called function.
ļ‚§ All the changes made to the object inside the function affect its original value.
ļ‚§ Object is mutable type, as mutable objects can be changed, the passed objects are
updated.
EXAMPLES OF PASSING IMMUTABLE ARGUMENTS
OUTPUT: New memory address of ā€˜a’
inside the function.
Actual variable ā€˜a’ is not
changed.
EXAMPLES OF PASSING MUTABLE ARGUMENTS
Address is same
Value of my_list is
changed after
function call.
FUNCTION ARGUMENTS
DEFAULT ARGUMENT VALUES
 Default Argument- argument that assumes a default value if a value is not
provided in the function call for that argument.
 The default value is evaluated only once.
 EXAMPLE:
Output:
In this code, argument ā€˜b’
has given a default value.
ie(b=90)
When the value of ā€˜b’ is not passed
in function ,then it takes default
argument.
DEFAULT ARGUMENTS
 The default value is evaluated only once. This makes a difference when the
default is a mutable object such as a list, dictionary, or instances of most
classes.
 Example:
 if we don’t want the default value to be shared between subsequent calls,
then
 Example :
Output:
Output:
KEYWORD ARGUMENTS
 Keyword arguments are related to the function calls.
 Using keyword arguments in a function call, the caller identifies the arguments
by the parameter name.
 Allows to skip arguments or place them out of order, the Python interpreter
use the keywords provided to match the values with parameters.
 Example:
Output:
ļ‚§ Caller identifies the keyword
argument by the parameter name.
ļ‚§ Calling of the argument Order
doesn’t matter .
KEYWORD ARGUMENTS
 Example 1:
Output:
Example 2: Output:
ARBITRARY ARGUMENT LISTS
 Variable-Length Arguments: more arguments than we specified while defining
the function.
 These arguments are also called variable-length arguments .
 An asterisk (*) is placed before the variable name that holds the values of all non
keyword variable arguments.
 This tuple remains empty if no additional arguments are specified during the
function call.
 Syntax :
def function_name([formal_args], *var_args_tuple ):
statement 1……
statement 2……
return [expression]
This is called
arbitrary argument
and asterisk sign is
placed before the
variable name.
ARBITRARY ARGUMENT LISTS
 EXAMPLE 1:
Output: Output:
 EXAMPLE 2:
 Any formal parameter which
occur after the *args
parameter are keyword-only
arguments.
 If formal parameter are not
keyword argument then error
occurred.
UNPACKING ARGUMENT LISTS
 Arguments are already in a list or tuple, need to be unpacked for a function
call requiring separate positional arguments.
 For instance, the built-in range() function expects separate start and stop
arguments.
 If range() are not available separately, write the function call with the *-operator
to unpack the arguments out of a list or tuple.
 Example 1: Example 2:
Output:
In dictionary, we use ā€˜**’ for unpacking the
arguments
In tuples or lists, ā€˜*’ is
used to unpack the
arguments
LAMBDA EXPRESSIONS
 Small functions can be created with the lambda keyword also known as
Anonymous function.
 Used wherever functions object are required.
 These functions are called anonymous because they are not declared in the
standard manner by using the def keyword. .
 Syntax :
 EXAMPLE:
lambda [arg1 [,arg2,.....argn]]:expression
Output:
ļ‚§ The function returns the multiply of
two arguments.
ļ‚§ Lambda function is restricted to a
single expressions.
FUNCTION ANNOTATIONS
 Function annotations are completely optional metadata information about the
types used by user-defined functions.
 Annotations are stored in the ā€œ__annotations__ā€ attribute .
 Parameter annotations are defined by a colon after the parameter name,
followed by an expression evaluating to the value of the annotation.
 Return annotations are defined by a literal ->, followed by an expression,
between the parameter list and the colon denoting the end of the def statement.
 Example:
Output:
Literal ā€˜->’ is used to
define return
annotations.
DOCUMENTATION STRINGS
 The first line should always be a short, concise summary of the object’s
purpose.
 The first line should begin with a capital letter and end with a period.
 If there are more lines in the documentation string, the second line should be
blank.
 The first non-blank line after the first line of the string determines the amount
of indentation for the entire documentation string.
 Example:
Output:
ā€˜.doc’ is used to print the
documentation strings.
CONTRIBUTERS

Functions in python slide share

  • 1.
  • 2.
    FUNCTIONS  A functionis a block of organized, reusable code that is used to perform a single, related action.  Functions provide better modularity for the applications.  Functions provide a high degree of code reusing.
  • 3.
    RULES FOR DEFININGFUNCTION IN PYTHON  Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).  Any input parameters or arguments should be placed within these parentheses. We also define parameters inside these parentheses.  The code block within every function starts with a colon (:) and is indented.  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.
  • 4.
    Function Syntax The keyword def introducesa function definition. Input Parameter is placed within the parenthesis() and also define parameter inside the parenthesis. Return statement exits a function block. And we can also use return with no argument. The code block within every function starts with a colon(:) .
  • 6.
    PASSING ARGUMENTS TOFUNCTIONS  In programming, there are two ways in which arguments can be passed to functions :-  Pass by Value: ļ‚§ Function creates a copy of the variable(Object in Python) passed to it as an argument. ļ‚§ The actual object is not affected. ļ‚§ Object is of immutable type,because immutable objects cannot be modified.  Pass by Reference: ļ‚§ The actual object is passed to the called function. ļ‚§ All the changes made to the object inside the function affect its original value. ļ‚§ Object is mutable type, as mutable objects can be changed, the passed objects are updated.
  • 7.
    EXAMPLES OF PASSINGIMMUTABLE ARGUMENTS OUTPUT: New memory address of ā€˜a’ inside the function. Actual variable ā€˜a’ is not changed.
  • 8.
    EXAMPLES OF PASSINGMUTABLE ARGUMENTS Address is same Value of my_list is changed after function call.
  • 9.
  • 10.
    DEFAULT ARGUMENT VALUES Default Argument- argument that assumes a default value if a value is not provided in the function call for that argument.  The default value is evaluated only once.  EXAMPLE: Output: In this code, argument ā€˜b’ has given a default value. ie(b=90) When the value of ā€˜b’ is not passed in function ,then it takes default argument.
  • 11.
    DEFAULT ARGUMENTS  Thedefault value is evaluated only once. This makes a difference when the default is a mutable object such as a list, dictionary, or instances of most classes.  Example:  if we don’t want the default value to be shared between subsequent calls, then  Example : Output: Output:
  • 12.
    KEYWORD ARGUMENTS  Keywordarguments are related to the function calls.  Using keyword arguments in a function call, the caller identifies the arguments by the parameter name.  Allows to skip arguments or place them out of order, the Python interpreter use the keywords provided to match the values with parameters.  Example: Output: ļ‚§ Caller identifies the keyword argument by the parameter name. ļ‚§ Calling of the argument Order doesn’t matter .
  • 13.
    KEYWORD ARGUMENTS  Example1: Output: Example 2: Output:
  • 14.
    ARBITRARY ARGUMENT LISTS Variable-Length Arguments: more arguments than we specified while defining the function.  These arguments are also called variable-length arguments .  An asterisk (*) is placed before the variable name that holds the values of all non keyword variable arguments.  This tuple remains empty if no additional arguments are specified during the function call.  Syntax : def function_name([formal_args], *var_args_tuple ): statement 1…… statement 2…… return [expression] This is called arbitrary argument and asterisk sign is placed before the variable name.
  • 15.
    ARBITRARY ARGUMENT LISTS EXAMPLE 1: Output: Output:  EXAMPLE 2:  Any formal parameter which occur after the *args parameter are keyword-only arguments.  If formal parameter are not keyword argument then error occurred.
  • 16.
    UNPACKING ARGUMENT LISTS Arguments are already in a list or tuple, need to be unpacked for a function call requiring separate positional arguments.  For instance, the built-in range() function expects separate start and stop arguments.  If range() are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple.  Example 1: Example 2: Output: In dictionary, we use ā€˜**’ for unpacking the arguments In tuples or lists, ā€˜*’ is used to unpack the arguments
  • 17.
    LAMBDA EXPRESSIONS  Smallfunctions can be created with the lambda keyword also known as Anonymous function.  Used wherever functions object are required.  These functions are called anonymous because they are not declared in the standard manner by using the def keyword. .  Syntax :  EXAMPLE: lambda [arg1 [,arg2,.....argn]]:expression Output: ļ‚§ The function returns the multiply of two arguments. ļ‚§ Lambda function is restricted to a single expressions.
  • 18.
    FUNCTION ANNOTATIONS  Functionannotations are completely optional metadata information about the types used by user-defined functions.  Annotations are stored in the ā€œ__annotations__ā€ attribute .  Parameter annotations are defined by a colon after the parameter name, followed by an expression evaluating to the value of the annotation.  Return annotations are defined by a literal ->, followed by an expression, between the parameter list and the colon denoting the end of the def statement.  Example: Output: Literal ā€˜->’ is used to define return annotations.
  • 19.
    DOCUMENTATION STRINGS  Thefirst line should always be a short, concise summary of the object’s purpose.  The first line should begin with a capital letter and end with a period.  If there are more lines in the documentation string, the second line should be blank.  The first non-blank line after the first line of the string determines the amount of indentation for the entire documentation string.  Example: Output: ā€˜.doc’ is used to print the documentation strings.
  • 20.