SlideShare a Scribd company logo
Python Functions
● Define functions
● Passing arguments to Function
● Return a value from function
● Scope of Objects
● Default arguments
● Positional and keyword arguments
● Variable length arguments
Functions
 Piece of reusable code
 Solves particular task
 Call function instead of writing code
yourself
Built-in Functions
Syntax of Function
Mohammed Sikander 4
Defining a function
Function Call
 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.
Calling the Function
my_function()
Square function:Take one
arguments and prints its square
Square function:Take one
arguments and returns its square
Function returning multiple
value
Scope and Lifetime of
variables
 Scope of a variable is the portion of a program where the
variable is recognized.
 Parameters and variables defined inside a function is not visible
from outside. Hence, they have a local scope.
 Lifetime of a variable is the period throughout which the variable
exits in the memory. The lifetime of variables inside a function is
as long as the function executes.
 They are destroyed once we return from the function. Hence, a
function does not remember the value of a variable from its
previous calls.
Python   Functions
Python   Functions
Python   Functions
Default Arguments
 Function arguments can have default values in
Python.
 We can provide a default value to an argument by
using the assignment operator (=).
Default Arguments
 In this function, the
parameter amount does not have a default
value and is required (mandatory) during a
call.
 On the other hand, the parameter
discountPercentage has a default value
of 0. So, it is optional during a call.
 If a value is provided, it will overwrite the
default value.
 Any number of arguments in a function can
have a default value.
Default Arguments
 Once we have a default argument, all
the arguments to its right must also
have default values.
 SyntaxError: non-default argument
follows default argument
Keyword Arguments
 Positional argument cannot follow
keyword argument
Variable number of arguments
Functions as Objects
● Although functions are created differently from normal
variables, functions are just like any other kind of value.
● They can be assigned and reassigned to variables, and later
referenced by those names.
Python   Functions

More Related Content

Python Functions

  • 1. Python Functions ● Define functions ● Passing arguments to Function ● Return a value from function ● Scope of Objects ● Default arguments ● Positional and keyword arguments ● Variable length arguments
  • 2. Functions  Piece of reusable code  Solves particular task  Call function instead of writing code yourself
  • 6. Function Call  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. Calling the Function my_function()
  • 7. Square function:Take one arguments and prints its square
  • 8. Square function:Take one arguments and returns its square
  • 10. Scope and Lifetime of variables  Scope of a variable is the portion of a program where the variable is recognized.  Parameters and variables defined inside a function is not visible from outside. Hence, they have a local scope.  Lifetime of a variable is the period throughout which the variable exits in the memory. The lifetime of variables inside a function is as long as the function executes.  They are destroyed once we return from the function. Hence, a function does not remember the value of a variable from its previous calls.
  • 14. Default Arguments  Function arguments can have default values in Python.  We can provide a default value to an argument by using the assignment operator (=).
  • 15. Default Arguments  In this function, the parameter amount does not have a default value and is required (mandatory) during a call.  On the other hand, the parameter discountPercentage has a default value of 0. So, it is optional during a call.  If a value is provided, it will overwrite the default value.  Any number of arguments in a function can have a default value.
  • 16. Default Arguments  Once we have a default argument, all the arguments to its right must also have default values.  SyntaxError: non-default argument follows default argument
  • 18.  Positional argument cannot follow keyword argument
  • 19. Variable number of arguments
  • 20. Functions as Objects ● Although functions are created differently from normal variables, functions are just like any other kind of value. ● They can be assigned and reassigned to variables, and later referenced by those names.