SlideShare a Scribd company logo
2
Most read
4
Most read
8
Most read
Swipe
Python - Functions
A function is a block of organized, reusable code
that is used to perform a single, related action.
Functions provide better modularity for your
application and a high degree of code reusing.
As you already know, Python gives you many
built-in functions like print(), etc.
but you can also create your own functions.
These functions are called user-defined functions.
Python - Functions
You can define functions to provide the required
functionality. Here are simple rules to define a
function in Python.
Function blocks begin with the keyword def
followed by the function name and
parentheses ( ( ) ).
Any input parameters or arguments should be
placed within these parentheses. You can also
define parameters inside these parentheses.
Defining a Function
The first statement of a function can be an
optional statement - the documentation string
of the function or docstring.
The code block within every function starts
with a colon (:) and is indented.
The statement return [expression] exits a
function, optionally passing back an
expression to the caller. A return statement
with no arguments is the same as return None.
Syntax
def functionname( parameters ):
"function_docstring"
function_suite
return [expression]
Calling a Function
Defining a function only gives it a name, specifies
the parameters that are to be included in the
function and structures the blocks of code.
Once the basic structure of a function is finalized,
you can execute it by calling it from another
function or directly from the Python prompt.
Pass by reference vs value
All parameters (arguments) in the Python
language are passed by reference.
It means if you change what a parameter refers to
within a function, the change also reflects back in
the calling function
Function Arguments
Required arguments
Keyword arguments
Default arguments
Variable-length arguments
You can call a function by using the following types of
formal arguments:-
Required arguments are the arguments passed to
a function in correct positional order.
Here, the number of arguments in the function
call should match exactly with the function
definition.
To call the function printme(), you definitely need
to pass one argument, otherwise it gives a syntax
error
Required arguments
Keyword arguments
Keyword arguments are related to the function
calls.
When you use keyword arguments in a function
call, the caller identifies the arguments by the
parameter name.
This allows you to skip arguments or place them
out of order because the Python interpreter is
able to use the keywords provided to match the
values with parameters.
You can also make keyword calls to the printme()
function
Default arguments
A default argument is an argument that assumes
a default value if a value is not provided in the
function call for that argument.
The following example gives an idea on default
arguments, it prints default age if it is not passed
#!/usr/bin/python
# Function definition is here
def printinfo( name, age = 35 ):
"This prints a passed info into this function"
print "Name: ", name
print "Age ", age
return;
# Now you can call printinfo function
printinfo( age=50, name="miki" )
printinfo( name="miki" )
When the Default arguments code is executed, it
produces the following result
Name: miki
Age 50
Name: miki
Age 35
Variable-length arguments
You may need to process a function for more
arguments than you specified while defining the
function.
These arguments are called variable-length
arguments and are not named in the function
definition, unlike required and default arguments.
Syntax
def functionname([formal_args,] *var_args_tuple ):
"function_docstring"
function_suite
return [expression]
Python - Modules
Python - Object Oriented
Stay Tuned with
Topics for next Post

More Related Content

What's hot (20)

PPTX
Advanced Python : Decorators
Bhanwar Singh Meena
 
PDF
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
PPSX
Modules and packages in python
TMARAGATHAM
 
ODP
Patterns in Python
dn
 
PPTX
Python-Classes.pptx
Karudaiyar Ganapathy
 
PDF
Python programming : Abstract classes interfaces
Emertxe Information Technologies Pvt Ltd
 
PPTX
Python: Modules and Packages
Damian T. Gordon
 
PPTX
Functions in python
colorsof
 
PPTX
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
PPTX
Python Lambda Function
Md Soyaib
 
PPTX
Arrays in Java
Abhilash Nair
 
PDF
Introduction to python 3
Youhei Sakurai
 
PPTX
Functions in Python
Kamal Acharya
 
PPTX
Std 12 computer chapter 6 object oriented concepts (part 1)
Nuzhat Memon
 
PPTX
Types of methods in python
Aravindreddy Mokireddy
 
PPTX
Python Functions
Mohammed Sikander
 
PPSX
Collections - Lists, Sets
Hitesh-Java
 
PPT
C++: Constructor, Copy Constructor and Assignment operator
Jussi Pohjolainen
 
PPTX
Chapter 05 classes and objects
Praveen M Jigajinni
 
ODP
OOP java
xball977
 
Advanced Python : Decorators
Bhanwar Singh Meena
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
Modules and packages in python
TMARAGATHAM
 
Patterns in Python
dn
 
Python-Classes.pptx
Karudaiyar Ganapathy
 
Python programming : Abstract classes interfaces
Emertxe Information Technologies Pvt Ltd
 
Python: Modules and Packages
Damian T. Gordon
 
Functions in python
colorsof
 
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Python Lambda Function
Md Soyaib
 
Arrays in Java
Abhilash Nair
 
Introduction to python 3
Youhei Sakurai
 
Functions in Python
Kamal Acharya
 
Std 12 computer chapter 6 object oriented concepts (part 1)
Nuzhat Memon
 
Types of methods in python
Aravindreddy Mokireddy
 
Python Functions
Mohammed Sikander
 
Collections - Lists, Sets
Hitesh-Java
 
C++: Constructor, Copy Constructor and Assignment operator
Jussi Pohjolainen
 
Chapter 05 classes and objects
Praveen M Jigajinni
 
OOP java
xball977
 

Similar to Python functions (20)

PPTX
Python programming - Functions and list and tuples
MalligaarjunanN
 
PPTX
Learn more about the concepts Functions of Python
PrathamKandari
 
PPT
Powerpoint presentation for Python Functions
BalaSubramanian376976
 
PPTX
Python Functions.pptx
AnuragBharti27
 
PPTX
Python Functions.pptx
AnuragBharti27
 
PPT
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
 
PPT
python slides introduction interrupt.ppt
Vinod Deenathayalan
 
PDF
functionnotes.pdf
AXL Computer Academy
 
PPTX
Lecture 08.pptx
Mohammad Hassan
 
PDF
Unit 1-Part-5-Functions and Set Operations.pdf
Harsha Patil
 
PPT
Python programming variables and comment
MalligaarjunanN
 
PPT
functions modules and exceptions handlings.ppt
Rajasekhar364622
 
PPTX
Functions in Python Programming Language
BeulahS2
 
PDF
Python Function.pdf
NehaSpillai1
 
PPTX
Python-Functions.pptx
Karudaiyar Ganapathy
 
PPTX
Functions in Python and its types for beginners
Mohammad Usman
 
PDF
Dive into Python Functions Fundamental Concepts.pdf
SudhanshiBakre1
 
PPTX
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
KalaivaniD12
 
PPTX
Functions and Modules.pptx
Ashwini Raut
 
PPTX
functions.pptx
KavithaChekuri3
 
Python programming - Functions and list and tuples
MalligaarjunanN
 
Learn more about the concepts Functions of Python
PrathamKandari
 
Powerpoint presentation for Python Functions
BalaSubramanian376976
 
Python Functions.pptx
AnuragBharti27
 
Python Functions.pptx
AnuragBharti27
 
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
 
python slides introduction interrupt.ppt
Vinod Deenathayalan
 
functionnotes.pdf
AXL Computer Academy
 
Lecture 08.pptx
Mohammad Hassan
 
Unit 1-Part-5-Functions and Set Operations.pdf
Harsha Patil
 
Python programming variables and comment
MalligaarjunanN
 
functions modules and exceptions handlings.ppt
Rajasekhar364622
 
Functions in Python Programming Language
BeulahS2
 
Python Function.pdf
NehaSpillai1
 
Python-Functions.pptx
Karudaiyar Ganapathy
 
Functions in Python and its types for beginners
Mohammad Usman
 
Dive into Python Functions Fundamental Concepts.pdf
SudhanshiBakre1
 
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
KalaivaniD12
 
Functions and Modules.pptx
Ashwini Raut
 
functions.pptx
KavithaChekuri3
 
Ad

More from Learnbay Datascience (20)

PDF
Top data science projects
Learnbay Datascience
 
PDF
Python my SQL - create table
Learnbay Datascience
 
PDF
Python my SQL - create database
Learnbay Datascience
 
PDF
Python my sql database connection
Learnbay Datascience
 
PDF
Python - mySOL
Learnbay Datascience
 
PDF
AI - Issues and Terminology
Learnbay Datascience
 
PDF
AI - Fuzzy Logic Systems
Learnbay Datascience
 
PDF
AI - working of an ns
Learnbay Datascience
 
PDF
Artificial Intelligence- Neural Networks
Learnbay Datascience
 
PDF
AI - Robotics
Learnbay Datascience
 
PDF
Applications of expert system
Learnbay Datascience
 
PDF
Components of expert systems
Learnbay Datascience
 
PDF
Artificial intelligence - expert systems
Learnbay Datascience
 
PDF
AI - natural language processing
Learnbay Datascience
 
PDF
Ai popular search algorithms
Learnbay Datascience
 
PDF
AI - Agents & Environments
Learnbay Datascience
 
PDF
Artificial intelligence - research areas
Learnbay Datascience
 
PDF
Artificial intelligence composed
Learnbay Datascience
 
PDF
Artificial intelligence intelligent systems
Learnbay Datascience
 
PDF
Applications of ai
Learnbay Datascience
 
Top data science projects
Learnbay Datascience
 
Python my SQL - create table
Learnbay Datascience
 
Python my SQL - create database
Learnbay Datascience
 
Python my sql database connection
Learnbay Datascience
 
Python - mySOL
Learnbay Datascience
 
AI - Issues and Terminology
Learnbay Datascience
 
AI - Fuzzy Logic Systems
Learnbay Datascience
 
AI - working of an ns
Learnbay Datascience
 
Artificial Intelligence- Neural Networks
Learnbay Datascience
 
AI - Robotics
Learnbay Datascience
 
Applications of expert system
Learnbay Datascience
 
Components of expert systems
Learnbay Datascience
 
Artificial intelligence - expert systems
Learnbay Datascience
 
AI - natural language processing
Learnbay Datascience
 
Ai popular search algorithms
Learnbay Datascience
 
AI - Agents & Environments
Learnbay Datascience
 
Artificial intelligence - research areas
Learnbay Datascience
 
Artificial intelligence composed
Learnbay Datascience
 
Artificial intelligence intelligent systems
Learnbay Datascience
 
Applications of ai
Learnbay Datascience
 
Ad

Recently uploaded (20)

PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
community health nursing question paper 2.pdf
Prince kumar
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 

Python functions

  • 2. A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. As you already know, Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions. Python - Functions
  • 3. You can define functions to provide the required functionality. Here are simple rules to define a function in Python. Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ). Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses. Defining a Function
  • 4. The first statement of a function can be an optional statement - the documentation string of the function or docstring. The code block within every function starts with a colon (:) and is indented. The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None. Syntax def functionname( parameters ): "function_docstring" function_suite return [expression]
  • 5. Calling a Function Defining a function only gives it a name, specifies the parameters that are to be included in the function and structures the blocks of code. Once the basic structure of a function is finalized, you can execute it by calling it from another function or directly from the Python prompt.
  • 6. Pass by reference vs value All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function
  • 7. Function Arguments Required arguments Keyword arguments Default arguments Variable-length arguments You can call a function by using the following types of formal arguments:-
  • 8. Required arguments are the arguments passed to a function in correct positional order. Here, the number of arguments in the function call should match exactly with the function definition. To call the function printme(), you definitely need to pass one argument, otherwise it gives a syntax error Required arguments
  • 9. Keyword arguments Keyword arguments are related to the function calls. When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name. This allows you to skip arguments or place them out of order because the Python interpreter is able to use the keywords provided to match the values with parameters. You can also make keyword calls to the printme() function
  • 10. Default arguments A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument. The following example gives an idea on default arguments, it prints default age if it is not passed #!/usr/bin/python # Function definition is here def printinfo( name, age = 35 ): "This prints a passed info into this function" print "Name: ", name print "Age ", age return; # Now you can call printinfo function printinfo( age=50, name="miki" ) printinfo( name="miki" )
  • 11. When the Default arguments code is executed, it produces the following result Name: miki Age 50 Name: miki Age 35
  • 12. Variable-length arguments You may need to process a function for more arguments than you specified while defining the function. These arguments are called variable-length arguments and are not named in the function definition, unlike required and default arguments. Syntax def functionname([formal_args,] *var_args_tuple ): "function_docstring" function_suite return [expression]
  • 13. Python - Modules Python - Object Oriented Stay Tuned with Topics for next Post