getting-started-with-functions/script.rst
author anand
Thu, 11 Nov 2010 00:03:57 +0530
changeset 472 fcdec2d28c9a
parent 444 1aa91f8d8d59
child 463 192559461dcb
child 480 42f7aa36f1bc
permissions -rw-r--r--
Checklist OK for `The other kinds of plots`
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
444
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
     1
.. Objectives
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
     2
.. ----------
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
     3
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
     4
.. 8.1 LO: getting started with functions (3)
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
     5
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
     6
.. At the end of this tutorial, you will be able to 
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
     7
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
     8
.. 1. define function
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
     9
.. #. define functions with arguments
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    10
.. #. learn about docstrings
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    11
.. #. learn about return values
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    12
..     can return multiple values
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    13
.. #. read code
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    14
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    15
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    16
.. Prerequisites
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    17
.. -------------
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    18
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    19
..   1. should have ``ipython`` installed. 
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    20
..   #. getting started with ``ipython``.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    21
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    22
     
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    23
.. Author              : Anoop Jacob Thomas <anoop@fossee.in>
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    24
   Internal Reviewer   : 
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    25
   External Reviewer   :
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    26
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    27
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    28
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    29
==============================
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    30
Getting started with functions
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    31
==============================
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    32
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    33
{{{ show welcome slide }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    34
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    35
Hello and welcome to the tutorial getting started with functions.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    36
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    37
{{{ switch to next slide, outline slide }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    38
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    39
In this tutorial we will learn about functions in python, how to
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    40
define functions, arguments to functions, docstrings, and function
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    41
return value.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    42
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    43
{{{ switch to next slide, Function }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    44
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    45
While writing code, we always want to reduce the number of lines of
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    46
code and functions is a way of reusing the code. Thus the same lines
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    47
of code can be used again and again. A function is a portion of code
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    48
within a larger program that performs a specific task and is
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    49
relatively independent of the remaining code. Now let us get more
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    50
familiar with functions,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    51
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    52
{{{ switch to next slide, f(x) a mathematical function }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    53
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    54
Consider a mathematical function f(x) = x square. Here x is a variable
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    55
and with different values of x the value of function will change. When
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    56
x is one f(1) will return the value 1 and f(2) will return us the
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    57
value 4. Let us now see how to define the function f(x) in python.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    58
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    59
{{{ switch to next slide, define f(x) in Python }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    60
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    61
In your Ipython interpreter type the following,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    62
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    63
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    64
    def f(x):
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    65
    	return x*x
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    66
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    67
Well that defined the function, so before learning what we did let us
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    68
see if it returns the expected values, try,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    69
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    70
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    71
    f(1)
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    72
    f(2)
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    73
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    74
Yes, it returned 1 and 2 respectively. And now let us see what we did,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    75
we wrote two lines. The first line ``def f(x)`` is used to define the
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    76
name and the parameters to the function. ``def`` is a keyword and
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    77
``f`` is the name of the function and ``x`` the parameter of the
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    78
function.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    79
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    80
{{{ switch to next slide, problem statement 1 }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    81
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    82
%% 1 %% Write a python function named cube which computes the cube of
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    83
   a given number n.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    84
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    85
Pause here and try to solve the problem yourself.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    86
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    87
{{{ switch to next slide, solution }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    88
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    89
The problem can be solved as,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    90
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    91
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    92
    def cube(n):
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    93
    	return n**3
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    94
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    95
And now let us see how to write functions without arguments.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    96
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    97
{{{ switch to next slide, greet function }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    98
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    99
let us define a new function called ``greet`` which will print ``Hello
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   100
World``.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   101
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   102
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   103
    def greet():
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   104
    	print "Hello World!"
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   105
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   106
now try calling the function,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   107
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   108
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   109
    greet()
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   110
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   111
Well that is a function which takes no arguments. Also note that it is
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   112
not mandatory for a function to return values. The function ``greet``
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   113
neither takes any argument nor returns any value.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   114
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   115
Now let us see how to write functions with more than one argument.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   116
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   117
{{{ switch to next slide, exercise 2 }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   118
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   119
%% 2 %% Write a python function named ``avg`` which computes the
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   120
   average of ``a`` and ``b``.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   121
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   122
Pause here and try to solve the problem yourself.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   123
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   124
{{{ switch to next slide, solution 2 }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   125
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   126
The problem can be solved as,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   127
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   128
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   129
    def avg(a,b):
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   130
    	return (a + b)/2
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   131
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   132
Thus if we want a function to accept more arguments, we just list them
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   133
separated with a comma between the parenthesis after the function name
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   134
in the ``def`` line.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   135
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   136
{{{ switch to next slide, docstring }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   137
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   138
It is always a good practice to document the code that we write, and
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   139
for a function we define we should write an abstract of what the
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   140
function does, and that is called a docstring. Let us modify the
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   141
function ``avg`` and add docstring to it. Do the following,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   142
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   143
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   144
    def avg(a,b):
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   145
        """ avg takes two numbers as input (a & b), and
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   146
	returns the average of a and b"""
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   147
	return (a+b)/2
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   148
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   149
Note that docstrings are entered in the immediate line after the
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   150
function definition and put as a triple quoted string. And here as far
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   151
as the code functionality is concerned, we didn't do anything. We just
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   152
added an abstract of what the function does.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   153
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   154
Now try this in the ipython interpreter.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   155
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   156
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   157
    avg?
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   158
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   159
It displays the docstring as we gave it. Thus docstring is a good way
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   160
of documenting the function we write.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   161
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   162
Try to do this,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   163
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   164
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   165
    f?
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   166
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   167
It doesn't have a docstring associated with it. Also we cannot infer
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   168
anything from the function name, and thus we are forced to read the
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   169
code to understand anything about the function.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   170
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   171
{{{ switch to next slide, exercise 3 }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   172
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   173
%% 3 %% Add docstring to the function f.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   174
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   175
Pause here and try to do it yourself.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   176
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   177
{{{ switch to next slide, solution }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   178
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   179
We need to define the function again to add docstring to the function
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   180
``f`` and we do it as,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   181
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   182
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   183
    def f(x):
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   184
    	"""Accepts a number x as argument and,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   185
	returns the square of the number x."""
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   186
	return x*x
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   187
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   188
{{{ switch to next slide, exercise 4 }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   189
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   190
%% 4 %% Write a python function named ``circle`` which returns the
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   191
   area and perimeter of a circle given radius ``r``.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   192
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   193
Pause here and try to solve the problem yourself.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   194
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   195
{{{ switch to next slide, solution 4 }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   196
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   197
The problem requires us to return two values instead of one which we
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   198
were doing till now. We can solve the problem as,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   199
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   200
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   201
    def circle(r):
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   202
    	"""returns area and perimeter of a circle given radius r"""
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   203
	pi = 3.14
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   204
	area = pi * r * r
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   205
	perimeter = 2 * pi * r
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   206
	return area, perimeter
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   207
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   208
A python function can return any number of values. There is no
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   209
restriction for it.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   210
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   211
Let us call the function ``circle`` as,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   212
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   213
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   214
    a, p = circle(6)
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   215
    print a
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   216
    print p
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   217
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   218
Now we have done enough coding, let us do some code reading exercise,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   219
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   220
{{{ switch to next slide, what }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   221
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   222
What does the function ``what`` do?
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   223
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   224
.. def what( n ):
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   225
..     if n < 0: n = -n
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   226
..     while n > 0:
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   227
..         if n % 2 == 1:
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   228
..             return False
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   229
..         n /= 10
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   230
..     return True
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   231
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   232
Pause here and try to figure out what the function ``what`` does.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   233
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   234
{{{ switch to next slide, even_digits }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   235
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   236
.. def even_digits( n ):
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   237
..    """returns True if all the digits of number n is even
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   238
..    returns False if all the digits of number n is not even"""
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   239
..     if n < 0: n = -n
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   240
..     while n > 0:
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   241
..         if n % 2 == 1:
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   242
..             return False
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   243
..         n /= 10
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   244
..     return True
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   245
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   246
The function returns ``True`` if all the digits of the number ``n``
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   247
are even, otherwise it returns ``False``.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   248
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   249
Now one more code reading exercise,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   250
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   251
{{{ switch to next slide, what }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   252
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   253
What does the function ``what`` do?
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   254
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   255
.. def what( n ):
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   256
..     i = 1
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   257
..     while i * i < n:
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   258
..         i += 1
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   259
..     return i * i == n, i
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   260
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   261
Pause here and try to figure out what the function ``what`` does.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   262
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   263
{{{ switch to next slide, is_perfect_square }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   264
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   265
.. def is_perfect_square( n ):
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   266
..     """returns True and square root of n, if n is a perfect square,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   267
..     otherwise returns False and the square root of the 
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   268
..     next perfect square"""
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   269
..     i = 1
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   270
..     while i * i < n:
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   271
..         i += 1
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   272
..     return i * i == n, i
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   273
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   274
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   275
The function returns ``True`` and the square root of ``n`` if n is a
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   276
perfect square, otherwise it returns ``False`` and the square root of
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   277
the next perfect square.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   278
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   279
This brings us to the end of this tutorial, in this tutorial we covered
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   280
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   281
{{{ switch to next slide, summary }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   282
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   283
- Functions in Python
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   284
- Passing parameters to a function
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   285
- Returning values from a function
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   286
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   287
We also did few code reading exercises.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   288
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   289
{{{ switch to next slide, Thank you }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   290
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   291
Thank you!