SlideShare a Scribd company logo
6
Most read
17
Most read
19
Most read
Python Functions
● Define functions
● Passing arguments to Function
● Return a value from function
● Scope of Objects
● Default arguments
● Positional and keyword arguments
● Variable length arguments
Functions
 Piece of reusable code
 Solves particular task
 Call function instead of writing code
yourself
Built-in Functions
Syntax of Function
Mohammed Sikander 4
Defining a function
Function Call
 Once we have defined a function, we can call it
from another function, program or even the Python
prompt.
 To call a function we simply type the function name
with appropriate parameters.
Calling the Function
my_function()
Square function:Take one
arguments and prints its square
Square function:Take one
arguments and returns its square
Function returning multiple
value
Scope and Lifetime of
variables
 Scope of a variable is the portion of a program where the
variable is recognized.
 Parameters and variables defined inside a function is not visible
from outside. Hence, they have a local scope.
 Lifetime of a variable is the period throughout which the variable
exits in the memory. The lifetime of variables inside a function is
as long as the function executes.
 They are destroyed once we return from the function. Hence, a
function does not remember the value of a variable from its
previous calls.
Python   Functions
Python   Functions
Python   Functions
Default Arguments
 Function arguments can have default values in
Python.
 We can provide a default value to an argument by
using the assignment operator (=).
Default Arguments
 In this function, the
parameter amount does not have a default
value and is required (mandatory) during a
call.
 On the other hand, the parameter
discountPercentage has a default value
of 0. So, it is optional during a call.
 If a value is provided, it will overwrite the
default value.
 Any number of arguments in a function can
have a default value.
Default Arguments
 Once we have a default argument, all
the arguments to its right must also
have default values.
 SyntaxError: non-default argument
follows default argument
Keyword Arguments
 Positional argument cannot follow
keyword argument
Variable number of arguments
Functions as Objects
● Although functions are created differently from normal
variables, functions are just like any other kind of value.
● They can be assigned and reassigned to variables, and later
referenced by those names.
Python   Functions

More Related Content

PPTX
Functions in python slide share
PDF
Python Flow Control
PPTX
Looping statement in python
PPTX
Python dictionary
PPTX
Functions in python
PDF
Python programming : Control statements
PPTX
Basics of Object Oriented Programming in Python
PPTX
Data types in python
Functions in python slide share
Python Flow Control
Looping statement in python
Python dictionary
Functions in python
Python programming : Control statements
Basics of Object Oriented Programming in Python
Data types in python

What's hot (20)

PPTX
Python-Inheritance.pptx
PDF
Strings in python
PDF
Datatypes in python
PPTX
Conditional and control statement
PPTX
Functions in C
PPSX
Modules and packages in python
PPTX
Functions in Python
PDF
Python set
PPTX
Classes, objects in JAVA
PPTX
File handling in Python
PPSX
python Function
ODP
Python Modules
PPTX
Interface in java
PPTX
Call by value or call by reference in C++
PPTX
Type casting in java
PPTX
Method overloading
PPTX
Python
PPTX
List in Python
PPTX
Dynamic memory allocation in c
Python-Inheritance.pptx
Strings in python
Datatypes in python
Conditional and control statement
Functions in C
Modules and packages in python
Functions in Python
Python set
Classes, objects in JAVA
File handling in Python
python Function
Python Modules
Interface in java
Call by value or call by reference in C++
Type casting in java
Method overloading
Python
List in Python
Dynamic memory allocation in c
Ad

Similar to Python Functions (20)

PPTX
Functions Programming in Python Language
PPTX
Learn more about the concepts Functions of Python
PDF
Python Function.pdf
PDF
Userdefined functions brief explaination.pdf
PPTX
use of Functions to write python program.pptx
PDF
Python_Unit_2.pdf
PDF
functionnotes.pdf
PPTX
04. WORKING WITH FUNCTIONS-2 (1).pptx
PDF
PPTX
Python-Functions.pptx
PDF
Functions-.pdf
PDF
functions notes.pdf python functions and opp
PPT
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
PPTX
python-fefedfasdgsgfahfdshdhunctions-190506123237.pptx
PPTX
FUNCTION CPU
PPT
Powerpoint presentation for Python Functions
PPTX
User defined function in C.pptx
PDF
Python functions
PPTX
functions in python language to run programming
PPTX
functions new.pptx
Functions Programming in Python Language
Learn more about the concepts Functions of Python
Python Function.pdf
Userdefined functions brief explaination.pdf
use of Functions to write python program.pptx
Python_Unit_2.pdf
functionnotes.pdf
04. WORKING WITH FUNCTIONS-2 (1).pptx
Python-Functions.pptx
Functions-.pdf
functions notes.pdf python functions and opp
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
python-fefedfasdgsgfahfdshdhunctions-190506123237.pptx
FUNCTION CPU
Powerpoint presentation for Python Functions
User defined function in C.pptx
Python functions
functions in python language to run programming
functions new.pptx
Ad

More from Mohammed Sikander (20)

PPTX
Strings in C - covers string functions
PDF
Smart Pointers, Modern Memory Management Techniques
PDF
Multithreading_in_C++ - std::thread, race condition
PDF
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
PDF
Operator Overloading in C++
PDF
Python_Regular Expression
PPTX
Modern_CPP-Range-Based For Loop.pptx
PDF
Modern_cpp_auto.pdf
PDF
Python exception handling
PDF
Python tuple
PDF
Python strings
PDF
Python list
PDF
Introduction to Python
PPTX
Pointer basics
PPTX
PPTX
File management
PPT
Functions in C++
PPT
CPP Language Basics - Reference
PPTX
Java arrays
Strings in C - covers string functions
Smart Pointers, Modern Memory Management Techniques
Multithreading_in_C++ - std::thread, race condition
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Operator Overloading in C++
Python_Regular Expression
Modern_CPP-Range-Based For Loop.pptx
Modern_cpp_auto.pdf
Python exception handling
Python tuple
Python strings
Python list
Introduction to Python
Pointer basics
File management
Functions in C++
CPP Language Basics - Reference
Java arrays

Recently uploaded (20)

PDF
Module 3: Health Systems Tutorial Slides S2 2025
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PPTX
Onica Farming 24rsclub profitable farm business
PPTX
Introduction and Scope of Bichemistry.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PDF
LDMMIA Reiki Yoga Workshop 15 MidTerm Review
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
How to Manage Bill Control Policy in Odoo 18
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PDF
English Language Teaching from Post-.pdf
PDF
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Open folder Downloads.pdf yes yes ges yes
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
Module 3: Health Systems Tutorial Slides S2 2025
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
Onica Farming 24rsclub profitable farm business
Introduction and Scope of Bichemistry.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Insiders guide to clinical Medicine.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Open Quiz Monsoon Mind Game Final Set.pptx
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
How to Manage Starshipit in Odoo 18 - Odoo Slides
LDMMIA Reiki Yoga Workshop 15 MidTerm Review
The Final Stretch: How to Release a Game and Not Die in the Process.
102 student loan defaulters named and shamed – Is someone you know on the list?
How to Manage Bill Control Policy in Odoo 18
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
English Language Teaching from Post-.pdf
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
human mycosis Human fungal infections are called human mycosis..pptx
Open folder Downloads.pdf yes yes ges yes
Abdominal Access Techniques with Prof. Dr. R K Mishra

Python Functions

  • 1. Python Functions ● Define functions ● Passing arguments to Function ● Return a value from function ● Scope of Objects ● Default arguments ● Positional and keyword arguments ● Variable length arguments
  • 2. Functions  Piece of reusable code  Solves particular task  Call function instead of writing code yourself
  • 6. Function Call  Once we have defined a function, we can call it from another function, program or even the Python prompt.  To call a function we simply type the function name with appropriate parameters. Calling the Function my_function()
  • 7. Square function:Take one arguments and prints its square
  • 8. Square function:Take one arguments and returns its square
  • 10. Scope and Lifetime of variables  Scope of a variable is the portion of a program where the variable is recognized.  Parameters and variables defined inside a function is not visible from outside. Hence, they have a local scope.  Lifetime of a variable is the period throughout which the variable exits in the memory. The lifetime of variables inside a function is as long as the function executes.  They are destroyed once we return from the function. Hence, a function does not remember the value of a variable from its previous calls.
  • 14. Default Arguments  Function arguments can have default values in Python.  We can provide a default value to an argument by using the assignment operator (=).
  • 15. Default Arguments  In this function, the parameter amount does not have a default value and is required (mandatory) during a call.  On the other hand, the parameter discountPercentage has a default value of 0. So, it is optional during a call.  If a value is provided, it will overwrite the default value.  Any number of arguments in a function can have a default value.
  • 16. Default Arguments  Once we have a default argument, all the arguments to its right must also have default values.  SyntaxError: non-default argument follows default argument
  • 18.  Positional argument cannot follow keyword argument
  • 19. Variable number of arguments
  • 20. Functions as Objects ● Although functions are created differently from normal variables, functions are just like any other kind of value. ● They can be assigned and reassigned to variables, and later referenced by those names.