Functions
Based on CBSE Curriculum
Class-12
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 a 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 functions.
• Functions can be categorized in three types-
1. Built-in
2. Modules
3. User Defined
Built-in Functions
• These are the functions which are
predefined in python we have to just call
them to use.
• Functions make any programming
language efficient and provide a
structure to language.
• Python has many built-in functions
which makes programming easy,
fast and efficient.
• They always reside in standard library
and we need not to import any module
to use them.
• We will study about some built-in
function in next slide.
Built-in Functions. . .
1. Type Conversion Functions: These are
the functions which converts the values from
one type to another-
1. int( ) – To convert the string into integer.
2. str( ) – To covert any value into string.
3. float( ) – To covert string into float.
2. Input Functions: This function is used to take
input from user in the form of string.
e.g. name=input(“Enter your name : “)
3. eval function: This function is used to evaluate the
value of a string.
e.g. x=eval(“45+10“)
print(x) # answer will be 55
Python Modules
• Module is a .py file which contains the
definitions of functions and variables.
• Module is a simple python file.
• When we divide a program into modules
then each module contains functions and
variables. And each functions is made for
a special task.
• Once written code in modules can be
used in other programs.
• When we make such functions which
may be used in other programs also then
we write them in module.
• We can import those module in any
program an we can use the functions.
Python Modules. . .
• Python provides two ways to import
a module -
• import statement: to import full module.
• from: To import all or selected functions from the
module.
string Module
• We have already studied about string module in class
XI. Here
are some other functions of string module.
– [Link]() Converts first character to Capital Letter
– [Link]() Returns the Lowest Index of Substring
– [Link]() Returns Index of Substring
– [Link]() Checks Alphanumeric Character
– [Link]() Checks if All Characters are Alphabets
– [Link]() Checks Digit Characters
– [Link]() Checks if all Alphabets in a String, are Lowercase
– [Link]() returns if all characters are uppercase characters
– [Link]() Returns a Concatenated String
– [Link]() returns lowercased string
– [Link]()returns uppercased string
– len()Returns Length of an Object
– ord()returns Unicode code point for Unicode character
– reversed()returns reversed iterator of a sequence
Neha Tyagi, PGT CS, KV No-5,Jaipur
– slice()creates a slice object specified by range()
Neha Tyagi, PGT CS, KV No-5,Jaipur
User-defined Functions
• These are the functions which are
made by user as per the requirement
of the user.
• Function is a kind of collection of
statements which are written for a
specific task.
• We can use them in any part of our
program by calling them.
• def keyword is used to make user
defined functions.
Neha Tyagi, PGT CS, KV No-5,Jaipur
User-defined Functions. . .
• We use following syntax with def
keyword to prepare a user defined
function.
def
Function_Name(List_Of_Parame
ters): ”””docstring”””
statement(s)
Keyword After the line containing def
there should be a 4 spaces
indentation, which is also Function
know
Neha Tyagi, PGT CS, as the body or block
KV No-5,Jaipur Definition
of the function.
User-defined Functions without argument
and without return (Practical)
Function
Definition
Function Call
Neha Tyagi, PGT CS, KV No-5,Jaipur
User-defined Functions with argument and
without return (Practical)
In the case of arguments,
the values are passed in the
function’s parenthesis. And
they are declared in
definition as well.
Neha Tyagi, PGT CS, KV No-5,Jaipur
User-defined Functions with argument and
with return value (Practical)
In the case of returning
value, the calculated value is
sent outside the function by
a returning statement. This
returned value will be hold
by a variable used in calling
statement.
Neha Tyagi, PGT CS, KV No-5,Jaipur
User-defined Functions with multiple
return values (Practical)
I python a functio
n
may n
values I multipl
.returning n e
it returns a
sequence of values
to the calling
statement. These
returned values may
be used as per the
requirement.
Neha Tyagi, PGT CS, KV No-5,Jaipur
Parameters and Arguments in Functions
• When we write header of any function
then the one or more values given to its
parenthesis ( ) are known as parameter.
• These are the values which are used by
the function for any specific task.
• While argument is the value passed at
the time of calling a function.
• In other words the arguments are used
to invoke a function.
• Formal parameters are said to be
parameters and actual parameters are
said to be arguments.
Neha Tyagi, PGT CS, KV No-5,Jaipur
Parameters and Arguments in Functions . . .
These are the
parameters .
These are the
arguments .
Neha Tyagi, PGT CS, KV No-5,Jaipur
Types of Arguments
• Python supports 4 types of arguments-
1. Positional Arguments
2. Default Arguments
3. Keyword Arguments
4. Variable Length Arguments
Neha Tyagi, PGT CS, KV No-5,Jaipur
1. Positional Arguments
• These are the arguments which are
passed in correct positional order in
function.
• If we change the position of the
arguments then the answer will be
changed.
Neha Tyagi, PGT CS, KV No-5,Jaipur
2. Default Arguments
• These are the arguments through
which we can provide default values to
the function.
• If we don’t pass any value to the
function then it will take a pre defined
value.
This is point to
remember that the
default argument should
be given after non
Neha Tyagi, PGT CS, KV No-5,Jaipur default argument.
3. Keyword Arguments
• If a function have many arguments and we want to
change the sequence of them then we have to use
keyword arguments.
• Biggest benefit of keyword argument is that we need
not to remember the position of the argument.
• For this whenever we pass the values to the function
then we pass the values with the argument name. e.g.
If you used one
argumet with keyword
n then ers must also
oth be with
called keywords e
otherwian error will
Neha Tyagi, PGT CS, KV No-5,Jaipur
s be
raised.
Neha Tyagi, PGT CS, KV No-5,Jaipur
Scope of Variable
• Scope of variable means the part of program
where the variable will be visible. It means
where we can use this variable.
• We can say that scope is the collection of
variables and their values.
• Scope can of two types -
• Global (module)
– All those names which are assigned at top level in
module or directly assigned in interpreter.
• Local (function)
– Those variables which are assigned within a loop of
function.
Neha Tyagi, PGT CS, KV No-5,Jaipur
Using main() as a Function
• Main function is not necessary in python.
• For the convenience of Non-python
programmers, we can use it as follows-
Neha Tyagi, PGT CS, KV No-5,Jaipur
Flow of Execution at the Time of
Function Call
• The execution of any program starts from the very first
line and this execution goes line by line.
• One statement is executed at a time.
• Function doesn’t change the flow of any program.
• Statements of function doesn’t start executing until it is
called.
• When function is called then the control is jumped
into the body of function.
• Then all the statements of the function gets executed
from top to bottom one by one.
• And again the control returns to the place where it was
called.
Neha Tyagi, PGT CS, KV No-5,Jaipur
• And in this sequence the python program gets
executed.
Neha Tyagi, PGT CS, KV No-5,Jaipur