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 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
26 Language Reviewer : Bhanukiran |
|
27 Checklist OK? : <put date stamp here, not OK> [2010-10-05] |
27 |
28 |
28 |
29 |
29 ============================== |
30 ============================== |
30 Getting started with functions |
31 Getting started with functions |
31 ============================== |
32 ============================== |
32 |
33 |
33 {{{ show welcome slide }}} |
34 {{{ show welcome slide }}} |
34 |
35 |
35 Hello and welcome to the tutorial, getting started with functions. |
36 Hello and welcome to the tutorial getting started with functions. |
36 |
37 |
37 {{{ switch to next slide, outline slide }}} |
38 {{{ switch to next slide, outline slide }}} |
38 |
39 |
39 In this tutorial we will learn about functions in python, how to |
40 In this tutorial we will learn about functions in python, how to |
40 define functions, passing arguments to functions, docstrings, and |
41 define functions, arguments to functions, docstrings, and function |
41 function return value. |
42 return value. |
42 |
43 |
43 {{{ switch to next slide, Function }}} |
44 {{{ switch to next slide, Function }}} |
44 |
45 |
45 While writing code, we would like to reduce the number of lines of |
46 While writing code, we always want to reduce the number of lines of |
46 code and using functions is a way of reusing the code. A function is |
47 code and functions is a way of reusing the code. Thus the same lines |
47 a portion of code within a larger program that performs a specific |
48 of code can be used as many times as needed. A function is a portion of code |
48 task and is relatively independent of the remaining code. Now let us |
49 within a larger program that performs a specific task and is |
49 get more familiar with functions, |
50 relatively independent of the remaining code. Now let us get more |
|
51 familiar with functions, |
50 |
52 |
51 {{{ switch to next slide, f(x) a mathematical function }}} |
53 {{{ switch to next slide, f(x) a mathematical function }}} |
52 |
54 |
53 Consider a mathematical function f(x) = x squared. Here x is a |
55 Consider a mathematical function f(x) = x square. Here x is a variable |
54 variable and with different values of x the value of function will |
56 and with different values of x the value of function will change. When |
55 change. When x is one f(1) will return the value 1 and f(2) will |
57 x is one f(1) will return the value 1 and f(2) will return us the |
56 return us the value 4. Let us now see how to define the function f(x) |
58 value 4. Let us now see how to define the function f(x) in python. |
57 in Python. |
|
58 |
59 |
59 {{{ switch to next slide, define f(x) in Python }}} |
60 {{{ switch to next slide, define f(x) in Python }}} |
60 |
61 |
61 In your Ipython interpreter type the following, |
62 In your Ipython interpreter type the following, |
62 :: |
63 :: |
63 |
64 |
64 def f(x): |
65 def f(x): |
65 return x*x |
66 return x*x |
66 |
67 |
67 Let us see, if the function ``f``, we defined, works as we expect. |
68 Well that defined the function, so before learning what we did let us |
|
69 see if it returns the expected values, try, |
68 :: |
70 :: |
69 |
71 |
70 f(1) |
72 f(1) |
71 f(2) |
73 f(2) |
72 |
74 |
73 Yes, it returned 1 and 2 respectively. |
75 Yes, it returned 1 and 4 respectively. And now let us see what we did. |
74 |
76 We wrote two lines: The first line ``def f(x)`` is used to define the |
75 Now, let us see what we did. The first line ``def f(x)`` is used to |
77 name and the parameters to the function and the second line is used to |
76 define the name and the parameters to the function. ``def`` is a |
78 fix what the function is supposed to return. ``def`` is a keyword and |
77 keyword and ``f`` is the name of the function and ``x`` the parameter |
79 ``f`` is the name of the function and ``x`` the parameter of the |
78 of the function. |
80 function. |
79 |
|
80 The second line is the body of the function. Incidentally, this |
|
81 function has a single line definition. Note that the body of the |
|
82 function is indented, just like any other code block, in Python. |
|
83 |
81 |
84 {{{ switch to next slide, problem statement 1 }}} |
82 {{{ switch to next slide, problem statement 1 }}} |
85 |
83 |
86 %% 1 %% Write a python function named cube which computes the cube of |
84 %% 1 %% Write a python function named cube which computes the cube of |
87 a given number n. |
85 a given number n. |
125 |
123 |
126 Pause here and try to solve the problem yourself. |
124 Pause here and try to solve the problem yourself. |
127 |
125 |
128 {{{ switch to next slide, solution 2 }}} |
126 {{{ switch to next slide, solution 2 }}} |
129 |
127 |
130 The problem can be solved as shown, |
128 The problem can be solved as, |
131 :: |
129 :: |
132 |
130 |
133 def avg(a,b): |
131 def avg(a,b): |
134 return (a + b)/2 |
132 return (a + b)/2 |
135 |
133 |
136 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 |
137 separated with a comma between the parenthesis after the function name |
135 separated with a comma between the parenthesis after the function's name |
138 in the ``def`` line. |
136 in the ``def`` line. |
139 |
137 |
140 {{{ switch to next slide, docstring }}} |
138 {{{ switch to next slide, docstring }}} |
141 |
139 |
142 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 |
143 for a function we define we should write an abstract of what the |
141 for a function we define we should write an abstract of what the |
144 function does, and that is called a doc-string. Let us modify the |
142 function does, and that is called a docstring. Let us modify the |
145 function ``avg`` and add doc-string to it. Do the following, |
143 function ``avg`` and add docstring to it. Do the following, |
146 :: |
144 :: |
147 |
145 |
148 def avg(a,b): |
146 def avg(a,b): |
149 """ avg takes two numbers as input (a & b), and |
147 """ avg takes two numbers as input (a & b), and |
150 returns the average of a and b""" |
148 returns the average of a and b""" |
151 return (a+b)/2 |
149 return (a+b)/2 |
152 |
150 |
153 Note that doc-strings are entered in the immediate line after the |
151 Note that docstrings are entered in the immediate line after the |
154 function definition and put as a triple quoted string. And here as far |
152 function definition and put as a triple quoted string. And here as far |
155 as the code functionality is concerned, we didn't do anything. We just |
153 as the code functionality is concerned, we didn't do anything. We just |
156 added an abstract of what the function does. |
154 added an abstract of what the function does. |
157 |
155 |
158 Now try this in the ipython interpreter. |
156 Now try this in the ipython interpreter. |