summaryrefslogtreecommitdiff
path: root/getting-started-with-functions/script.rst
blob: be099382b2b8b259d4391704d74b33e8918c92a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
.. Objectives
.. ----------

.. 8.1 LO: getting started with functions (3)

.. At the end of this tutorial, you will be able to 

.. 1. define function
.. #. define functions with arguments
.. #. learn about docstrings
.. #. learn about return values
..     can return multiple values
.. #. read code


.. Prerequisites
.. -------------

..   1. should have ``ipython`` installed. 
..   #. getting started with ``ipython``.

     
.. Author              : Anoop Jacob Thomas <anoop@fossee.in>
   Internal Reviewer   : 
   External Reviewer   :
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]


==============================
Getting started with functions
==============================

{{{ show welcome slide }}}

Hello and welcome to the tutorial, getting started with functions.

{{{ switch to next slide, outline slide }}}

In this tutorial we will learn about functions in python, how to
define functions, passing arguments to functions, docstrings, and
function return value.

{{{ switch to next slide, Function }}}

While writing code, we would like to reduce the number of lines of
code and using functions is a way of reusing the code.  A function is
a portion of code within a larger program that performs a specific
task and is relatively independent of the remaining code. Now let us
get more familiar with functions,

{{{ switch to next slide, f(x) a mathematical function }}}

Consider a mathematical function f(x) = x squared. Here x is a
variable and with different values of x the value of function will
change. When x is one f(1) will return the value 1 and f(2) will
return us the value 4. Let us now see how to define the function f(x)
in Python.

{{{ switch to next slide, define f(x) in Python }}}

In your Ipython interpreter type the following,
::

    def f(x):
    	return x*x

Let us see, if the function ``f``, we defined, works as we expect. 
::

    f(1)
    f(2)

Yes, it returned 1 and 2 respectively. 

Now, let us see what we did. The first line ``def f(x)`` is used to
define the name and the parameters to the function. ``def`` is a
keyword and ``f`` is the name of the function and ``x`` the parameter
of the function.  

The second line is the body of the function. Incidentally, this
function has a single line definition. Note that the body of the
function is indented, just like any other code block, in Python.

{{{ switch to next slide, problem statement 1 }}}

%% 1 %% Write a python function named cube which computes the cube of
   a given number n.

Pause here and try to solve the problem yourself.

{{{ switch to next slide, solution }}}

The problem can be solved as,
::

    def cube(n):
    	return n**3

Now let us see how to write functions without arguments.

{{{ switch to next slide, greet function }}}

Let us define a new function called ``greet`` which will print ``Hello
World``.
::

    def greet():
    	print "Hello World!"

Now try calling the function,
::

    greet()

Well that is a function which takes no arguments. Also note that it is
not mandatory for a function to return values. The function ``greet``
neither takes any argument nor returns any value.

Now let us see how to write functions with more than one argument.

{{{ switch to next slide, exercise 2 }}}

%% 2 %% Write a python function named ``avg`` which computes the
   average of ``a`` and ``b``.

Pause here and try to solve the problem yourself.

{{{ switch to next slide, solution 2 }}}

The problem can be solved as shown,
::

    def avg(a,b):
    	return (a + b)/2

Thus if we want a function to accept more arguments, we just list them
separated with a comma between the parenthesis after the function name
in the ``def`` line.

{{{ switch to next slide, docstring }}}

It is always a good practice to document the code that we write, and
for a function we define we should write an abstract of what the
function does, and that is called a doc-string. Let us modify the
function ``avg`` and add doc-string to it. Do the following,
::

    def avg(a,b):
        """ avg takes two numbers as input (a & b), and
	returns the average of a and b"""
	return (a+b)/2

Note that doc-strings are entered in the immediate line after the
function definition and put as a triple quoted string. And here as far
as the code functionality is concerned, we didn't do anything. We just
added an abstract of what the function does.

Now try this in the ipython interpreter.
::

    avg?

It displays the docstring as we gave it. Thus docstring is a good way
of documenting the function we write.

Try to do this,
::

    f?

It doesn't have a docstring associated with it. Also we cannot infer
anything from the function name, and thus we are forced to read the
code to understand anything about the function.

{{{ switch to next slide, exercise 3 }}}

%% 3 %% Add docstring to the function f.

Pause here and try to do it yourself.

{{{ switch to next slide, solution }}}

We need to define the function again to add docstring to the function
``f`` and we do it as,
::

    def f(x):
    	"""Accepts a number x as argument and,
	returns the square of the number x."""
	return x*x

{{{ switch to next slide, exercise 4 }}}

%% 4 %% Write a python function named ``circle`` which returns the
   area and perimeter of a circle given radius ``r``.

Pause here and try to solve the problem yourself.

{{{ switch to next slide, solution 4 }}}

The problem requires us to return two values instead of one which we
were doing till now. We can solve the problem as,
::

    def circle(r):
    	"""returns area and perimeter of a circle given radius r"""
	pi = 3.14
	area = pi * r * r
	perimeter = 2 * pi * r
	return area, perimeter

A python function can return any number of values. There is no
restriction for it.

Let us call the function ``circle`` as,
::

    a, p = circle(6)
    print a
    print p

Now we have done enough coding, let us do some code reading exercise,

{{{ switch to next slide, what }}}

What does the function ``what`` do?

.. def what( n ):
..     if n < 0: n = -n
..     while n > 0:
..         if n % 2 == 1:
..             return False
..         n /= 10
..     return True

Pause here and try to figure out what the function ``what`` does.

{{{ switch to next slide, even_digits }}}

.. def even_digits( n ):
..    """returns True if all the digits of number n is even
..    returns False if all the digits of number n is not even"""
..     if n < 0: n = -n
..     while n > 0:
..         if n % 2 == 1:
..             return False
..         n /= 10
..     return True

The function returns ``True`` if all the digits of the number ``n``
are even, otherwise it returns ``False``.

Now one more code reading exercise,

{{{ switch to next slide, what }}}

What does the function ``what`` do?

.. def what( n ):
..     i = 1
..     while i * i < n:
..         i += 1
..     return i * i == n, i

Pause here and try to figure out what the function ``what`` does.

{{{ switch to next slide, is_perfect_square }}}

.. def is_perfect_square( n ):
..     """returns True and square root of n, if n is a perfect square,
..     otherwise returns False and the square root of the 
..     next perfect square"""
..     i = 1
..     while i * i < n:
..         i += 1
..     return i * i == n, i


The function returns ``True`` and the square root of ``n`` if n is a
perfect square, otherwise it returns ``False`` and the square root of
the next perfect square.

This brings us to the end of this tutorial, in this tutorial we covered

{{{ switch to next slide, summary }}}

- Functions in Python
- Passing parameters to a function
- Returning values from a function

We also did few code reading exercises.

{{{ Show the "sponsored by FOSSEE" slide }}}

This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India

Hope you have enjoyed and found it useful.
Thank you!