functions.org
author Santosh Vattam <vattam@fossee.in>
Tue, 11 May 2010 08:27:24 +0530
changeset 123 ca292a5b83c7
parent 120 50716c7c4c0c
permissions -rw-r--r--
Minor edits 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
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    20
    In this tutorial we shall be looking at Functions in Python. We already
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    21
    have looked at the basics of functions in the tutorial on solving
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    22
    equations. We shall first review these basics. Then we shall move on to
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    23
    other details such as doc-strings, default arguments and keyword
110
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    24
    arguments. 
123
ca292a5b83c7 Minor edits to functions.org.
Santosh Vattam <vattam@fossee.in>
parents: 120
diff changeset
    25
    
ca292a5b83c7 Minor edits to functions.org.
Santosh Vattam <vattam@fossee.in>
parents: 120
diff changeset
    26
    First let's start IPython by typing ipython in the terminal.
110
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    27
123
ca292a5b83c7 Minor edits to functions.org.
Santosh Vattam <vattam@fossee.in>
parents: 120
diff changeset
    28
    Let's write a simple function that prints a Hello message, upon
110
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    29
    accepting a name. 
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    30
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    31
        def welcome(name):
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    32
	    print "Hello", name 
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    33
123
ca292a5b83c7 Minor edits to functions.org.
Santosh Vattam <vattam@fossee.in>
parents: 120
diff changeset
    34
    You would recall that def is a keyword that indicates the function
110
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    35
    definition. 'welcome' is the name of the function and 'name' is
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    36
    the lone argument to the function. Note that the function is
123
ca292a5b83c7 Minor edits to functions.org.
Santosh Vattam <vattam@fossee.in>
parents: 120
diff changeset
    37
    defined within an indented block, just like to any other block. Our
110
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    38
    function welcome just has one line in it's definition.  
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    39
    
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    40
    We can call our function, as follows -
110
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    41
        welcome("World")
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    42
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    43
    (all arguments are local to a function)
110
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    44
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    45
    In general functions should be accompanied by documentation on how
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    46
    to use them. Python provides a convenient way of writing this within the
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    47
    function itself, using what are called doc strings. They were mentioned in the
110
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    48
    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
    49
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    50
    Let us add a simple doc string to our welcome function. 
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    51
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    52
        def welcome(name):
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    53
	    """ Prints a hello message given a name, 
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    54
	        passed as argument. """
110
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    55
	    print "Hello", name 
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    56
    
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    57
    Notice that the doc string uses triple quotes. If the doc-string
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    58
    exceeds one line, we can use new line characters in it. 
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    59
    Also, as expected the doc-string is indented as is required
123
ca292a5b83c7 Minor edits to functions.org.
Santosh Vattam <vattam@fossee.in>
parents: 120
diff changeset
    60
    for anything within a block. Now that we have written the
ca292a5b83c7 Minor edits to functions.org.
Santosh Vattam <vattam@fossee.in>
parents: 120
diff changeset
    61
    documentation, how do we access it? IPython provides the question
ca292a5b83c7 Minor edits to functions.org.
Santosh Vattam <vattam@fossee.in>
parents: 120
diff changeset
    62
    mark feature that we have seen in the previous tutorials. welcome?
ca292a5b83c7 Minor edits to functions.org.
Santosh Vattam <vattam@fossee.in>
parents: 120
diff changeset
    63
    will display the docstring that we have just written.
110
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    64
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    65
    We shall now look at default arguments. 
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    66
    [show slide with examples of functions with default arguments]
113
6388eacf7502 Changes to functions.org.
Shantanu <shantanu@fossee.in>
parents: 110
diff changeset
    67
    The split function has been used in two different ways in the
120
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
    68
    given example - one for splitting on spaces and the other for
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
    69
    splitting on commas.
110
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
    The function split is being called with no arguments and one
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    72
    argument, respectively. In the first case, white space is being
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    73
    used as a default value. Let's now edit our function, welcome, to
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    74
    use default values. (For convenience sake, we have dropped the doc-string)
110
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    75
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    76
        def welcome(name="World!"):
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    77
	    print "Hello", name 
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    78
    
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    79
    Now, we call the function 'welcome' without passing any arguments
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    80
    to it. 
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    81
        welcome()
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    82
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    83
    As you can see the output is "Hello World!". Here "World!" is used as a
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    84
    default argument, when no name argument is passed to 'welcome'. 
110
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 look at the use of keyword arguments. 
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    87
    [show slide with examples of functions with keyword arguments]
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    88
    We have already looked at functions and keyword arguments in these
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    89
    examples. loc, linewidth, xy, labels are all keywords. 
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    90
120
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
    91
    Let's now edit our function so that it displays a custom 
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    92
    greeting message as well. 
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    93
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    94
    def welcome( greet = 'Hello', name = 'World!'):
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    95
        print greet, name
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    96
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
    97
    Let's now see, how we can call our updated 'welcome' function, using
110
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    98
    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
    99
        welcome("Hello", "James")
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
   100
	welcome("Hi", name="Guido")
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
   101
	welcome(name="Guido", greet="Hello")
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
   102
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
   103
    Keyword arguments allow us to call functions by passing arguments
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
   104
    in any order and removes the need to remember the order of arguments
110
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
   105
    in the function definition. 
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
   106
120
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
   107
    Let's now write a new function 
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
   108
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
   109
    def per_square(n):
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
   110
        i = 1
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
   111
	while ( i*i < n ):
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
   112
	    i += 1
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
   113
	return i*i == n, i
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
   114
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
   115
    What does this function do? It checks if the given number is a perfect square.
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
   116
    If it is, then the function returns True along with the square root of
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
   117
    the given number. If the number is not a perfect square it returns
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
   118
    False and the square root of the next perfect square.
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
   119
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
   120
    Please observe that this function returns 2 values.
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
   121
    In Python there is no restriction on the number of values returned by
120
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
   122
    a function. Whenever a function has to return more than one value, the multiple
119
7dc53e6c8065 Updated functions and dictionaries script.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 118
diff changeset
   123
    values are packed into one single tuple and that single tuple is returned.
118
2f247bcfae8b Minor edits to functions script.
Puneeth Chaganti <punchagan@gmail.com>
parents: 113
diff changeset
   124
120
50716c7c4c0c Completed functions.org and added functions.tex file.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 119
diff changeset
   125
    With this we come to the end of this tutorial on functions. In this tutorial
110
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
   126
    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
   127
    how to define functions, calling them, default and keyword
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
   128
    arguments. 
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
   129
c1099ad60539 Added functions org file.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
   130
*** Notes