FUNCTIONS
FUNCTIONS
Computer Science
Class XII ( As per CBSE Board)
Functions
Function Introduction
A function is a programming block of codes
which is used to perform a single, related
task . It only runs when it is called. We can
pass data, known as parameters in to a
function . A function can return data as a
result.
We have already used some python built in
functions like print(), etc. But we can also
create our own functions .These functions
are called user-defined functions.
Advantages of Using functions:
1.Program development made easy and fast:
Work can be divided
Among project members thus implementation
can be completed fast.
2.Program testing becomes easy: Easy to
locate and isolate a faulty function for further
investigation
3.Code sharing becomes possible: A function
may be used later by many other programs this
means that a python programmer canuse
function written by others, instead of starting
over from scratch.
4.Code re-usability increases: A function can be
used to keep away from rewriting the same
block of codes which we are going use two or
more locations in a program. This is especially
5.Increases program readability: It makes
possible top down modular programming. In
this style of programming , the high level logic
of the over all problem is solved first while the
details of each lower level functions is
addressed later. The length of the source
program can be reduced by using functions at
appropriate places.
6.Function facilitates procedural abstraction:
Once a function is written, it serves as a black
box. All that a programmer would have to
know to invoke a function would be to know
its name, and the parameters that it expects
7.Functions facilitate the factoring of code: A
function can be called in other function and so
on…
Creating & calling a Function(user defined)
A function is defined using the def keyword in python .E.g.
program is given below.
def my_own_function():
print("Hello from a function")
#program start here. Program code
print("hello before calling a function")
my_own_function() #function calling. nowfunction codes
will be executed
print("hello after calling a function")
Save the above source code in python file and execute it
Variable’s Scope in function
There are three types of variables with the view of scope.
1.Local variable –accessible only inside the functional block
where it is declared.
2.Global variable –variable which is accessible among whole
program using global keyword.
3.Non local variable –accessible in nesting of functions,using
non local keyword.
String functions:
Method Description
capitalize() Converts the first character to upper case
casefold() Converts string into lower case
center() Returns a centered string
count() Returns the number of times a specified
value occurs in a string
encode() Returns an encoded version of the string
endswith() Returns true if the string ends with the specified
value
find() Searches the string for a specified value and returns
the position of where it was found
format() Formats specified values in a string
index() Searches the string for a specified value and
returns the position of where it was found
Functions using libraries
String functions:
Method Description
isalnum() Returns True if all characters in the string are
alphanumeric
isalpha() Returns True if all characters in the string are in the
alphabet
isdecimal() Returns True if all characters in the string are decimals
isdigit() Returns True if all characters in the string are digits
isidentifier() Returns True if the string is an identifier
islower() Returns True if all characters in the string are lower case
String functions:
Method Description
replace() Returns a string where a specified value is replaced
with a specified value
split() Splits the string at the specified separator, and
returns a list
splitlines() Splits the string at line breaks and returns a list
startswith() Returns true if the string starts with the specified
value
swapcase() Swaps cases, lower case becomes upper case and
vice versa
title() Converts the first character of each word to upper
case
translate() Returns a translated string
upper() Converts a string into upper case
zfill() Fills the string with a specified number of 0 values at
the beginning