Functions in Python PPT free download

Python Functions from Mohammed Sikander

Functions in python slide share from Devashish Kumar

Happy Birthday Example: Function Flow # # Simple illustration of functions. def happy(): print 'Happy Birthday to you!' def sing(person): happy() happy ... – PowerPoint PPT presentation

Transcript and Presenter's Notes

Title: FUNCTIONS in Python

What Are Functions?

  • Functions are sub-programs which perform tasks
    which may need to be repeated
  • Some functions are bundled in standard
    libraries which are part of any languages core
  • Functions are similar to methods, but may not be
    connected with objects
  • Programmers can write their own functions

Why Write Functions?
  • Reusability
  • Fewer errors introduced when code isnt rewritten
  • Reduces complexity of code

Happy Birthday Example Function Flow
  • Simple illustration of functions.
  • def happy()
  • print "Happy Birthday to you!"
  • def sing(person)
  • happy()
  • happy()
  • print "Happy birthday, dear", person "."
  • happy()
  • def main()
  • sing("Fred")
  • print
  • sing("Lucy")
  • print
  • sing("Elmer")

Functions Formal vs. Actual Paramaters (and
Formal Parameters
  • from graphics import
  • def moveTo(object, point)
  • c object.getCenter()
  • dx point.getX() - c.getX()
  • dy point.getY() - c.getY()
  • object.move(dx,dy)
  • def main()
  • win GraphWin()
  • circ Circle(Point(100,100), 20)
  • circ.draw(win)
  • p win.getMouse()
  • moveTo(circ, p)
  • win.close()
  • center circ.getCenter()
  • print center.getX(), center.getY()
  • main()

Function definition
Actual parameters or arguments
Call or invocation of function Arguments must be
in correct order according to function definition
Functions Return values
  • return keyword indicates what value(s) will be
    kicked back after a function has been invoked
  • def square(x) return x xThe call
    output square(myNum)

Formal parameter
Return value
Return value used as argument Example of
calculating a hypotenuse
  • num1 10
  • num2 14
  • Hypotenuse math.sqrt(sum_of_squares(num1,
  • def sum_of_squares(x,y) t (xx) (y y)
    return t

8 example
  • Text of

Returning more than one value
  • Functions can return more than one value
  • def hi_low(x,y) if x y return x, y
    else return y, x
  • The callhiNum, lowNum hi_low(data1, data2)

Modifying Parameters How could this be?
  • Usually, we ask a function to return a value to
    the calling function. This is how the calling
    function gets data processed.
  • But we can also have a function modify a
    parameter used in the calling function.
  • This seems counterintuitive, since we said
    earlier that the scope of a variable is limited
    to the function in which it is declared.
  • It also seems counterintuitive because we know
    that the VALUE of the parameters is passed to
    other functions, not the parameters themselves.

Function call that fails to get a value back
  • def addInterest(balance, rate)
  • newBalance balance (1rate)
  • balance newBalance
  • def test()
  • amount 1000 rate 0.05addInterest(amount,
  • print amount
  • test()

Function call that gets a return value
  • def addInterest(balance, rate) newBalance
    balance (1rate) return newBalance def
    test()amount 1000rate 0.05amount
    addInterest(amount, rate)print amounttest()

Changing parameter values by reference
  • Some programming languages allow a calling
    function to pass parameters by reference (the
    memory address of the variable is passed, not the
    actual value, so the value of parameter in the
    calling function is changed. PYTHON DOES NOT

Passing parameters by value
  • Python (like all languages) permits passing
    parameter VALUES, not actual parameters, as we
    have already seen. This is called PASSING BY VALUE

Changing passed parameters
  • Python does have a mechanism for having a called
    function alter the value of a parameter passed by
    a calling function.
  • The trick is that Python allows OBJECTS to be
    passed, and when this happens the values
    associated with that object CAN be changed by a
    called function.

Changing parameters passed as objects
  • def addInterest(balances, rate)for i in
    range(len(balances))balancesi balancesi
    (1rate)def test()amounts 1000, 2200, 800,
    360rate 0.05addInterest(amounts, 0.05)print

Summary passing parameters
  • Python always passes VARIABLES (given as
    parameters) by VALUE. In this case, the original
    variables in the calling function are NOT
  • Functions can RETURN a value which alters a
    variable in the calling function.
  • Python allows OBJECTS (lists, graphics, etc.) to
    be passed and altered by functions.

Program Structure
  • Functions serve to reduce complexity in the main
    algorithm of a program, making the program easier
    to understand and maintain.
  • Examine is a leading presentation sharing website. It has millions of presentations already uploaded and available with 1,000s more being uploaded by its users every day. Whatever your area of interest, here you’ll be able to find and view presentations you’ll love and possibly download. And, best of all, it is completely free and easy to use.

You might even have a presentation you’d like to share with others. If so, just upload it to We’ll convert it to an HTML5 slideshow that includes all the media types you’ve already added: audio, video, music, pictures, animations and transition effects. Then you can share it with your target audience as well as’s millions of monthly visitors. And, again, it’s all free.

About the Developers is brought to you by CrystalGraphics, the award-winning developer and market-leading publisher of rich-media enhancement products for presentations. Our product offerings include millions of PowerPoint templates, diagrams, animated 3D characters and more.

What are functions in Python PPT?

A function is a block of organized, reusable code that is used to perform a single, related action. Functions provides better modularity for your application and a high degree of code reusing..
As you already know, Python gives you many built-in functions like print() etc. but you can also create your own functions..

What are the 4 types of functions in Python?

The following are the different types of Python Functions:.
Python Built-in Functions..
Python Recursion Functions..
Python Lambda Functions..
Python User-defined Functions..

How to write functions in Python?

Basic Syntax for Defining a Function in Python In Python, you define a function with the def keyword, then write the function identifier (name) followed by parentheses and a colon. The next thing you have to do is make sure you indent with a tab or 4 spaces, and then specify what you want the function to do for you.

What are the basic functions in Python?

Python Built-in Functions.
print( ) function..
type( ) function..
input( ) function..
abs( ) function..
pow( ) function..
dir( ) function..
sorted( ) function..
max( ) function..