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
04. WORKING WITH FUNCTIONS-2 (1).pptx
PPTX
Learn more about the concepts Functions of Python
PPTX
Functions in Python Programming Language
PDF
functions notes.pdf python functions and opp
PPTX
FUNCTIONS IN R PROGRAMMING.pptx
PDF
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
PPTX
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
PPTX
FUNCTION CPU
04. WORKING WITH FUNCTIONS-2 (1).pptx
Learn more about the concepts Functions of Python
Functions in Python Programming Language
functions notes.pdf python functions and opp
FUNCTIONS IN R PROGRAMMING.pptx
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
FUNCTION CPU

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

PPTX
functions in the c programming and the examples
PDF
Chapter 11 Function
PDF
3-Python Functions.pdf in simple.........
PPT
Powerpoint presentation for Python Functions
PPTX
FUNCTIONengineeringtechnologyslidesh.pptx
PPTX
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
PPTX
Functions in C
PPTX
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
PPTX
function of C.pptx
PPTX
functions new.pptx
DOC
4. function
PDF
Chapter 1. Functions in C++.pdf
PDF
Chapter_1.__Functions_in_C++[1].pdf
PPT
Lecture 11 - Functions
PDF
Python functions
PPTX
Functions and modular programming.pptx
PPT
python slides introduction interrupt.ppt
PPTX
Functions
PPTX
User defined function in C.pptx
PDF
functions in the c programming and the examples
Chapter 11 Function
3-Python Functions.pdf in simple.........
Powerpoint presentation for Python Functions
FUNCTIONengineeringtechnologyslidesh.pptx
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
Functions in C
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
function of C.pptx
functions new.pptx
4. function
Chapter 1. Functions in C++.pdf
Chapter_1.__Functions_in_C++[1].pdf
Lecture 11 - Functions
Python functions
Functions and modular programming.pptx
python slides introduction interrupt.ppt
Functions
User defined function in C.pptx
Ad

Recently uploaded (20)

PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Digital Strategies for Manufacturing Companies
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
history of c programming in notes for students .pptx
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Nekopoi APK 2025 free lastest update
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
medical staffing services at VALiNTRY
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Introduction to Artificial Intelligence
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
System and Network Administration Chapter 2
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Design an Analysis of Algorithms II-SECS-1021-03
Upgrade and Innovation Strategies for SAP ERP Customers
Digital Strategies for Manufacturing Companies
Navsoft: AI-Powered Business Solutions & Custom Software Development
history of c programming in notes for students .pptx
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Softaken Excel to vCard Converter Software.pdf
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
How to Choose the Right IT Partner for Your Business in Malaysia
Nekopoi APK 2025 free lastest update
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
medical staffing services at VALiNTRY
PTS Company Brochure 2025 (1).pdf.......
Wondershare Filmora 15 Crack With Activation Key [2025
Introduction to Artificial Intelligence
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
How Creative Agencies Leverage Project Management Software.pdf
System and Network Administration Chapter 2
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Design an Analysis of Algorithms II-SECS-1021-03
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.