Working With Functions Notes
Working With Functions Notes
Advantages Of Function:
Example:
In order to call the ceil function of the math module the following
statement is required.
import math
x=math.ceil(4.2)
print(x) # 5
(iii) User Defined Function: We can also define our own functions
in Python, such functions are called user defined functions. In
order to define user defined functions, the function header and
function body has to be defined by the user.
Defining a function:
def functionname(paramterlist): #functionheader
Function-body
<Return Statement>
(v) return keyword is used to return the value from the function
body to the calling function. In the absence of a return statement,
None literal is returned.
Calling a function:
When we call a function, the name of the function along with the
required parameters are passed. If a function is returning a value
then it should be called in a variable or expression (not
mandatory).
Example:
def evenodd(x):
if x%2==0:
print(‘Even’)
else:
print(‘Odd’)
Calling:
x=int(input(‘Enter a number’))
def sum(n):
esum=osum=0
for i in range(1,n+1):
if i%2==0:
esum+=i
else:
osum+=i
print(esum,osum)
sum(10)
sum(12)
Example:
def factorial(x):
f=1
for i in range(1,x+1):
f=f*i
return f
x=factorial(5) # Calling 1
print(x)
y=int(input('Enter Factorial'))
print(factorial(y)) # Calling 2
z=factorial(y) #Calling 3
print('Factorial',z,sep='->',end='##')
Answer 1: 120
Answer 2: 720
Answer 3: 720
Factorial->720##
Example:
s=a+b
print(s)
x,y=20,30
Types of Parameter
● Positional Parameter:
a)When the parameter in a function header is a variable
then it is called positional parameter.
b)It is required to pass the values of positional
parameters while calling.
● Default Parameter:
a)When the variable in a function header is assigned with
some value then it is called the default parameter.
b)It is optional to pass the value of the default parameter.
c)If a function header has both positional and default
parameters then the order of all positional parameters
come first then followed by default parameters.
● Keyword Parameter:
a)It is associated with function calling
b)In this, parameter name and value is passed during
calling.
c)If both positional and keyword parameter are passed,
the positional should written first followed by keyword
parameter,
Example:
x=x+y
y=2*x-y
print(x,y)
def interest(p,r,t=5):
(a) interest(10,r=10)
(b) interest (p=1000,t=12,r=4)
(c) interest(p=10000,r=p//1000,t=r*2)
(d) interest(10000,4)
(e) interest(10000,5,2):
(f) Interest (10000,p=2000,4)
● The variables that are defined outside the function body are
called global variables. The global variable is defined using
global variablename
● The variables that are defined inside the function body are
called local variables. The parameters are also local to the
function.
Example:
x=10
def globalex(y=20):
z=10
s=y+z
print(s)
k=12
globalex(x+k)
print(x,k)
def globalex(y=20):
global x # Statement 1
x=x+y
print(x) #Statement 2
print(x) # Statement 3
globalex(x)
print(x) # Statement 4
(a)
(a)
(a)