Python Function
Python Function
BY SUCHITA MISHRA
PGT(COMP. SC.)
FUNCTIONS IN PYTHON
• Function is a collection of statements which is made to perform a specific task.
• To execute function, we have to call it in the program.
• Instead of writing large program, we can write small functions as a specific part of
program to accomplish the task.
• Once written, a function can also be used in other programs as library function.
• Functions can be categorised into three types:
1. Built-in Functions
2. Functions defined in Modules
3. User defined Functions
BUILT-IN FUNCTIONS
• These are the function which are predefined in python, we have to just
call them to use.
• Python has many built-in functions which make programming easy,
fast and efficient.
• They always reside in Standard Library and we need not to import any
module to use them.
• Ex: len(), max(), input(), type() etc.
FUNCTIONS DEFINED IN MODULES
• Module is a .py file which contains the definitions of functions and variables.
• These functions are predefined in particular Modules and can only be used
when the corresponding module is imported.
• Python provides two ways to import a module:
1. import statement: To import full module
EX: import math
a=20
print(math.sqrt(a))
2. from statement: To import all or selected functions from the module.
EX: from math import sqrt
a=20
print(sqrt(a))
USER DEFINED FUNCTIONS
• These are the functions made by the user as per the requirement.
• Function is a collection of statements which are written for a specific task.
• We can use them in any part of the program by calling them.
• def keyword is used to make user defined functions.
• SYNTAX:
def function_name(list of parameters):
keyword statement(s)
After the line containing def there
should be a 4 spaces indentation, which is
also known as body or block of the function.
USER DEFINED FUNCTIONS WITHOUT
ARGUMENT AND WITHOUT RETURN
Function Definition
Function Call
Output
USER DEFINED FUNCTIONS WITHOUT
ARGUMENT AND WITH RETURN
Output
USER DEFINED FUNCTIONS WITH
ARGUMENT AND WITH RETURN
Output
USER DEFINED FUNCTION WITH
MULTIPLE RETURN VALUES
* In Python a function may
return multiple values.
*In multiple returning it returns
a sequence of values to the
calling statement.
*These returned values may be
used as per the requirement.
USER DEFINED FUNCTION WITH
MULTIPLE RETURN VALUES
OUTPUT
POINTS TO REMEMBER….
➢ The return statement ends the function execution even if it is in the
middle of the function . The statements written after return statement
won’t get executed.
➢ If a function does not return any value, then by default it returns
None.
ARGUMENTS AND PARAMETERS
ARGUMENTS :
▪ Values being passed are called Arguments.
▪ Arguments appear in function call statement.
▪ Arguments are also known as Actual arguments or Actual parameters.
PARAMETERS :
▪ Values being received are called Parameters.
▪ Parameters appear in function header or in function definition.
▪ Parameters are also known as formal parameters or formal arguments.
ARGUMENTS AND PARAMETERS
Parameters/ Formal
Parameters
Arguments/ Actual
Parameters
TYPES OF ARGUMENTS
DEFAULT ARGUMENTS
1 4 5 6 1 2 3 6 7
FLOW OF EXECUTION
(EXAMPLE-2)
1 4 5 6 1 2 7
FLOW OF EXECUTION
(EXAMPLE-3)
1 4 7 8 4 5 1 2 3 5 6 8 1 2 3 8 9
SCOPE OF A VARIABLE
➢ Scope of a variable is the portion of a program where the variable is
recognized.
OR
➢ Scope means part of a program in which a variable can be used.
SCOPE OF A VARIABLE
LOCAL SCOPE
GLOBAL SCOPE
LOCAL SCOPE
➢ The variables declared inside the function definition are called Local
Variables. Here, L is a local variable.
OUTPUT:
LOCAL Variable
Here, L is a local variable. Local variable is not
accessible
outside the
function.
OUTPUT:
ERROR
GLOBAL SCOPE
➢ The variables declared outside the function definition are called Global
Variables. Here, G is a Global Variable
OUTPUT:
What if, a same
variable is declared
inside as well as
outside the
function??
Preference will be
given to Local
Variable, inside the
function…
SCOPE OF A VARIABLE
OUTPUT:
SCOPE OF A VARIABLE
OUTPUT:
SCOPE OF A VARIABLE
OUTPUT:
25
LEGB RULE
L E G B
Local
variable gets
first priority.
OUTPUT:
So, if both can have
same variable
names, How can a
function change
the value of a
Enclosing variable
in a Local Scope??
Using
nonlocal
Keyword
LEGB RULE
➢ Use of nonlocal keyword:
nonlocal keyword
is used to access
the enclosing
variable inside
local scope
OUTPUT:
LEGB RULE
➢ Use of Built-in variable:
In LEGB Rule, if non of
the three variables are
present then preference
will be given to the
imported Built-in
variable.
OUTPUT: