0% found this document useful (0 votes)
14 views

Python Function

Python class 12 function documents

Uploaded by

sibanandsahu69
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Python Function

Python class 12 function documents

Uploaded by

sibanandsahu69
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

FUNCTIONS

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

POSITIONAL / REQUIRED ARGUMENTS

DEFAULT ARGUMENTS

KEYWORD / NAMED ARGUMENTS

VARIABLE- LENGTH ARGUMENTS


POSITIONAL/REQUIRED ARGUMENTS

➢ Positional Arguments are arguments passed to a function in correct positional


order .
➢ If we change their order , then the result will be changed.
➢ If we change the number of arguments passed, then it shall result in an error.
➢ When the functions call statement must match the number and order of arguments
as defined in the function definition, this is called positional argument matching.
POSITIONAL/REQUIRED ARGUMENTS
(EXAMPLE……)
# FIND THE SIMPLE INTEREST USING FUNCTION WITH
POSITIONAL ARGUMENTS. OUTPUT
DEFAULT ARGUMENTS
➢ Default Argument is an argument that assumes a default value if a value is
not provided in the function call for that argument.
➢ If you are not passing any value, then only default values will be considered.
➢ Non-default arguments cannot follow default arguments.
➢ In a function header, any parameter cannot have a default value unless all
parameters appearing on its right have their default values.
DEFAULT ARGUMENTS
(EXAMPLE…..)
# FIND THE SIMPLE INTEREST USING FUNCTION WITH
DEFAULT ARGUMENTS. OUTPUT
KEYWORD / NAMED ARGUMENTS

➢ Python allows us to call the function with the keyword arguments.


➢ This type of function call enables us to pass the arguments in the random
order.
➢ The name of the arguments are treated as the keywords and matched in the
function calling and definition.
➢ If the same match is found, the values of the arguments are copied in the
function definition.
KEYWORD / NAMED ARGUMENTS
(EXAMPLE…)

# FIND THE SIMPLE INTEREST USING FUNCTION WITH


KEYWORD ARGUMENTS. OUTPUT
FLOW OF EXECUTION

➢ The flow of execution refers to the order


in which statements are executed during a
program run.
FLOW OF EXECUTION
(EXAMPLE-1)

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

Inside the function


Local variable will be
accessed.

Outside the function


Global variable will be
accessed.
OUTPUT:
So, if both can have
same variable
names, How can a
function change
the value of a
Global variable in
a Local Scope??
Using
global
Keyword
SCOPE OF A VARIABLE

OUTPUT:
SCOPE OF A VARIABLE

To make changes in a global


variable, we have to use global
Keyword.

OUTPUT:
SCOPE OF A VARIABLE

OUTPUT:
25
LEGB RULE

L E G B

LOCAL ENCLOSING GLOBAL BUILT-IN


1 2 3 4
LEGB RULE

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:

You might also like