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