Functions2new
Functions2new
syllabus
2023-24
Chapter 2
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, into a function. A function can return
data as a result.
Import the module named mymodule, and call the greeting function:
import mymodule
mymodule.greeting(“India")
3). User defined function:
Functions that we define ourselves to do certain specific task are
referred as user-defined functions because these are not already
available.
Creating & calling a Function
(user defined)/Flow of execution
A function is defined using the def keyword in
python.E.g. program is given below.
#Function block/
def my_own_function(): definition/creation
print("Hello from a function")
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)
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)
OUTPUT :-
10 30 100 50
10 2 3 4
Variable’s Scope in
function
Global variables in nested function
def fun1():
x = 100
def fun2():
global x
x = 200
print("Before calling fun2: " + str(x))
print("Calling fun2 now:")
fun2() OUTPUT:
print("After calling fun2: " + str(x)) Before calling fun2: 100
Calling fun2 now:
fun1() After calling fun2: 100
print("x in main: " + str(x)) x in main: 200
Variable’s Scope in
function
Non local variable
def fun1():
x = 100
def fun2():
nonlocal x #change it to global or remove this declaration
x = 200
print("Before calling fun2: " + str(x))
print("Calling fun2 now:")
fun2()
print("After calling fun2: " + str(x)) OUTPUT:
Before calling fun2: 100
x=50 Calling fun2 now:
fun1()
print("x in main: " + str(x))
After calling fun2: 200
x in main: 50
Function
Parameters / Arguments Passing and return value
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(z):
def sum(x,y): #x, y are formal arguments
z=x+y
return z #return the value/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.
Function
Function Arguments
Functions can be called using following types of formal arguments −
• Required arguments/Positional parameter - arguments passed in correct positional order
• Keyword arguments - the caller identifies the arguments by the parameter name
• Default arguments - that assumes a default value if a value is not provided to argu.
• Variable-length arguments – pass multiple values with single argument name.
E.g.
x = lambda a, b : a * b
print(x(5, 6))
OUTPUT:
30
Mutable/immutable
properties
of data objects w/r function
OUTPUT :
2.0
Functions using libraries
Functions available in Python Math Module
Functions using libraries
(System defined function)
String functions:
String functions are available in python standard module.These
are always availble to use.
OUTPUT:
I love programming
Functions using libraries
String functions:
Method Description
capitalize() Converts the first character to upper case
casefold() Converts string into lower case
center() Returns a centered string
zfill() Fills the string with a specified number of 0 values at the beginning