SlideShare a Scribd company logo
Functions in
PYTHON
Rahul Singh Sikarwar
Functions
• A function is a device that groups a set of statements so they
can be run more than once in a program—a packaged
procedure invoked by name
• Functions also can compute a result value and let us specify
parameters that serve as function inputs and may differ each
time the code is run.
• More fundamentally, functions are the alternative to
programming by cutting and pasting—rather than having
multiple redundant copies of an operation’s code, we can
factor it into a single function.
Function-related statements and
expressions
Introduction
• Functions are a nearly universal program-structuring device.
You may have come across them before in other languages,
where they may have been called subroutines or procedures.
• As a brief introduction, functions serve two primary
development roles:
1. Maximizing code reuse and minimizing redundancy
2. Procedural decomposition
• def is executable code. Python functions are written with a new
statement, the def.
 def is an executable statement—your function does not exist until
Python reaches and runs the def. In fact, it’s legal (and even
occasionally useful) to nest def statements inside if statements,
while loops, and even other defs.
• def creates an object and assigns it to a name.: When Python
reaches and runs a def statement, it generates a new function
object and assigns it to the function’s name. As with all
assignments, the function name becomes a reference to the
function object
• lambda creates an object but returns it as a result: Functions
may also be created with the lambda expression, a feature that
allows us to in-line function definitions in places where a def
statement won’t work syntactically.
• return sends a result object back to the caller: When a
function is called, the caller stops until the function finishes its
work and returns control to the caller. Functions that compute
a value send it back to the caller with a return statement; the
returned value becomes the result of the function call. A return
without a value simply returns to the caller
• yield sends a result object back to the caller, but
remembers where it left off. Functions known as generators
may also use the yield statement to send back a value and
suspend their state such that they may be resumed later, to
produce a series of results over time
def Statements
• The def statement creates a function object and assigns it to a name. Its general format is as follows:
def name(arg1, arg2,... argN):
Statements
Function bodies often contain a return statement:
def name(arg1, arg2,... argN):
...
return value
The Python return statement can show up anywhere in a function body; when reached, it ends the
function call and sends a result back to the caller. The return statement consists of an optional object
value expression that gives the function’s result. If the value is omitted, return sends back a None.
nest a function def inside an if statement to
select between alternative definitions:
if test:
def func(): # Define func this way
...
else:
def func(): # Or else this way
...
...
func() # Call the version selected and built
A First Example: Definitions and Calls
Definition:
Here’s a definition typed interactively that defines a function called
times, which returns the product of its two arguments:
>>> def times(x, y): # Create and assign function
... return x * y # Body executed when called
...
>>> times(2, 4) # Arguments in parentheses
8
Calls
Python program for solving a quadratic equation of the form ax
2
+bx+c=0
1. Use function def() to find the solution
2. a ,b and c is user input.
Input 1: a= 1, b=-3, c=2
Input 2: a= 1, b=-8, c=5
Input 3: a=5, b= 20, c= 32
use of Functions to write python program.pptx

More Related Content

PPTX
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
PPTX
Learn more about the concepts Functions of Python
PPTX
PP unit 2.pptx.................................
PPTX
PP unit 2.pptx............................
PPTX
Decided to go to the 65 and the value of the number
PPTX
Functions in python slide share
PPTX
Functions in Python Programming Language
PPTX
python programming module 3 notes for study material
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
Learn more about the concepts Functions of Python
PP unit 2.pptx.................................
PP unit 2.pptx............................
Decided to go to the 65 and the value of the number
Functions in python slide share
Functions in Python Programming Language
python programming module 3 notes for study material

Similar to use of Functions to write python program.pptx (20)

PDF
functions- best.pdf
PDF
Notes5
PPTX
Function in Python function in python.pptx
PPTX
JNTUK python programming python unit 3.pptx
PPT
04_python_functions.ppt You can define functions to provide the required func...
PPTX
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
DOCX
Python unit 3 and Unit 4
PPTX
Python Functions
PPTX
ForLoopandUserDefinedFunctions.pptx
PPTX
Chapter 2 Python Functions
PPT
Exception handling and function in python
PPTX
Working with functions the minumum ppt.pptx
PPTX
Lecture 5 Functions all about business categories
PDF
DOCX
Functions.docx
PDF
3-Python Functions.pdf in simple.........
PPTX
UNIT 3 python.pptx
PPTX
Python Details Functions Description.pptx
PPTX
04. WORKING WITH FUNCTIONS-2 (1).pptx
PDF
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
functions- best.pdf
Notes5
Function in Python function in python.pptx
JNTUK python programming python unit 3.pptx
04_python_functions.ppt You can define functions to provide the required func...
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
Python unit 3 and Unit 4
Python Functions
ForLoopandUserDefinedFunctions.pptx
Chapter 2 Python Functions
Exception handling and function in python
Working with functions the minumum ppt.pptx
Lecture 5 Functions all about business categories
Functions.docx
3-Python Functions.pdf in simple.........
UNIT 3 python.pptx
Python Details Functions Description.pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
Ad

Recently uploaded (20)

PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Download FL Studio Crack Latest version 2025 ?
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Complete Guide to Website Development in Malaysia for SMEs
PDF
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
history of c programming in notes for students .pptx
PDF
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PDF
Salesforce Agentforce AI Implementation.pdf
PDF
AutoCAD Professional Crack 2025 With License Key
PPTX
assetexplorer- product-overview - presentation
Computer Software and OS of computer science of grade 11.pptx
Download FL Studio Crack Latest version 2025 ?
Digital Systems & Binary Numbers (comprehensive )
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Advanced SystemCare Ultimate Crack + Portable (2025)
Odoo Companies in India – Driving Business Transformation.pdf
Complete Guide to Website Development in Malaysia for SMEs
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
history of c programming in notes for students .pptx
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
Internet Downloader Manager (IDM) Crack 6.42 Build 41
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
CHAPTER 2 - PM Management and IT Context
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
Salesforce Agentforce AI Implementation.pdf
AutoCAD Professional Crack 2025 With License Key
assetexplorer- product-overview - presentation
Ad

use of Functions to write python program.pptx

  • 2. Functions • A function is a device that groups a set of statements so they can be run more than once in a program—a packaged procedure invoked by name • Functions also can compute a result value and let us specify parameters that serve as function inputs and may differ each time the code is run. • More fundamentally, functions are the alternative to programming by cutting and pasting—rather than having multiple redundant copies of an operation’s code, we can factor it into a single function.
  • 4. Introduction • Functions are a nearly universal program-structuring device. You may have come across them before in other languages, where they may have been called subroutines or procedures. • As a brief introduction, functions serve two primary development roles: 1. Maximizing code reuse and minimizing redundancy 2. Procedural decomposition
  • 5. • def is executable code. Python functions are written with a new statement, the def.  def is an executable statement—your function does not exist until Python reaches and runs the def. In fact, it’s legal (and even occasionally useful) to nest def statements inside if statements, while loops, and even other defs. • def creates an object and assigns it to a name.: When Python reaches and runs a def statement, it generates a new function object and assigns it to the function’s name. As with all assignments, the function name becomes a reference to the function object • lambda creates an object but returns it as a result: Functions may also be created with the lambda expression, a feature that allows us to in-line function definitions in places where a def statement won’t work syntactically.
  • 6. • return sends a result object back to the caller: When a function is called, the caller stops until the function finishes its work and returns control to the caller. Functions that compute a value send it back to the caller with a return statement; the returned value becomes the result of the function call. A return without a value simply returns to the caller • yield sends a result object back to the caller, but remembers where it left off. Functions known as generators may also use the yield statement to send back a value and suspend their state such that they may be resumed later, to produce a series of results over time
  • 7. def Statements • The def statement creates a function object and assigns it to a name. Its general format is as follows: def name(arg1, arg2,... argN): Statements Function bodies often contain a return statement: def name(arg1, arg2,... argN): ... return value The Python return statement can show up anywhere in a function body; when reached, it ends the function call and sends a result back to the caller. The return statement consists of an optional object value expression that gives the function’s result. If the value is omitted, return sends back a None.
  • 8. nest a function def inside an if statement to select between alternative definitions: if test: def func(): # Define func this way ... else: def func(): # Or else this way ... ... func() # Call the version selected and built
  • 9. A First Example: Definitions and Calls Definition: Here’s a definition typed interactively that defines a function called times, which returns the product of its two arguments: >>> def times(x, y): # Create and assign function ... return x * y # Body executed when called ... >>> times(2, 4) # Arguments in parentheses 8
  • 10. Calls
  • 11. Python program for solving a quadratic equation of the form ax 2 +bx+c=0 1. Use function def() to find the solution 2. a ,b and c is user input. Input 1: a= 1, b=-3, c=2 Input 2: a= 1, b=-8, c=5 Input 3: a=5, b= 20, c= 32

Editor's Notes

  • #4: As in most programming languages, Python functions are the simplest way to package logic you may wish to use in more than one place and more than one time. Functions allow us to group and generalize code to be used arbitrarily many times later. Because they allow us to code an operation in a single place and use it in many places, Python functions are the most basic factoring tool in the language: they allow us to reduce code redundancy in our programs, and thereby reduce maintenance effort. Functions also provide a tool for splitting systems into pieces that have well-defined roles. For instance, to make a pizza from scratch, you would start by mixing the dough, rolling it out, adding toppings, baking it, and so on. If you were programming a pizza-making robot, functions would help you divide the overall “make pizza” task into chunks—one function for each subtask in the process. It’s easier to implement the smaller tasks in isolation than it is to implement the entire process at once
  • #7: The statement block becomes the function’s body—that is, the code Python executes each time the function is later called.