getting-started-with-functions/script.rst
author anand
Mon, 15 Nov 2010 15:23:28 +0530
changeset 510 06561ee0f6a4
parent 484 a1828587a617
permissions -rw-r--r--
checklist OK for `writing python scripts`
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   :
480
42f7aa36f1bc language check done for `getting started with functions`
bhanu
parents: 444
diff changeset
    26
   Language Reviewer   : Bhanukiran
481
a4056cec859e Internal Review not don for `getting starte with functions`
anand
parents: 480
diff changeset
    27
   Checklist OK?       : <put date stamp here, not OK> [2010-10-05]
444
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
==============================
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    31
Getting started with functions
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
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    34
{{{ show welcome slide }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    35
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    36
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
    37
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    38
{{{ switch to next slide, outline slide }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    39
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    40
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
    41
define functions, arguments to functions, docstrings, and function
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    42
return value.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    43
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    44
{{{ switch to next slide, Function }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    45
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    46
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
    47
code and functions is a way of reusing the code. Thus the same lines
480
42f7aa36f1bc language check done for `getting started with functions`
bhanu
parents: 444
diff changeset
    48
of code can be used as many times as needed. A function is a portion of code
444
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    49
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
    50
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
    51
familiar with functions,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    52
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    53
{{{ 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
    54
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    55
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
    56
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
    57
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
    58
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
    59
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    60
{{{ 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
    61
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    62
In your Ipython interpreter type the following,
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
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    65
    def f(x):
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    66
    	return x*x
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    67
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    68
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
    69
see if it returns the expected values, try,
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
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    72
    f(1)
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    73
    f(2)
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    74
480
42f7aa36f1bc language check done for `getting started with functions`
bhanu
parents: 444
diff changeset
    75
Yes, it returned 1 and 4 respectively. And now let us see what we did.
42f7aa36f1bc language check done for `getting started with functions`
bhanu
parents: 444
diff changeset
    76
We wrote two lines: The first line ``def f(x)`` is used to define the
42f7aa36f1bc language check done for `getting started with functions`
bhanu
parents: 444
diff changeset
    77
name and the parameters to the function and the second line is used to
42f7aa36f1bc language check done for `getting started with functions`
bhanu
parents: 444
diff changeset
    78
fix what the function is supposed to return. ``def`` is a keyword and
444
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    79
``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
    80
function.
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
{{{ switch to next slide, problem statement 1 }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    83
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    84
%% 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
    85
   a given number n.
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
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
    88
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    89
{{{ switch to next slide, solution }}}
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
The problem can be solved as,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    92
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    93
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    94
    def cube(n):
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    95
    	return n**3
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
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
    98
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    99
{{{ switch to next slide, greet function }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   100
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   101
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
   102
World``.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   103
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   104
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   105
    def greet():
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   106
    	print "Hello World!"
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
now try calling the function,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   109
::
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
    greet()
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   112
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   113
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
   114
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
   115
neither takes any argument nor returns any value.
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
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
   118
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   119
{{{ switch to next slide, exercise 2 }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   120
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   121
%% 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
   122
   average of ``a`` and ``b``.
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
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
   125
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   126
{{{ switch to next slide, solution 2 }}}
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
The problem can be solved as,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   129
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   130
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   131
    def avg(a,b):
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   132
    	return (a + b)/2
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   133
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   134
Thus if we want a function to accept more arguments, we just list them
480
42f7aa36f1bc language check done for `getting started with functions`
bhanu
parents: 444
diff changeset
   135
separated with a comma between the parenthesis after the function's name
444
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   136
in the ``def`` line.
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
{{{ switch to next slide, docstring }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   139
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   140
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
   141
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
   142
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
   143
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
   144
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   145
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   146
    def avg(a,b):
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   147
        """ 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
   148
	returns the average of a and b"""
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   149
	return (a+b)/2
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   150
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   151
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
   152
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
   153
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
   154
added an abstract of what the function does.
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
Now try this in the ipython interpreter.
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   157
::
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
    avg?
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   160
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   161
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
   162
of documenting the function we write.
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
Try to do this,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   165
::
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
    f?
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   168
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   169
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
   170
anything from the function name, and thus we are forced to read the
480
42f7aa36f1bc language check done for `getting started with functions`
bhanu
parents: 444
diff changeset
   171
code to understand about the function.
444
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
{{{ switch to next slide, exercise 3 }}}
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
%% 3 %% Add docstring to the function f.
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
Pause here and try to do it yourself.
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
{{{ switch to next slide, solution }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   180
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   181
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
   182
``f`` and we do it as,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   183
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   184
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   185
    def f(x):
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   186
    	"""Accepts a number x as argument and,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   187
	returns the square of the number x."""
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   188
	return x*x
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
{{{ switch to next slide, exercise 4 }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   191
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   192
%% 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
   193
   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
   194
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   195
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
   196
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   197
{{{ switch to next slide, solution 4 }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   198
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   199
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
   200
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
   201
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   202
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   203
    def circle(r):
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   204
    	"""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
   205
	pi = 3.14
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   206
	area = pi * r * r
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   207
	perimeter = 2 * pi * r
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   208
	return area, perimeter
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   209
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   210
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
   211
restriction for it.
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
Let us call the function ``circle`` as,
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   214
::
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   215
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   216
    a, p = circle(6)
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   217
    print a
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   218
    print p
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
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
   221
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   222
{{{ switch to next slide, what }}}
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
What does the function ``what`` do?
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   225
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   226
.. def what( n ):
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   227
..     if n < 0: n = -n
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   228
..     while n > 0:
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   229
..         if n % 2 == 1:
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   230
..             return False
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   231
..         n /= 10
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   232
..     return True
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
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
   235
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   236
{{{ switch to next slide, even_digits }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   237
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   238
.. def even_digits( n ):
480
42f7aa36f1bc language check done for `getting started with functions`
bhanu
parents: 444
diff changeset
   239
..    """returns True if all the digits in the number n are even,
42f7aa36f1bc language check done for `getting started with functions`
bhanu
parents: 444
diff changeset
   240
..    returns False if all the digits in the number n are not even"""
444
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   241
..     if n < 0: n = -n
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   242
..     while n > 0:
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   243
..         if n % 2 == 1:
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   244
..             return False
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   245
..         n /= 10
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   246
..     return True
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   247
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   248
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
   249
are even, otherwise it returns ``False``.
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
Now one more code reading exercise,
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
{{{ switch to next slide, what }}}
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
What does the function ``what`` do?
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   256
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   257
.. def what( 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
..     while i * i < n:
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   260
..         i += 1
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   261
..     return i * i == n, i
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
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
   264
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   265
{{{ switch to next slide, is_perfect_square }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   266
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   267
.. def is_perfect_square( n ):
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   268
..     """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
   269
..     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
   270
..     next perfect square"""
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
..     while i * i < n:
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   273
..         i += 1
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   274
..     return i * i == n, i
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   275
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   276
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   277
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
   278
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
   279
the next perfect square.
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
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
   282
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   283
{{{ switch to next slide, summary }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   284
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   285
- Functions in Python
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   286
- Passing parameters to a function
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   287
- Returning values from a function
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
We also did few code reading exercises.
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
{{{ switch to next slide, Thank you }}}
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   292
1aa91f8d8d59 added script getting started with functions.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   293
Thank you!