1
00:00:00,000 --> 00:00:04,990
One very common way to reuse code in Python is the use of a function, so let's

2
00:00:05,000 --> 00:00:07,990
take a look at how functions are used in Python.

3
00:00:08,000 --> 00:00:11,990
We'll go ahead and make a working copy of function.py.

4
00:00:12,000 --> 00:00:14,990
I'm going to call this function-working.py.

5
00:00:15,000 --> 00:00:18,990
Save that and open it up.

6
00:00:19,000 --> 00:00:23,990
Then I'll go ahead and maximize this, so we can look at this code.

7
00:00:24,000 --> 00:00:25,990
Here, we have a function definition.

8
00:00:26,000 --> 00:00:30,990
def is the keyword. It says this is going to define a function.

9
00:00:31,000 --> 00:00:35,990
isprime is the name of the function, and n, in this case, is a parameter.

10
00:00:36,000 --> 00:00:39,990
We can have multiple parameters, separated by commas.

11
00:00:40,000 --> 00:00:41,990
We'll get into more of the details of how this works.

12
00:00:42,000 --> 00:00:42,990
This is the Quick Start.

13
00:00:43,000 --> 00:00:46,990
It's going to show you in a nutshell, if you're an experienced programmer, how

14
00:00:47,000 --> 00:00:47,990
to create functions.

15
00:00:48,000 --> 00:00:50,990
We'll get into the details later on in the course.

16
00:00:51,000 --> 00:00:53,990
In this case, this is a simple function for testing if a number is a

17
00:00:54,000 --> 00:00:54,990
prime number or not.

18
00:00:55,000 --> 00:00:56,990
It checks if it's 1.

19
00:00:57,000 --> 00:01:01,990
It checks if it's divisible evenly by other factors, and otherwise it says,

20
00:01:02,000 --> 00:01:04,990
this is a prime number.

21
00:01:05,000 --> 00:01:10,990
So all of this code that is the body of the function is indented, as is how

22
00:01:11,000 --> 00:01:12,990
blocks of code are done in Python.

23
00:01:13,000 --> 00:01:14,990
And it's introduced with this colon.

24
00:01:15,000 --> 00:01:19,990
So you have the def, the name of the function, parenthesis, any parameters

25
00:01:20,000 --> 00:01:26,990
inside the parenthesis, and a colon, and then indented code for the body of the function.

26
00:01:27,000 --> 00:01:27,990
Down here, we call it.

27
00:01:28,000 --> 00:01:29,990
Here, we're calling it inside a for loop.

28
00:01:30,000 --> 00:01:34,990
And we simply call it by referencing the name and putting any parameters inside.

29
00:01:35,000 --> 00:01:39,990
Now in this case, the parameter is a variable called n, which happens to be the

30
00:01:40,000 --> 00:01:41,990
same as what I call it up here.

31
00:01:42,000 --> 00:01:46,990
We could have used an x, or any other legal variable name down here, but just so

32
00:01:47,000 --> 00:01:50,990
you know that this n is different than this n.

33
00:01:51,000 --> 00:01:55,990
All right, so let's go ahead and run it.

34
00:01:56,000 --> 00:01:59,990
We'll select Run up here and Python Run.

35
00:02:00,000 --> 00:02:05,990
And there we have a list of all of these prime numbers.

36
00:02:06,000 --> 00:02:06,990
So 1 is special.

37
00:02:07,000 --> 00:02:07,990
2 is a prime number.

38
00:02:08,000 --> 00:02:09,990
3 is a prime number.

39
00:02:10,000 --> 00:02:15,990
4 is not a prime number, because it's divisible evenly by 2, and et cetera,

40
00:02:16,000 --> 00:02:20,990
et cetera, prime numbers and not prime numbers.

41
00:02:21,000 --> 00:02:25,990
So this function is getting called over and over inside of this loop.

42
00:02:26,000 --> 00:02:27,990
And we only have to have the code there once.

43
00:02:28,000 --> 00:02:31,990
And that, of course, is the value of a function or a subroutine.

44
00:02:32,000 --> 00:02:35,990
So that's how functions are defined and used in Python.

45
00:02:36,000 --> 00:02:39,990
And of course, they're used quite a bit, and we'll see a lot more examples of

46
00:02:40,000 --> 00:02:50,000
this as we go through the course.

