WEBVTT

00:00.720 --> 00:06.540
Until this point we've only ever had our code in our main.

00:06.810 --> 00:12.310
And I said before that main is what is called a function.

00:12.720 --> 00:15.540
Well we can make our own functions as well.

00:15.540 --> 00:18.950
So let's see how you can do that.

00:18.960 --> 00:29.670
So the very first step always is to declare our function so that anything below can use it.

00:29.780 --> 00:40.010
So we're in cellos are always going to have our declarations above mean usually near the top of the

00:40.010 --> 00:40.900
file.

00:40.910 --> 00:43.570
That's where I like to put them.

00:43.580 --> 00:47.220
So let's take a look at this little bit.

00:47.240 --> 00:53.460
So the first thing you'll see is what is called the return value of a function.

00:53.480 --> 01:03.470
So here it's this weird thing called void and what void means in this case is that there is no return

01:03.470 --> 01:06.280
value for this function.

01:06.350 --> 01:14.230
It doesn't have to be or it could be something like an int or a char or any type you've seen so far.

01:14.630 --> 01:18.710
But we'll talk about return values that later.

01:18.720 --> 01:25.380
So the next thing is the name of the function and we get to choose what that name is.

01:25.380 --> 01:34.880
So this name again remember that it's also an identifier so names have the same restrictions as variables

01:34.880 --> 01:37.180
so you can have a.

01:37.250 --> 01:46.310
So you have to have a letter as the first character but after that it can be digits or letters or underscores.

01:46.310 --> 01:56.540
So for this course our names are always have capital letters as first letter of the function and each

01:56.540 --> 02:00.490
subsequent word will also be capitalized.

02:00.650 --> 02:03.240
But everything else will be lower case like this.

02:05.280 --> 02:09.390
So the next part is the parentheses.

02:09.570 --> 02:14.050
And this indicates the parameters or arguments of the function.

02:14.100 --> 02:19.840
So parameters are variables that can be passed into the function from outside sources.

02:20.420 --> 02:24.060
We'll be looking at this a bit later as well.

02:24.120 --> 02:31.470
So since it's a statement there's always a semi-colon at the end so always remember that this declaration

02:31.470 --> 02:35.140
is a statement.

02:35.210 --> 02:41.110
So this whole line is called a the function signature.

02:41.390 --> 02:49.530
So the function signature consists of the parameters the name of the function and the return type is

02:49.550 --> 02:51.150
the return type.

02:51.360 --> 02:55.070
K. so will be.

02:55.230 --> 03:02.770
You'll see why I say Funt function signature and why a function signature is important in a leader lecture

03:03.150 --> 03:05.900
in this section.

03:05.930 --> 03:10.010
So the next thing we need to do is actually define this function.

03:10.010 --> 03:12.830
So for our definition

03:15.280 --> 03:18.390
we're just kind of say hello.

03:18.550 --> 03:21.860
And notice that I'm rewriting the function signature here.

03:21.970 --> 03:24.420
Same as above.

03:24.700 --> 03:33.890
This K and we have the source code blocker statement block OK.

03:34.280 --> 03:38.790
And this is where we actually define what the code should be for this function.

03:38.780 --> 03:44.080
So all we're going to have here is a hello world that's it.

03:45.850 --> 03:50.930
And so we've defined our function now.

03:59.430 --> 04:04.470
So the last thing to do is just to call the function.

04:04.590 --> 04:10.830
So all we need to do is actually say we don't need the return type anymore more we just have to say

04:10.890 --> 04:18.030
print hello and then pass in whatever parameters we would have which we have none.

04:18.030 --> 04:21.320
In this case and then the semi-colon.

04:21.600 --> 04:22.400
OK.

04:22.720 --> 04:33.150
So Main here its main function is considered the color of the print Telo function.

04:33.180 --> 04:39.770
So just remember that anyone who calls this is what's called calling a function or using the function

04:39.780 --> 04:40.380
right.

04:40.770 --> 04:45.420
So this is what's called calling the function and Main is the caller.

04:45.750 --> 04:48.990
And print hello would be considered the call Lee.

04:49.050 --> 04:59.610
So if you ever hear me say the caller I'm really talking about the caller of the function.

04:59.770 --> 05:02.610
So let's run this and see what happens.

05:03.480 --> 05:06.460
So print out Hello world as we would expect.

05:06.460 --> 05:11.810
So basically we defined our function.

05:13.750 --> 05:24.520
And since Maine is the starting point man calls this function the control flow switches to this print

05:24.520 --> 05:26.360
Telo function.

05:26.420 --> 05:32.330
It does everything it needs to do and the code does all the steps.

05:32.390 --> 05:36.560
There's only one in this case this number 26.

05:36.560 --> 05:47.110
And then this prints hello world and then the control flow shifts back to main and it exits that way.

05:47.220 --> 05:51.630
That's kind of how the flow of function calls work.

05:52.370 --> 05:54.640
It's very important to understand.

05:54.810 --> 06:01.970
And in this case it is very simple and doesn't really do much but it illustrates how functions really

06:01.970 --> 06:11.300
work and so functions are really useful for logically breaking up the code into smaller and easier to

06:11.300 --> 06:12.860
understand chunks.

06:12.980 --> 06:16.330
And we'll be using them basically from now on.

06:16.540 --> 06:21.650
And all of our games will be designed using functions.

06:21.650 --> 06:26.870
So we'll be talking a lot more about functions in the future.

06:27.080 --> 06:31.090
So in the next lecture I want to talk about parameters to functions.
