WEBVTT

00:00.480 --> 00:06.230
Now let's talk about functions that can return values.

00:06.470 --> 00:10.360
So let's see an example.

00:10.920 --> 00:18.840
We're going to make a function called Square and all of this will do is square the number that we pass

00:18.840 --> 00:19.740
into it.

00:20.750 --> 00:33.790
And notice here that we have it at the start of the signature and it is the return type of the square

00:33.790 --> 00:34.330
function.

00:34.330 --> 00:41.440
So it's going to return an integer to the color of the square function.

00:41.790 --> 00:45.170
So it's defined square here.

00:46.760 --> 00:54.440
X and all this will do is return back to the caller.

00:54.770 --> 01:01.080
The number that they passed in multiplied by itself.

01:01.820 --> 01:13.850
And to do this we use this return keyword so return once it's executed then it will return the control

01:13.850 --> 01:17.680
flow back to the caller.

01:17.690 --> 01:23.770
So for example if we had more lines of code here K

01:26.960 --> 01:35.580
these would never be executed because we already returned the control flow and this value right.

01:35.640 --> 01:44.360
This this will evaluate to an integer and we'll return back to the caller.

01:44.390 --> 01:49.930
So let's say here say

01:52.050 --> 01:57.450
Square let's say 5 is

02:05.930 --> 02:16.270
can we call it rate here and we can do this because Square really just evaluates to an integer.

02:16.430 --> 02:25.810
So this whole thing will evaluate to basically this down here k.

02:25.990 --> 02:35.660
And since square returns an integer you can just print out at integer just by calling the square function.

02:36.190 --> 02:42.690
So it's just reminiscence the square 5 is 25.

02:42.780 --> 02:44.630
It's not very surprising.

02:44.640 --> 02:54.450
So we're talking about here with that return key word so say we had C out after the return.

02:57.610 --> 02:59.250
If we run that.

02:59.420 --> 03:07.860
So you will never actually see that because return returns the control flow of the program back up to

03:07.890 --> 03:08.640
its caller.

03:08.640 --> 03:21.130
So the caller is Main in this case and after this line it will go back up to here.

03:21.140 --> 03:24.570
Ill print out this whole whole thing.

03:24.620 --> 03:27.970
This square of five is twenty five.

03:28.230 --> 03:32.460
And meanwhile continue on its merry way.

03:32.690 --> 03:36.690
Kay so it will never do this.

03:39.370 --> 03:45.910
So keep that in mind if you return something and you don't have some kind of condition or something

03:45.910 --> 03:47.150
associated with it.

03:47.440 --> 03:50.200
Nothing else after that will execute

03:53.270 --> 04:06.050
an really return the return type can be any type we want so it is not limited to any type that we looked

04:06.050 --> 04:06.350
at.

04:06.380 --> 04:07.160
Right.

04:07.200 --> 04:13.420
So and we'll see this a bit later as well.

04:13.420 --> 04:21.370
So for now the return values will be limited to our basic types that we can over but later on you'll

04:21.370 --> 04:32.530
see that all get into some more complicated types so in the next lecture I'm going to talk about some

04:32.530 --> 04:34.540
more interesting things.
