functions.org
changeset 126 2eac725a5766
parent 123 ca292a5b83c7
equal deleted inserted replaced
125:27ddf1255daa 126:2eac725a5766
    20     In this tutorial we shall be looking at Functions in Python. We already
    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
    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
    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
    23     other details such as doc-strings, default arguments and keyword
    24     arguments. 
    24     arguments. 
       
    25     
       
    26     First let's start IPython by typing ipython in the terminal.
    25 
    27 
    26     Let's write a simple function that prints a Hello message, after
    28     Let's write a simple function that prints a Hello message, upon
    27     accepting a name. 
    29     accepting a name. 
    28 
    30 
    29         def welcome(name):
    31         def welcome(name):
    30 	    print "Hello", name 
    32 	    print "Hello", name 
    31 
    33 
    32     You would recall that def is a keyword that indicates a function
    34     You would recall that def is a keyword that indicates the function
    33     definition. 'welcome' is the name of the function and 'name' is
    35     definition. 'welcome' is the name of the function and 'name' is
    34     the lone argument to the function. Note that the function is
    36     the lone argument to the function. Note that the function is
    35     defined within an indented block, similar to any other block. Our
    37     defined within an indented block, just like to any other block. Our
    36     function welcome just has one line in it's definition.  
    38     function welcome just has one line in it's definition.  
    37     
    39     
    38     We can call our function, as follows -
    40     We can call our function, as follows -
    39         welcome("World")
    41         welcome("World")
    40 
    42 
    53 	    print "Hello", name 
    55 	    print "Hello", name 
    54     
    56     
    55     Notice that the doc string uses triple quotes. If the doc-string
    57     Notice that the doc string uses triple quotes. If the doc-string
    56     exceeds one line, we can use new line characters in it. 
    58     exceeds one line, we can use new line characters in it. 
    57     Also, as expected the doc-string is indented as is required
    59     Also, as expected the doc-string is indented as is required
    58     for anything within a block. 
    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.
    59 
    64 
    60     We shall now look at default arguments. 
    65     We shall now look at default arguments. 
    61     [show slide with examples of functions with default arguments]
    66     [show slide with examples of functions with default arguments]
    62     The split function has been used in two different ways in the
    67     The split function has been used in two different ways in the
    63     given example - one for splitting on spaces and the other for
    68     given example - one for splitting on spaces and the other for