Page 117 -
P. 117

define your function


           Reuse code with functions                                           A function is a

                                                                               reusable code.
           Most programming languages let you create reusable, shareable code with   boxed-up piece of
           functions. A function is a chunk of code that you separate out from the
           rest of your program, give a name, and then call from your code.
                                                                                                        smoothie()
           Different languages have different ways of creating functions. In Python,                   make_
           use the def keyword to define a new function. Here’s some Python code
           that defines a make_smoothie() function:
        Give the function a name.        The parentheses are important,
                                         so be sure to include them.


                   def make_smoothie():
                       juice = input("What juice would you like? ")
                       fruit = input("OK - and how about the fruit? ")
     The code          print("Thanks. Let's go!")
      you share is        print("Crushing the ice...")
      indented.        print("Blending the " + fruit)

                       print("Now adding in the " + juice + " juice")
                       print("Finished! There's your " + fruit + " and " + juice + " smoothie!")



           In Python, it’s important that you define the function before you use it, so
           make sure the code that calls (or uses) the function comes after the definition
           of the function:



                   print("Welcome to smoothie-matic 2.0")
       Call the    another = "Y"
       function.
       Note the    while another == "Y":
       use of          make_smoothie()
                                                                                encounters a call to the function,
       parens.         another = input("How about another(Y/N)? ")             When the computer first
                                                                                it jumps to the start of the
                                                                                 finds there... then returns to
                                                                                 the calling piece of code. The
           Every time that Python sees make_smoothie() in the code, it jumps    function, runs the code it
           to the code in the make_smoothie() function. It runs the code in the   function “answers the call" to
           function until it gets to the end, and then returns to the next line in the
           code that called it.                                                   run its code.


           Let’s use functions to share code within your program.


           82    Chapter 3
   112   113   114   115   116   117   118   119   120   121   122