equal
deleted
inserted
replaced
21 |
21 |
22 |
22 |
23 .. Author : Anoop Jacob Thomas <anoop@fossee.in> |
23 .. Author : Anoop Jacob Thomas <anoop@fossee.in> |
24 Internal Reviewer : |
24 Internal Reviewer : |
25 External Reviewer : |
25 External Reviewer : |
|
26 Language Reviewer : Bhanukiran |
26 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
27 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
27 |
28 |
28 |
29 |
29 ============================== |
30 ============================== |
30 Getting started with functions |
31 Getting started with functions |
42 |
43 |
43 {{{ switch to next slide, Function }}} |
44 {{{ switch to next slide, Function }}} |
44 |
45 |
45 While writing code, we always want to reduce the number of lines of |
46 While writing code, we always want to reduce the number of lines of |
46 code and functions is a way of reusing the code. Thus the same lines |
47 code and functions is a way of reusing the code. Thus the same lines |
47 of code can be used again and again. A function is a portion of code |
48 of code can be used as many times as needed. A function is a portion of code |
48 within a larger program that performs a specific task and is |
49 within a larger program that performs a specific task and is |
49 relatively independent of the remaining code. Now let us get more |
50 relatively independent of the remaining code. Now let us get more |
50 familiar with functions, |
51 familiar with functions, |
51 |
52 |
52 {{{ switch to next slide, f(x) a mathematical function }}} |
53 {{{ switch to next slide, f(x) a mathematical function }}} |
69 :: |
70 :: |
70 |
71 |
71 f(1) |
72 f(1) |
72 f(2) |
73 f(2) |
73 |
74 |
74 Yes, it returned 1 and 2 respectively. And now let us see what we did, |
75 Yes, it returned 1 and 4 respectively. And now let us see what we did. |
75 we wrote two lines. The first line ``def f(x)`` is used to define the |
76 We wrote two lines: The first line ``def f(x)`` is used to define the |
76 name and the parameters to the function. ``def`` is a keyword and |
77 name and the parameters to the function and the second line is used to |
|
78 fix what the function is supposed to return. ``def`` is a keyword and |
77 ``f`` is the name of the function and ``x`` the parameter of the |
79 ``f`` is the name of the function and ``x`` the parameter of the |
78 function. |
80 function. |
79 |
81 |
80 {{{ switch to next slide, problem statement 1 }}} |
82 {{{ switch to next slide, problem statement 1 }}} |
81 |
83 |
128 |
130 |
129 def avg(a,b): |
131 def avg(a,b): |
130 return (a + b)/2 |
132 return (a + b)/2 |
131 |
133 |
132 Thus if we want a function to accept more arguments, we just list them |
134 Thus if we want a function to accept more arguments, we just list them |
133 separated with a comma between the parenthesis after the function name |
135 separated with a comma between the parenthesis after the function's name |
134 in the ``def`` line. |
136 in the ``def`` line. |
135 |
137 |
136 {{{ switch to next slide, docstring }}} |
138 {{{ switch to next slide, docstring }}} |
137 |
139 |
138 It is always a good practice to document the code that we write, and |
140 It is always a good practice to document the code that we write, and |
164 |
166 |
165 f? |
167 f? |
166 |
168 |
167 It doesn't have a docstring associated with it. Also we cannot infer |
169 It doesn't have a docstring associated with it. Also we cannot infer |
168 anything from the function name, and thus we are forced to read the |
170 anything from the function name, and thus we are forced to read the |
169 code to understand anything about the function. |
171 code to understand about the function. |
170 |
172 |
171 {{{ switch to next slide, exercise 3 }}} |
173 {{{ switch to next slide, exercise 3 }}} |
172 |
174 |
173 %% 3 %% Add docstring to the function f. |
175 %% 3 %% Add docstring to the function f. |
174 |
176 |
232 Pause here and try to figure out what the function ``what`` does. |
234 Pause here and try to figure out what the function ``what`` does. |
233 |
235 |
234 {{{ switch to next slide, even_digits }}} |
236 {{{ switch to next slide, even_digits }}} |
235 |
237 |
236 .. def even_digits( n ): |
238 .. def even_digits( n ): |
237 .. """returns True if all the digits of number n is even |
239 .. """returns True if all the digits in the number n are even, |
238 .. returns False if all the digits of number n is not even""" |
240 .. returns False if all the digits in the number n are not even""" |
239 .. if n < 0: n = -n |
241 .. if n < 0: n = -n |
240 .. while n > 0: |
242 .. while n > 0: |
241 .. if n % 2 == 1: |
243 .. if n % 2 == 1: |
242 .. return False |
244 .. return False |
243 .. n /= 10 |
245 .. n /= 10 |