1
00:00:00,000 --> 00:00:05,990
Functions, or subroutines, are a very common form of reusable code, so let's look

2
00:00:06,000 --> 00:00:08,990
at how functions are done in Python.

3
00:00:09,000 --> 00:00:11,990
We'll make a working copy of syntax.py.

4
00:00:12,000 --> 00:00:17,990
We'll call this syntax-functions.py.

5
00:00:18,000 --> 00:00:18,990
Go ahead and open our working copy.

6
00:00:19,000 --> 00:00:23,990
We'll notice, of course, that main here is a function.

7
00:00:24,000 --> 00:00:27,990
And the purpose of that is not so much reusability as it is modularity in

8
00:00:28,000 --> 00:00:32,990
allowing us to create more functions, and be able to run them from main.

9
00:00:33,000 --> 00:00:38,990
So let's go ahead and create a function here.

10
00:00:39,000 --> 00:00:41,990
And I'll just call it func.

11
00:00:42,000 --> 00:00:51,990
And we'll print out a range of numbers.

12
00:00:52,000 --> 00:00:56,990
And in my print statement I am going to say end= ' '.

13
00:00:57,000 --> 00:01:00,990
And that way instead of putting each number on a separate line, we'll put them

14
00:01:01,000 --> 00:01:02,990
all in the same line, separated by spaces.

15
00:01:03,000 --> 00:01:08,990
And I'll put another print after it, an empty one to print a new line.

16
00:01:09,000 --> 00:01:13,990
And so I'll save that, and I'll call the function from in main.

17
00:01:14,000 --> 00:01:18,990
And we'll just call it instead of this print function here, we'll just say func.

18
00:01:19,000 --> 00:01:20,990
And that's all we need to do to call our function.

19
00:01:21,000 --> 00:01:23,990
So we'll save it and run.

20
00:01:24,000 --> 00:01:27,990
And we see we are printing out this sequence of 10 numbers.

21
00:01:28,000 --> 00:01:30,990
And this is, in fact, how the range function works.

22
00:01:31,000 --> 00:01:34,990
It prints out everything not including the number of the range.

23
00:01:35,000 --> 00:01:38,990
So it's 10 numbers staring at 0.

24
00:01:39,000 --> 00:01:42,990
Now if I wanted to call the function again, I would just say func again, and I can

25
00:01:43,000 --> 00:01:44,990
do that several times.

26
00:01:45,000 --> 00:01:47,990
And each time it will run this same block of code.

27
00:01:48,000 --> 00:01:52,990
So I save it and I run it, and that's how a function works.

28
00:01:53,000 --> 00:01:54,990
So let's look at how we define the function.

29
00:01:55,000 --> 00:01:57,990
The def keyword is for defining the function.

30
00:01:58,000 --> 00:02:00,990
That means that what comes next is going to be the name of the function, so

31
00:02:01,000 --> 00:02:04,990
there is the name, and then there is a set of parentheses where we would put

32
00:02:05,000 --> 00:02:06,990
arguments, or parameters.

33
00:02:07,000 --> 00:02:10,990
And the colon says that this is going to be followed by a block of code, or a

34
00:02:11,000 --> 00:02:13,990
suite of codes, as it's called in Python.

35
00:02:14,000 --> 00:02:17,990
And then we have the suite of code itself.

36
00:02:18,000 --> 00:02:21,990
The suite of code is indented under the definition of the function, and in this

37
00:02:22,000 --> 00:02:28,990
case of course, we're using the traditional four spaces of indenting, like that.

38
00:02:29,000 --> 00:02:32,990
Inside the parentheses we could put arguments.

39
00:02:33,000 --> 00:02:39,990
So for example, if I put an argument called a there, then I could say a, 10.

40
00:02:40,000 --> 00:02:46,990
So it would start at the number a in its range here, and so here I could use a 1,

41
00:02:47,000 --> 00:02:49,990
and here I could use a 3, and here I could use a 5.

42
00:02:50,000 --> 00:02:53,990
And it would use that as an argument to the range function.

43
00:02:54,000 --> 00:02:57,990
What that does is it defines a starting point for the range.

44
00:02:58,000 --> 00:03:01,990
And so if I save that and run it, then we see we have one that starts with 1, one

45
00:03:02,000 --> 00:03:04,990
that starts with 3, and one that starts with 5.

46
00:03:05,000 --> 00:03:11,990
And so this argument in the parentheses is used inside the function in this way.

47
00:03:12,000 --> 00:03:16,990
We can give the argument a default value by saying a=0.

48
00:03:17,000 --> 00:03:21,990
Well, let's give it something different so we can see. Let's say a=7.

49
00:03:22,000 --> 00:03:26,990
And if in this middle one I don't put an argument, then that will be the default

50
00:03:27,000 --> 00:03:27,990
argument that's used.

51
00:03:28,000 --> 00:03:29,990
It'll default to 7.

52
00:03:30,000 --> 00:03:34,990
So if I save this and run it, you see that the middle one starts at 7.

53
00:03:35,000 --> 00:03:38,990
So there are a lot of other things we can do with functions and with function

54
00:03:39,000 --> 00:03:41,990
arguments, and we'll cover those later in our chapter on functions.

55
00:03:42,000 --> 00:03:46,990
But this is, in a nutshell, how you define a function in Python, and how you

56
00:03:47,000 --> 00:03:51,990
call and use that function, and how you can use arguments when you call and

57
00:03:52,000 --> 00:04:02,000
use that function.

