SlideShare a Scribd company logo
2
Most read
4
Most read
7
Most read
FUNCTIONS IN PYTHON
KAMINI SINGHAL
PGT CS
K V KAUSANI
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.
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
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)
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
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.
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!
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)
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
OUTPUT: Before calling fun2: 100
Calling fun2
now: After calling fun2: 100
x in main: 200
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.
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
MATH FUNCTIONS:
 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

More Related Content

What's hot (20)

PPTX
Python: Modules and Packages
Damian T. Gordon
 
PPT
friend function(c++)
Ritika Sharma
 
PPTX
Looping statement in python
RaginiJain21
 
PPTX
Conditional and control statement
narmadhakin
 
PPTX
Function in C program
Nurul Zakiah Zamri Tan
 
PPTX
Inline function
Tech_MX
 
PPTX
Python Exception Handling
Megha V
 
PDF
Function overloading ppt
Prof. Dr. K. Adisesha
 
ODP
Python Modules
Nitin Reddy Katkam
 
PPTX
Python dictionary
Mohammed Sikander
 
PPTX
Operators in Python
Anusuya123
 
PDF
List,tuple,dictionary
nitamhaske
 
PPTX
Functions in Python
Kamal Acharya
 
PDF
Python : Regular expressions
Emertxe Information Technologies Pvt Ltd
 
PPTX
Constructor and Types of Constructors
Dhrumil Panchal
 
PPTX
This keyword in java
Hitesh Kumar
 
PPT
Function overloading(c++)
Ritika Sharma
 
PPTX
INLINE FUNCTION IN C++
Vraj Patel
 
PPSX
python Function
Ronak Rathi
 
PDF
Python exception handling
Mohammed Sikander
 
Python: Modules and Packages
Damian T. Gordon
 
friend function(c++)
Ritika Sharma
 
Looping statement in python
RaginiJain21
 
Conditional and control statement
narmadhakin
 
Function in C program
Nurul Zakiah Zamri Tan
 
Inline function
Tech_MX
 
Python Exception Handling
Megha V
 
Function overloading ppt
Prof. Dr. K. Adisesha
 
Python Modules
Nitin Reddy Katkam
 
Python dictionary
Mohammed Sikander
 
Operators in Python
Anusuya123
 
List,tuple,dictionary
nitamhaske
 
Functions in Python
Kamal Acharya
 
Python : Regular expressions
Emertxe Information Technologies Pvt Ltd
 
Constructor and Types of Constructors
Dhrumil Panchal
 
This keyword in java
Hitesh Kumar
 
Function overloading(c++)
Ritika Sharma
 
INLINE FUNCTION IN C++
Vraj Patel
 
python Function
Ronak Rathi
 
Python exception handling
Mohammed Sikander
 

Similar to Functions in python (20)

PPTX
cbse class 12 Python Functions2 for class 12 .pptx
tcsonline1222
 
PDF
Functions2.pdf
prasnt1
 
PPTX
CHAPTER 01 FUNCTION in python class 12th.pptx
PreeTVithule1
 
PPTX
FUNCTIONINPYTHON.pptx
SheetalMavi2
 
PPTX
2 Functions2.pptx
RohitYadav830391
 
PDF
Functions.pdf cbse board latest 2023-24 all covered
anuragmaji08
 
PDF
Functions_19_20.pdf
paijitk
 
PPTX
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
tony8553004135
 
PDF
Functions2.pdf
Daddy84
 
PDF
3-Python Functions.pdf in simple.........
mxdsnaps
 
PPTX
Functions2.pptx
AkhilTyagi42
 
PPTX
Python Functions.pptx
AnuragBharti27
 
PPTX
functioninpython-1.pptx
SulekhJangra
 
PPTX
Python functions PYTHON FUNCTIONS1234567
AnjaneyuluKunchala1
 
PPTX
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
usha raj
 
PPTX
JNTUK python programming python unit 3.pptx
Venkateswara Babu Ravipati
 
PDF
Functionscs12 ppt.pdf
RiteshKumarPradhan1
 
PDF
Functions.pdf
kailashGusain3
 
PDF
Functions_21_22.pdf
paijitk
 
PDF
Python functions
Prof. Dr. K. Adisesha
 
cbse class 12 Python Functions2 for class 12 .pptx
tcsonline1222
 
Functions2.pdf
prasnt1
 
CHAPTER 01 FUNCTION in python class 12th.pptx
PreeTVithule1
 
FUNCTIONINPYTHON.pptx
SheetalMavi2
 
2 Functions2.pptx
RohitYadav830391
 
Functions.pdf cbse board latest 2023-24 all covered
anuragmaji08
 
Functions_19_20.pdf
paijitk
 
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
tony8553004135
 
Functions2.pdf
Daddy84
 
3-Python Functions.pdf in simple.........
mxdsnaps
 
Functions2.pptx
AkhilTyagi42
 
Python Functions.pptx
AnuragBharti27
 
functioninpython-1.pptx
SulekhJangra
 
Python functions PYTHON FUNCTIONS1234567
AnjaneyuluKunchala1
 
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
usha raj
 
JNTUK python programming python unit 3.pptx
Venkateswara Babu Ravipati
 
Functionscs12 ppt.pdf
RiteshKumarPradhan1
 
Functions.pdf
kailashGusain3
 
Functions_21_22.pdf
paijitk
 
Python functions
Prof. Dr. K. Adisesha
 
Ad

Recently uploaded (20)

PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Dimensions of Societal Planning in Commonism
StefanMz
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Ad

Functions in python

  • 1. FUNCTIONS IN PYTHON KAMINI SINGHAL PGT CS K V KAUSANI
  • 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