Functions allow programmers to organize code into reusable blocks. There are built-in functions and user-defined functions. Functions make code easier to develop, test and reuse. Variables inside functions can be local, global or nonlocal. Parameters pass data into functions, while functions can return values. Libraries contain pre-defined functions for tasks like mathematics and string manipulation.
2. FUNCTIONS
ï‚— 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, into a
function.
ï‚— A function can return data as a result.
ï‚— We have already used some python built in
functions like print().
ï‚— We can also create our own functions. These
functions are called user-defined functions.
3. 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
ï‚— 3.Code sharing becomes possible : A function may be used later
by many other programs this means that a python programmer
can use 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
useful if the code involved is long or complicated.
ï‚— 5. A function can be called in other function a
4. TYPES OF FUNCTIONS
ï‚— 1. LIBRARY FUNCTIONSïƒ They are
pre-defined, inbuilt functions, used as
it. For eg: sqrt(81)ïƒ 9
ï‚— 2. USER DEFINED FUNCTIONSïƒ
They are defined by user or
programmer according to the
requirement. For eg: def square(a):
â—¦ Print (a*a)
5. FUNCTION DEFINITION
ï‚— A function is defined using the def
keyword in
ï‚— python.
ï‚— E.g. program is given below.
def abc():
print("Hello")
ï‚— abc() #function calling.
ï‚— Hello
ï‚— Save the above source code in python
file and
ï‚— execute it
6. 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 nonlocal
keyword.
7. Local variable:
ï‚— def fun():
ï‚— s = "I love
India!" print(s)
ï‚— s = "I love
World!"
ï‚— fun()
ï‚— print(s)
ï‚— Output:
ï‚— I love India!
ï‚— I love World!
Global variable :
def fun():
global s
fun()
print(s)
s = "I love India!“
print(s)
s = "I love world!"
fun()
print(s)
Output:
I love world!
I love India!
I love India!
8. Variable’s Scope in function
ï‚— #Find the output of below program
ï‚— def fun(x, y):
ï‚— # argument /parameter x and y
ï‚— global a
ï‚— a = 10
ï‚— x,y = y,x
ï‚— b = 20
ï‚— b = 30
ï‚— c = 30
ï‚— print(a,b,x,y)
ï‚— a, b, x, y = 1, 2, 3,4
ï‚— fun(50, 100)
ï‚— #passing value 50 and 100 in parameter x and y of function
fun()
ï‚— print(a, b, x, y)
9. Variable’s Scope in function
#Find the output of below program
def fun(x, y):
global a
a = 10
x,y = y,x
b = 20
b = 30
c = 30
print(a,b,x,y)
a, b, x, y = 1, 2, 3,4
fun(50, 100)
print(a, b, x, y)
OUTPUT :-
10 30 100 50
10 2 3 4
Visit
10. OUTPUT: Before calling fun2: 100
Calling fun2
now: After calling fun2: 100
x in main: 200
11. Parameters / Arguments
These are specified after the function name, inside the parentheses.
Multiple parameters are separated by comma.
The following example has a function with two parameters x and y.
When the function is called, we pass two values, which is used inside
the function to sum up the values and store in z and then return the
result
def sum(x,y): #x, y are formal arguments
z=x+y
return z #return the result
x,y=4,5
r=sum(x,y) #x, y are actual arguments
print(r)
Note :- 1. Function Prototype is declaration of function with name
,argument and return type.
2. A formal parameter, i.e. a parameter, is in the function definition. An
actual parameter, i.e. an argument, is in a function call.
12. Functions using libraries:
Mathematical functions: Mathematical functions
are available under math module.To use
mathematical functions under this module, we
have to import the
module using import math.
For e.g.
To use sqrt() function we have to write statements
like given below.
import math
r=math.sqrt(4)
print(r)
OUTPUT :
2.0
14. ï‚— String functions:
ï‚— String functions are available in python
standard module.These are always
ï‚— availble to use.
ï‚— For e.g. capitalize() function Converts
the first character of string to upper
ï‚— case.
ï‚— s="i love programming"
ï‚— r=s.capitalize()
ï‚— print(r)
ï‚— OUTPUT:
ï‚— I love programming