SlideShare a Scribd company logo
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
 A function is a block of code that performs
a specific task.
 Piece of reusable code
 For performing repeated, we can make a
function so that instead of writing the same
code again and again for different inputs
 Call function instead of writing code
yourself
Functions Types & Benefits
 Types of Functions in Python
◦ Built-in library function: These are
Standard functions in Python that are available
to use.
◦ User-defined function: We can create our
own functions based on our requirements.
 Benefits of Using Functions
◦ Increase Code Readability
◦ Increase Code Reusability
Built-in Functions
Mohammed Sikander 5
Syntax of Function
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.
Functions Programming in Python Language
Functions Programming in Python Language
Functions Programming in Python Language
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.
Functions Programming in Python Language

More Related Content

PPTX
Python Functions
PPTX
2 Functions2.pptx
PPTX
UNIT 3 python.pptx
PPTX
CHAPTER 01 FUNCTION in python class 12th.pptx
PDF
PDF
functions- best.pdf
PPTX
FUNCTIONS.pptx
PDF
Functions2.pdf
Python Functions
2 Functions2.pptx
UNIT 3 python.pptx
CHAPTER 01 FUNCTION in python class 12th.pptx
functions- best.pdf
FUNCTIONS.pptx
Functions2.pdf

Similar to Functions Programming in Python Language (20)

PDF
3-Python Functions.pdf in simple.........
PDF
Functions2.pdf
PPTX
cbse class 12 Python Functions2 for class 12 .pptx
PPT
Python programming variables and comment
PPTX
functioninpython-1.pptx
PPTX
Python Functions.pptx
PPT
Powerpoint presentation for Python Functions
PPT
functions modules and exceptions handlings.ppt
PPT
python slides introduction interrupt.ppt
PDF
Functions.pdf cbse board latest 2023-24 all covered
PPTX
Learn more about the concepts Functions of Python
PPTX
04. WORKING WITH FUNCTIONS-2 (1).pptx
PPT
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
PDF
functions notes.pdf python functions and opp
PDF
Python Function.pdf
PDF
Functions.pdf
PDF
Functionscs12 ppt.pdf
PPTX
python-fefedfasdgsgfahfdshdhunctions-190506123237.pptx
PDF
Userdefined functions brief explaination.pdf
PPT
functions _
3-Python Functions.pdf in simple.........
Functions2.pdf
cbse class 12 Python Functions2 for class 12 .pptx
Python programming variables and comment
functioninpython-1.pptx
Python Functions.pptx
Powerpoint presentation for Python Functions
functions modules and exceptions handlings.ppt
python slides introduction interrupt.ppt
Functions.pdf cbse board latest 2023-24 all covered
Learn more about the concepts Functions of Python
04. WORKING WITH FUNCTIONS-2 (1).pptx
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
functions notes.pdf python functions and opp
Python Function.pdf
Functions.pdf
Functionscs12 ppt.pdf
python-fefedfasdgsgfahfdshdhunctions-190506123237.pptx
Userdefined functions brief explaination.pdf
functions _
Ad

Recently uploaded (20)

PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Newfamily of error-correcting codes based on genetic algorithms
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
cuic standard and advanced reporting.pdf
PDF
Modernizing your data center with Dell and AMD
PPTX
MYSQL Presentation for SQL database connectivity
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
SAP855240_ALP - Defining the Global Template PUBLIC.pdf
PDF
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
PPTX
CroxyProxy Instagram Access id login.pptx
PDF
DevOps & Developer Experience Summer BBQ
PDF
KodekX | Application Modernization Development
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Electronic commerce courselecture one. Pdf
PDF
Advanced Soft Computing BINUS July 2025.pdf
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
NewMind AI Monthly Chronicles - July 2025
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Newfamily of error-correcting codes based on genetic algorithms
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
cuic standard and advanced reporting.pdf
Modernizing your data center with Dell and AMD
MYSQL Presentation for SQL database connectivity
“AI and Expert System Decision Support & Business Intelligence Systems”
SAP855240_ALP - Defining the Global Template PUBLIC.pdf
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
CroxyProxy Instagram Access id login.pptx
DevOps & Developer Experience Summer BBQ
KodekX | Application Modernization Development
Sensors and Actuators in IoT Systems using pdf
GamePlan Trading System Review: Professional Trader's Honest Take
20250228 LYD VKU AI Blended-Learning.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Electronic commerce courselecture one. Pdf
Advanced Soft Computing BINUS July 2025.pdf
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
NewMind AI Monthly Chronicles - July 2025
Ad

Functions Programming in Python Language

  • 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  A function is a block of code that performs a specific task.  Piece of reusable code  For performing repeated, we can make a function so that instead of writing the same code again and again for different inputs  Call function instead of writing code yourself
  • 3. Functions Types & Benefits  Types of Functions in Python ◦ Built-in library function: These are Standard functions in Python that are available to use. ◦ User-defined function: We can create our own functions based on our requirements.  Benefits of Using Functions ◦ Increase Code Readability ◦ Increase Code Reusability
  • 7. 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()
  • 8. Square function:Take one arguments and prints its square
  • 9. Square function:Take one arguments and returns its square
  • 11. 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.
  • 15. Default Arguments  Function arguments can have default values in Python.  We can provide a default value to an argument by using the assignment operator (=).
  • 16. 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.
  • 17. 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
  • 19.  Positional argument cannot follow keyword argument
  • 20. Variable number of arguments
  • 21. 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.

Editor's Notes

  • #2: Function is a group of related statements that perform a specific task. Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable. Furthermore, it avoids repetition and makes code reusable.
  • #5: Keyword def marks the start of function header. A function name to uniquely identify it. Parameters (arguments) through which we pass values to a function. They are optional. A colon (:) to mark the end of function header. Optional documentation string (docstring) to describe what the function does. One or more valid python statements that make up the function body. Statements must have same indentation level. An optional return statement to return a value from the function
  • #10: import math def quadEquation(a,b,c): x1 = (-b + math.sqrt( b**2 - 4 * a * c)) / (2 * a) x2 = (-b - math.sqrt( b**2 - 4 * a * c)) / (2 * a) return x1,x2 if __name__ == "__main__": print("Program to calculate the quadratic equation ") a = int(input("Enter the value of a : ")) b = int(input("Enter the value of b : ")) c = int(input("Enter the value of c : ")) x1,x2 = quadEquation(a,b,c) print(x1, x2)
  • #11: def my_func(): x = 10 print("Value inside function:",x) x = 20 my_func() print("Value outside function:",x)
  • #14: total = 0 def add(a , b): global total total = a + b print('Inside add total = ', total) add(4, 9) print('Main Block total = ', total)
  • #15: def calculateTotal(amount , discountPercentage = 0): discountAmount = discountPercentage / 100 * amount return amount - discountAmount amount = 500 totalBillAmount = calculateTotal(amount , 10) print(totalBillAmount) amount = 500 totalBillAmount = calculateTotal(amount) print(totalBillAmount) 450 500
  • #20: def mysum(*a): total = 0 for ele in a: print(total,"+",ele,"=", end='') total += ele print(total) return total print(mysum(5, 2, 9, 4)) ------------------------------------- Built-in sum function works with list (not variable number of arguments) Max function works with variable number of arguments --------------------------------------------------------------- def mymax(a,*b): large = a for ele in b: if ele > large: large = ele return large print(mymax(4,5,7,3,1,8,2))
  • #21: def multiply(x, y): return x * y a = 4 b = 7 operation = multiply print(operation(a, b))
  • #22: def add(a,b): print("Add") return a + b def sub(a,b): print("Sub") return a - b def mul(a,b): print("Multiply") return a * b def div(a,b): print("Divide") return a / b if __name__ == "__main__": print("Arithmetic Operations") print("Enter two numbers ") a = int(input()) b = int(input()) print("1. Add 2. Sub 3. Multiply 4. Divide") ch = int(input()) calculate=add if ch==1 else sub if ch==2 else mul if ch==3 else div res = calculate(a, b) print(res)