functions.org
changeset 110 c1099ad60539
child 113 6388eacf7502
equal deleted inserted replaced
107:dd6973b09679 110:c1099ad60539
       
     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. We already have
       
    21     looked at the basics of functions in the tutorial on solving
       
    22     equations. We shall first review these basics. Next we shall look
       
    23     at other details like doc-strings, default arguments and keyword
       
    24     arguments. 
       
    25 
       
    26     Let's write a simple function that prints a Hello message, after
       
    27     accepting a name. 
       
    28 
       
    29         def welcome(name):
       
    30 	    print "Hello", name 
       
    31 
       
    32     You would recall that def is a keyword that indicates a function
       
    33     definition. 'welcome' is the name of the function and 'name' is
       
    34     the lone argument to the function. Note that the function is
       
    35     defined within an indented block, similar to any other block. Our
       
    36     function welcome just has one line in it's definition.  
       
    37     
       
    38     We can call our function by passing, as follows -
       
    39         welcome("World")
       
    40 
       
    41     (arguments are local to a function)
       
    42 
       
    43     In general functions should be accompanied by documentation on how
       
    44     to use it. Python provides a convenient way of writing within the
       
    45     function itself, using doc strings. They were mentioned in the
       
    46     tutorial on strings. Let's look at how to write them here. 
       
    47 
       
    48     Let us add a simple doc string to our welcome function. 
       
    49 
       
    50         def welcome(name):
       
    51 	    """ Prints a hello message to a person, given a name. """
       
    52 	    print "Hello", name 
       
    53     
       
    54     Notice that the doc string uses triple quotes. If the doc-string
       
    55     exceeded one line, we could have used new line characters in
       
    56     it. Also, as expected the doc-string is indented as is required
       
    57     for anything within a block. 
       
    58 
       
    59     We shall now look at default arguments. 
       
    60     [show slide with examples of functions with default arguments]
       
    61     The strip function has been used in two different ways in the
       
    62     previous tutorials - one for splitting on spaces and the other for
       
    63     splitting on commas. 
       
    64 
       
    65     The function split is being called with no arguments and one
       
    66     argument, respectively. In the first case, white space is being
       
    67     used as a default value. Let's now edit our function, welcome, to
       
    68     use default values. (For convenience, we have dropped the doc-string)
       
    69 
       
    70         def welcome(name="World!"):
       
    71 	    print "Hello", name 
       
    72     
       
    73     Now, we call the function 'welcome' without passing any arguments
       
    74     to it. 
       
    75         welcome()
       
    76 
       
    77     "World!" is used as a default argument, when no name argument is
       
    78     passed to 'welcome'. 
       
    79 
       
    80     Let's now look at the use of keyword arguments. 
       
    81     [show slide with examples of functions with keyword arguments]
       
    82     We have already looked at functions and keyword arguments in these
       
    83     examples. loc, linewidth, xy, labels are all keywords. 
       
    84 
       
    85     Let's now see, how we can call our function 'welcome', using
       
    86     keyword arguments. We can call the function in a variety of ways.
       
    87         welcome("Hello", "James")
       
    88 	welcome("Hi", name="Guido")
       
    89 	welcome(name="Guido", greet="Hello")
       
    90 
       
    91     Keyword arguments allow us to call functions by passing arguments
       
    92     in any order and removes need to remember the order of arguments
       
    93     in the function definition. 
       
    94 
       
    95 
       
    96 ***** return values
       
    97 ***** arguments are local to a function
       
    98 ***** availability library functions
       
    99 ***** code reading exercises?
       
   100   
       
   101     We come to the end of this tutorial on functions. In this tutorial
       
   102     we have learnt about functions in a greater detail. We looked at
       
   103     how to define functions, calling them, default and keyword
       
   104     arguments. 
       
   105 
       
   106 *** Notes