functions.org
changeset 120 50716c7c4c0c
parent 119 7dc53e6c8065
child 123 ca292a5b83c7
equal deleted inserted replaced
119:7dc53e6c8065 120:50716c7c4c0c
    58     for anything within a block. 
    58     for anything within a block. 
    59 
    59 
    60     We shall now look at default arguments. 
    60     We shall now look at default arguments. 
    61     [show slide with examples of functions with default arguments]
    61     [show slide with examples of functions with default arguments]
    62     The split function has been used in two different ways in the
    62     The split function has been used in two different ways in the
    63     previous tutorials - one for splitting on spaces and the other for
    63     given example - one for splitting on spaces and the other for
    64     splitting on semicolons. 
    64     splitting on commas.
    65 
    65 
    66     The function split is being called with no arguments and one
    66     The function split is being called with no arguments and one
    67     argument, respectively. In the first case, white space is being
    67     argument, respectively. In the first case, white space is being
    68     used as a default value. Let's now edit our function, welcome, to
    68     used as a default value. Let's now edit our function, welcome, to
    69     use default values. (For convenience sake, we have dropped the doc-string)
    69     use default values. (For convenience sake, we have dropped the doc-string)
    81     Let's now look at the use of keyword arguments. 
    81     Let's now look at the use of keyword arguments. 
    82     [show slide with examples of functions with keyword arguments]
    82     [show slide with examples of functions with keyword arguments]
    83     We have already looked at functions and keyword arguments in these
    83     We have already looked at functions and keyword arguments in these
    84     examples. loc, linewidth, xy, labels are all keywords. 
    84     examples. loc, linewidth, xy, labels are all keywords. 
    85 
    85 
    86     Let's now customize our function so that it displays a custom 
    86     Let's now edit our function so that it displays a custom 
    87     greeting message as well. 
    87     greeting message as well. 
    88 
    88 
    89     def welcome( greet = 'Hello', name = 'World!'):
    89     def welcome( greet = 'Hello', name = 'World!'):
    90         print greet, name
    90         print greet, name
    91 
    91 
    97 
    97 
    98     Keyword arguments allow us to call functions by passing arguments
    98     Keyword arguments allow us to call functions by passing arguments
    99     in any order and removes the need to remember the order of arguments
    99     in any order and removes the need to remember the order of arguments
   100     in the function definition. 
   100     in the function definition. 
   101 
   101 
   102     
   102     Let's now write a new function 
       
   103 
       
   104     def per_square(n):
       
   105         i = 1
       
   106 	while ( i*i < n ):
       
   107 	    i += 1
       
   108 	return i*i == n, i
       
   109 
       
   110     What does this function do? It checks if the given number is a perfect square.
       
   111     If it is, then the function returns True along with the square root of
       
   112     the given number. If the number is not a perfect square it returns
       
   113     False and the square root of the next perfect square.
       
   114 
       
   115     Please observe that this function returns 2 values.
   103     In Python there is no restriction on the number of values returned by
   116     In Python there is no restriction on the number of values returned by
   104     a function. When a function returns more than one value, the multiple
   117     a function. Whenever a function has to return more than one value, the multiple
   105     values are packed into one single tuple and that single tuple is returned.
   118     values are packed into one single tuple and that single tuple is returned.
   106 
   119 
   107     We come to the end of this tutorial on functions. In this tutorial
   120     With this we come to the end of this tutorial on functions. In this tutorial
   108     we have learnt about functions in a greater detail. We looked at
   121     we have learnt about functions in a greater detail. We looked at
   109     how to define functions, calling them, default and keyword
   122     how to define functions, calling them, default and keyword
   110     arguments. 
   123     arguments. 
   111 
   124 
   112 *** Notes
   125 *** Notes