Skip to content

Commit 7618c9d

Browse files
committed
Added tutorial
1 parent 6874e6f commit 7618c9d

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

python/lesson3/tutorial.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
layout: page
3+
title: Fun with Functions
4+
---
5+
6+
In this tutorial we are going to look at what functions are, how we use them and why.
7+
8+
### Calling a fuction
9+
10+
A function is a collection of processes grouped together, that can be called later to run these processes. Python comes with plenty of functions already built in that you can use to perform common tasks. One example would be printing to the screen.
11+
12+
>>> print "This is a function"
13+
This is a function
14+
15+
It is also possible to store the output of a function in a variable for future use. Let's try this with the built in function range(), which will provide us with a list of numbers.
16+
17+
>>> list_of_numbers = range(1, 4)
18+
>>> print list_of_numbers
19+
[1, 2, 3]
20+
21+
You may have noticed that we specified the use of the function range() for our particular situation. We provided the numbers 1 and 4 as parameters to the function, a start number and a stop number. These parameters let the function know where to start the number list, and where to end it (in the case of range, it provides a list of numbers up to, but not including the stop number).
22+
23+
We can also chain functions to use the output of other functions as parameters. In the earlier example, we stored the list provided by the range function in a variable, and then passed that variable as a parameter for the print function, which then print the list of numbers to the screen. We could also use the range function as a direct parameter for the print function.
24+
25+
>>> print range(1, 4)
26+
[1, 2, 3]
27+
28+
This can be very useful in making code more concise, but using it too much could make code harder to read. Knowing when to use variables instead of chaining functions comes with experience; in reading and in writing code.
29+
30+
Try creating your own lists of numbers using range() with different parameters. Looking at the [documentation](https://docs.python.org/2/library/functions.html#range "Python 2 documentation for range") is always a good idea when learning more about the language. You can also learn more by purposefully trying to break the function. What happens when you don't provide any parameters?
31+
32+
### Defining your own functions
33+
34+
Now we are going to create our own function. To do this we use the def keyword, provide a unique (and descriptive) name for the function, and specify any parameters that may be accepted by the function. Then we write the code we want to be run when the function is called. Let's start with a simple function that takes no parameters.
35+
36+
>>> def print_name():
37+
... print "My name is Bart"
38+
39+
We can then call this function like we would any other.
40+
41+
>>> print_name()
42+
My name is Bart
43+
44+
We can change the function to use a parameter. Let's change it so that it allows the user to specify the name they want to print.
45+
46+
>>> def print_name(name):
47+
... print "My name is " + name
48+
...
49+
>>> print_name("Lisa")
50+
My name is Lisa
51+
52+
You may have noticed that the code inside the function is indented. This is to let the Python interpreter know what is part of the function we are defining, and what is not. That is why when we write print_name("Lisa") directly after, it knows to call the function and not include it in the function definition, it is not indented.
53+
54+
Now that we have defined a parameter, the function will raise an error if no parameter is provided. Try it yourself, it's good to get used to understanding how Python errors work and what they mean. It's an important skill when debugging more complex code.
55+
56+
We can enhance the function to run with a default name if one is not provided. Let's do that by specifying a default value.
57+
58+
>>> def print_name(name='Bart'):
59+
... print "My name is " + name
60+
...
61+
>>> print_name("Lisa")
62+
My name is Lisa
63+
>>> print_name()
64+
My name is Bart
65+
66+
67+
### Different kinds of functions
68+
69+
A function can be put into one of two categories. Fruitful functions, and void functions. So far we have only created void functions, these are functions that perform tasks but don't return a value. Fruitful functions return a value, a familiar example is the range function we used earlier that returned a list. Let's change our function from void to fruitful.
70+
71+
>>> def print_name(name='Bart'):
72+
... return "My name is " + name
73+
74+
Now the function returns a value that we can work with. We could store this in a value to be used later. Let's try storing it as a value, and using that value as a parameter in another print_name function call.
75+
76+
>>> printed_name = print_name("Lisa")
77+
>>> print print_name(printed_name)
78+
My name is My name is Lisa
79+
80+
Notice that we now need to use the print function to print to the screen. While the REPL will print what the function returns to the console, if run as a script, simply calling the function without a print statement would not print anything to the screen. This is because the function is returning a value, and no longer printing a string.
81+
82+
A subtle indicator of this is the quote marks around the sentance that denote a string is being output to the screen, rather than something being printed with the print function.
83+
84+
>>> printed_name = print_name("Lisa")
85+
>>> print_name(printed_name)
86+
'My name is My name is Lisa'
87+
88+
### Why use functions
89+
90+
At first glance it may not seem obvious why it is worth expending the extra effort of defining and calling a function, rather than just writing the code independant of such things. Defining tasks as functions reduces the need to copy and paste the same code multiple times to achieve the same effect. Simply calling the function multiple times makes it much easier to not only write code, but to read it also.
91+
92+
Using functions also makes it a lot easier to fix and change code. If you are performing the same tasks in mutiple places and discover a bug, without functions you would need to fix the same bug multiple times. However with functions, you simply fix the bug once, and all of the subsequent function calls will now behave accordingly.
93+
94+
95+
### Further reading
96+
97+
Python comes with a lot of built in functions that are worth learning about, as they have been created specifically to perform the most common tasks that developers require. They are listed in the [documentation](https://docs.python.org/2/library/functions.html "Python 2 built in functions documentation"). To begin with, try using a few to see what they do, and how they behave with varying parameters. Once you have grasped how a few of them work, try writing your own functions that call the built in functions within them, like we did when we used the print function in our own custom print_name function.

0 commit comments

Comments
 (0)