functions.org
changeset 128 fa5c77536e4e
parent 127 76fd286276f7
child 129 dcb9b50761eb
child 146 b92b4e7ecd7b
equal deleted inserted replaced
127:76fd286276f7 128:fa5c77536e4e
     1 * Functions
       
     2 *** Outline
       
     3 ***** Functions
       
     4 ******* review of what's been done in solving equations tutorial
       
     5 ********* def
       
     6 ********* name
       
     7 ********* arguments
       
     8 ********* indented block
       
     9 ********* calling a function
       
    10 ******* arguments are local to a function
       
    11 ******* return values
       
    12 ******* doc strings - with example.
       
    13 ******* code reading exercises?
       
    14 ******* default arguments
       
    15 ******* keyword arguments
       
    16 ******* availability library functions
       
    17 *** Script
       
    18     Welcome friends. 
       
    19 
       
    20     In this tutorial we shall be looking at Functions in Python. We already
       
    21     have looked at the basics of functions in the tutorial on solving
       
    22     equations. We shall first review these basics. Then we shall move on to
       
    23     other details such as doc-strings, default arguments and keyword
       
    24     arguments. 
       
    25     
       
    26     First let's start IPython by typing ipython in the terminal.
       
    27 
       
    28     Let's write a simple function that prints a Hello message, upon
       
    29     accepting a name. 
       
    30 
       
    31         def welcome(name):
       
    32 	    print "Hello", name 
       
    33 
       
    34     You would recall that def is a keyword that indicates the function
       
    35     definition. 'welcome' is the name of the function and 'name' is
       
    36     the lone argument to the function. Note that the function is
       
    37     defined within an indented block, just like to any other block. Our
       
    38     function welcome just has one line in it's definition.  
       
    39     
       
    40     We can call our function, as follows -
       
    41         welcome("World")
       
    42 
       
    43     (all arguments are local to a function)
       
    44 
       
    45     In general functions should be accompanied by documentation on how
       
    46     to use them. Python provides a convenient way of writing this within the
       
    47     function itself, using what are called doc strings. They were mentioned in the
       
    48     tutorial on strings. Let's look at how to write them here. 
       
    49 
       
    50     Let us add a simple doc string to our welcome function. 
       
    51 
       
    52         def welcome(name):
       
    53 	    """ Prints a hello message given a name, 
       
    54 	        passed as argument. """
       
    55 	    print "Hello", name 
       
    56     
       
    57     Notice that the doc string uses triple quotes. If the doc-string
       
    58     exceeds one line, we can use new line characters in it. 
       
    59     Also, as expected the doc-string is indented as is required
       
    60     for anything within a block. Now that we have written the
       
    61     documentation, how do we access it? IPython provides the question
       
    62     mark feature that we have seen in the previous tutorials. welcome?
       
    63     will display the docstring that we have just written.
       
    64 
       
    65     We shall now look at default arguments. 
       
    66     [show slide with examples of functions with default arguments]
       
    67     The split function has been used in two different ways in the
       
    68     given example - one for splitting on spaces and the other for
       
    69     splitting on commas.
       
    70 
       
    71     The function split is being called with no arguments and one
       
    72     argument, respectively. In the first case, white space is being
       
    73     used as a default value. Let's now edit our function, welcome, to
       
    74     use default values. (For convenience sake, we have dropped the doc-string)
       
    75 
       
    76         def welcome(name="World!"):
       
    77 	    print "Hello", name 
       
    78     
       
    79     Now, we call the function 'welcome' without passing any arguments
       
    80     to it. 
       
    81         welcome()
       
    82 
       
    83     As you can see the output is "Hello World!". Here "World!" is used as a
       
    84     default argument, when no name argument is passed to 'welcome'. 
       
    85 
       
    86     Let's now look at the use of keyword arguments. 
       
    87     [show slide with examples of functions with keyword arguments]
       
    88     We have already looked at functions and keyword arguments in these
       
    89     examples. loc, linewidth, xy, labels are all keywords. 
       
    90 
       
    91     Let's now edit our function so that it displays a custom 
       
    92     greeting message as well. 
       
    93 
       
    94     def welcome( greet = 'Hello', name = 'World!'):
       
    95         print greet, name
       
    96 
       
    97     Let's now see, how we can call our updated 'welcome' function, using
       
    98     keyword arguments. We can call the function in a variety of ways.
       
    99         welcome("Hello", "James")
       
   100 	welcome("Hi", name="Guido")
       
   101 	welcome(name="Guido", greet="Hello")
       
   102 
       
   103     Keyword arguments allow us to call functions by passing arguments
       
   104     in any order and removes the need to remember the order of arguments
       
   105     in the function definition. 
       
   106 
       
   107     Let's now write a new function 
       
   108 
       
   109     def per_square(n):
       
   110         i = 1
       
   111 	while ( i*i < n ):
       
   112 	    i += 1
       
   113 	return i*i == n, i
       
   114 
       
   115     What does this function do? It checks if the given number is a perfect square.
       
   116     If it is, then the function returns True along with the square root of
       
   117     the given number. If the number is not a perfect square it returns
       
   118     False and the square root of the next perfect square.
       
   119 
       
   120     Please observe that this function returns 2 values.
       
   121     In Python there is no restriction on the number of values returned by
       
   122     a function. Whenever a function has to return more than one value, the multiple
       
   123     values are packed into one single tuple and that single tuple is returned.
       
   124 
       
   125     With this we come to the end of this tutorial on functions. In this tutorial
       
   126     we have learnt about functions in a greater detail. We looked at
       
   127     how to define functions, calling them, default and keyword
       
   128     arguments. 
       
   129 
       
   130 *** Notes