SlideShare a Scribd company logo
2
Most read
3
Most read
4
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

PPTX
Waterfall model
PPSX
Cucumber & gherkin language
PDF
Test unitaire
PPTX
Build restful ap is with python and flask
PDF
Fiddler 피들러에 대해 알아보자
PPTX
System models of sdlc- v model
PPTX
V model
PPT
Test case design
Waterfall model
Cucumber & gherkin language
Test unitaire
Build restful ap is with python and flask
Fiddler 피들러에 대해 알아보자
System models of sdlc- v model
V model
Test case design

What's hot (20)

PDF
Py.test
PDF
MongoDB Aggregation Framework
PPTX
자동화된 Test Case의 효과
PPTX
Software development life cycle (SDLC)
PPTX
Software Development Life Cycle
PPTX
Karate DSL
PPTX
Api testing
PPTX
Iterative model in sdlc
PDF
cours java complet-2.pdf
PDF
Arquitecturas de software - Parte 2
PPTX
Swagger - make your API accessible
PPTX
METODOLOGIA RUP
PDF
Software Testing (1)
PPTX
OOP - Polymorphism
PPTX
V model Over View (Software Engineering)
PPTX
Iterative model
PDF
Calidad y Seguridad en Procesos de Desarrollo de Software
PPTX
Agile Software Development Model
PPTX
Behavior Driven Development Testing (BDD)
Py.test
MongoDB Aggregation Framework
자동화된 Test Case의 효과
Software development life cycle (SDLC)
Software Development Life Cycle
Karate DSL
Api testing
Iterative model in sdlc
cours java complet-2.pdf
Arquitecturas de software - Parte 2
Swagger - make your API accessible
METODOLOGIA RUP
Software Testing (1)
OOP - Polymorphism
V model Over View (Software Engineering)
Iterative model
Calidad y Seguridad en Procesos de Desarrollo de Software
Agile Software Development Model
Behavior Driven Development Testing (BDD)
Ad

Similar to Python functions (20)

PPTX
Python programming - Functions and list and tuples
PPTX
Learn more about the concepts Functions of Python
PPT
Powerpoint presentation for Python Functions
PPTX
Python Functions.pptx
PPTX
Python Functions.pptx
PPT
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
PPT
python slides introduction interrupt.ppt
PDF
functionnotes.pdf
PPTX
Lecture 08.pptx
PDF
Unit 1-Part-5-Functions and Set Operations.pdf
PPT
Python programming variables and comment
PPT
functions modules and exceptions handlings.ppt
PPTX
Functions in Python Programming Language
PDF
Python Function.pdf
PPTX
Python-Functions.pptx
PPTX
Functions in Python and its types for beginners
PDF
Dive into Python Functions Fundamental Concepts.pdf
PPTX
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
PPTX
Functions and Modules.pptx
PPTX
functions.pptx
Python programming - Functions and list and tuples
Learn more about the concepts Functions of Python
Powerpoint presentation for Python Functions
Python Functions.pptx
Python Functions.pptx
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
python slides introduction interrupt.ppt
functionnotes.pdf
Lecture 08.pptx
Unit 1-Part-5-Functions and Set Operations.pdf
Python programming variables and comment
functions modules and exceptions handlings.ppt
Functions in Python Programming Language
Python Function.pdf
Python-Functions.pptx
Functions in Python and its types for beginners
Dive into Python Functions Fundamental Concepts.pdf
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
Functions and Modules.pptx
functions.pptx
Ad

More from Learnbay Datascience (20)

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

Recently uploaded (20)

PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Computing-Curriculum for Schools in Ghana
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Pharma ospi slides which help in ospi learning
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Insiders guide to clinical Medicine.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
master seminar digital applications in india
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Pre independence Education in Inndia.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
01-Introduction-to-Information-Management.pdf
Complications of Minimal Access Surgery at WLH
Computing-Curriculum for Schools in Ghana
O7-L3 Supply Chain Operations - ICLT Program
Abdominal Access Techniques with Prof. Dr. R K Mishra
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Microbial disease of the cardiovascular and lymphatic systems
Pharma ospi slides which help in ospi learning
102 student loan defaulters named and shamed – Is someone you know on the list?
Insiders guide to clinical Medicine.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Microbial diseases, their pathogenesis and prophylaxis
master seminar digital applications in india
O5-L3 Freight Transport Ops (International) V1.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx

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