33 definition. 'welcome' is the name of the function and 'name' is |
33 definition. 'welcome' is the name of the function and 'name' is |
34 the lone argument to the function. Note that the function is |
34 the lone argument to the function. Note that the function is |
35 defined within an indented block, similar to any other block. Our |
35 defined within an indented block, similar to any other block. Our |
36 function welcome just has one line in it's definition. |
36 function welcome just has one line in it's definition. |
37 |
37 |
38 We can call our function by passing, as follows - |
38 We can call our function, as follows - |
39 welcome("World") |
39 welcome("World") |
40 |
40 |
41 (arguments are local to a function) |
41 (all arguments are local to a function) |
42 |
42 |
43 In general functions should be accompanied by documentation on how |
43 In general functions should be accompanied by documentation on how |
44 to use it. Python provides a convenient way of writing within the |
44 to use them. Python provides a convenient way of writing this within the |
45 function itself, using doc strings. They were mentioned in the |
45 function itself, using what are called doc strings. They were mentioned in the |
46 tutorial on strings. Let's look at how to write them here. |
46 tutorial on strings. Let's look at how to write them here. |
47 |
47 |
48 Let us add a simple doc string to our welcome function. |
48 Let us add a simple doc string to our welcome function. |
49 |
49 |
50 def welcome(name): |
50 def welcome(name): |
51 """ Prints a hello message to a person, |
51 """ Prints a hello message given a name, |
52 given a name. """ |
52 passed as argument. """ |
53 print "Hello", name |
53 print "Hello", name |
54 |
54 |
55 Notice that the doc string uses triple quotes. If the doc-string |
55 Notice that the doc string uses triple quotes. If the doc-string |
56 exceeded one line, we could have used new line characters in |
56 exceeds one line, we can use new line characters in it. |
57 it. Also, as expected the doc-string is indented as is required |
57 Also, as expected the doc-string is indented as is required |
58 for anything within a block. |
58 for anything within a block. |
59 |
59 |
60 We shall now look at default arguments. |
60 We shall now look at default arguments. |
61 [show slide with examples of functions with default arguments] |
61 [show slide with examples of functions with default arguments] |
62 The split function has been used in two different ways in the |
62 The split function has been used in two different ways in the |
63 previous tutorials - one for splitting on spaces and the other for |
63 previous tutorials - one for splitting on spaces and the other for |
64 splitting on commas. |
64 splitting on semicolons. |
65 |
65 |
66 The function split is being called with no arguments and one |
66 The function split is being called with no arguments and one |
67 argument, respectively. In the first case, white space is being |
67 argument, respectively. In the first case, white space is being |
68 used as a default value. Let's now edit our function, welcome, to |
68 used as a default value. Let's now edit our function, welcome, to |
69 use default values. (For convenience, we have dropped the doc-string) |
69 use default values. (For convenience sake, we have dropped the doc-string) |
70 |
70 |
71 def welcome(name="World!"): |
71 def welcome(name="World!"): |
72 print "Hello", name |
72 print "Hello", name |
73 |
73 |
74 Now, we call the function 'welcome' without passing any arguments |
74 Now, we call the function 'welcome' without passing any arguments |
75 to it. |
75 to it. |
76 welcome() |
76 welcome() |
77 |
77 |
78 "World!" is used as a default argument, when no name argument is |
78 As you can see the output is "Hello World!". Here "World!" is used as a |
79 passed to 'welcome'. |
79 default argument, when no name argument is passed to 'welcome'. |
80 |
80 |
81 Let's now look at the use of keyword arguments. |
81 Let's now look at the use of keyword arguments. |
82 [show slide with examples of functions with keyword arguments] |
82 [show slide with examples of functions with keyword arguments] |
83 We have already looked at functions and keyword arguments in these |
83 We have already looked at functions and keyword arguments in these |
84 examples. loc, linewidth, xy, labels are all keywords. |
84 examples. loc, linewidth, xy, labels are all keywords. |
85 |
85 |
86 Let's now see, how we can call our function 'welcome', using |
86 Let's now customize our function so that it displays a custom |
|
87 greeting message as well. |
|
88 |
|
89 def welcome( greet = 'Hello', name = 'World!'): |
|
90 print greet, name |
|
91 |
|
92 Let's now see, how we can call our updated 'welcome' function, using |
87 keyword arguments. We can call the function in a variety of ways. |
93 keyword arguments. We can call the function in a variety of ways. |
88 welcome("Hello", "James") |
94 welcome("Hello", "James") |
89 welcome("Hi", name="Guido") |
95 welcome("Hi", name="Guido") |
90 welcome(name="Guido", greet="Hello") |
96 welcome(name="Guido", greet="Hello") |
91 |
97 |
92 Keyword arguments allow us to call functions by passing arguments |
98 Keyword arguments allow us to call functions by passing arguments |
93 in any order and removes need to remember the order of arguments |
99 in any order and removes the need to remember the order of arguments |
94 in the function definition. |
100 in the function definition. |
95 |
|
96 Let us look at an example function. This will both act as a code |
|
97 reading exercise and tell us an important point about return |
|
98 values in python's functions. Pause the video for a while and |
|
99 ponder about what this piece of code does. |
|
100 [show slide?] |
|
101 |
101 |
102 def what( n ): |
102 |
103 i = 1 |
103 In Python there is no restriction on the number of values returned by |
104 while i * i < n: |
104 a function. When a function returns more than one value, the multiple |
105 i += 1 |
105 values are packed into one single tuple and that single tuple is returned. |
106 return i * i == n, i |
|
107 |
|
108 The function takes an integer as an argument and returns the |
|
109 boolean True, and the square of the number, if it is a perfect |
|
110 square OR the boolean False and the square root of the next |
|
111 perfect square of the number. |
|
112 Note that this function is returning two values. Functions in |
|
113 Python are capable of returning any number of values. |
|
114 |
|
115 ***** arguments are local to a function |
|
116 |
|
117 Python follows the philosophy of batteries included. It comes |
|
118 with a rich and versatile standard library with whole gamut of |
|
119 functions. Do check out the standard library, before you sit down |
|
120 to write functions of your own. |
|
121 |
106 |
122 We come to the end of this tutorial on functions. In this tutorial |
107 We come to the end of this tutorial on functions. In this tutorial |
123 we have learnt about functions in a greater detail. We looked at |
108 we have learnt about functions in a greater detail. We looked at |
124 how to define functions, calling them, default and keyword |
109 how to define functions, calling them, default and keyword |
125 arguments. |
110 arguments. |