KEMBAR78
use of Functions to write python program.pptx
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

use of Functions to write python program.pptx

  • 1.
  • 2.
    Functions • A functionis 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.
  • 3.
  • 4.
    Introduction • Functions area 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 isexecutable 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 sendsa 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 • Thedef 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 functiondef 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.
  • 11.
    Python program forsolving 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.