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