1
00:00:04,730 --> 00:00:08,960
Before we look at functions in more
detail, we're going now to take a few

2
00:00:08,960 --> 00:00:12,620
minutes to see what happens when you
call a function. You've probably worked

3
00:00:12,620 --> 00:00:17,040
out that execution will jump to line two,
when we call the multiply function,

4
00:00:17,040 --> 00:00:21,640
so let's see that happening. What I'm going
to do is set a breakpoint on line six,

5
00:00:21,700 --> 00:00:25,080
then run the program in the debugger.

6
00:00:26,540 --> 00:00:30,860
So the debugger stops on line six, where
we call multiply. In an earlier video,

7
00:00:30,870 --> 00:00:34,760
way back in the Program Flow Control
section, I said that I'd explain about

8
00:00:34,760 --> 00:00:38,240
the Step into my code button later. It's
now later.

9
00:00:38,240 --> 00:00:42,300
Up until now, we've been using the Step
over button -

10
00:00:42,309 --> 00:00:46,480
this one here - so let's start by seeing
what that does. I'm going to click Step

11
00:00:46,480 --> 00:00:52,560
over, to execute line six.
We get the answer in the console tab,

12
00:00:52,560 --> 00:00:57,600
and also to the right of line six. So the
Step over button executes a line, without

13
00:00:57,600 --> 00:01:03,680
stepping into any functions on that line.
So if you restart this again,

14
00:01:05,880 --> 00:01:09,960
and we stopped on line six again, we want
to make sure that the debugger tab is

15
00:01:09,960 --> 00:01:14,430
selected. This time, what I'm going to do
is use the Step into my code button - and

16
00:01:14,430 --> 00:01:19,320
that's not this one here. It's the one to
the right of that, Step into my code.

17
00:01:19,320 --> 00:01:25,660
Once I click that, notice that this time, the
program execution has jumped to line two.

18
00:01:25,660 --> 00:01:29,480
We've entered the code in our multiply
function. When you want to check the code

19
00:01:29,480 --> 00:01:33,880
in your functions, remember to use the
Step into my code button, as I just did.

20
00:01:33,880 --> 00:01:38,580
Using Step over, will execute the code in
your function without stepping through it.

21
00:01:38,580 --> 00:01:40,470
The step into button does the same thing

22
00:01:40,470 --> 00:01:43,770
at the moment - the one to the left of the
button that I clicked. You won't see a

23
00:01:43,770 --> 00:01:47,729
difference until you start splitting your
code into different modules. But get into

24
00:01:47,729 --> 00:01:52,049
the habit of using Step into my code,
because that's generally the code that

25
00:01:52,049 --> 00:01:55,229
you want to debug. Alright, so we're now
on line two,

26
00:01:55,229 --> 00:01:59,800
inside the multiply function. That's
about to multiply the two numbers,

27
00:01:59,800 --> 00:02:04,140
and store the result into the variable
called result. So at this point, we can

28
00:02:04,140 --> 00:02:08,880
either use the Step over, or Step into my
code here. They'll both do the same thing

29
00:02:08,880 --> 00:02:12,930
when there isn't a function call on the
current line. So as a result, I'm just going

30
00:02:12,930 --> 00:02:17,069
to use Step over, over here to the left,
just because that's a better description

31
00:02:17,069 --> 00:02:23,320
of what we're actually doing. We can see
that result now has the value 42.0,

32
00:02:23,320 --> 00:02:28,139
up there on line two, and we're ready to
execute line three. Before I Step over

33
00:02:28,140 --> 00:02:32,480
that line, have a look at the variables
in the debugger pane, down the bottom.

34
00:02:32,480 --> 00:02:36,900
We've got the variable result in there.
Keep an eye on that pane, because I want

35
00:02:36,900 --> 00:02:41,660
to talk about the scope of variables.
This is a good way to understand scope.

36
00:02:41,660 --> 00:02:46,620
That variable only exists inside the
multiply function. You'll see it vanish

37
00:02:46,620 --> 00:02:51,580
when we leave the function, so let's do
that. I'm going to click Step over again,

38
00:02:51,580 --> 00:02:55,570
and we get back to line 6, as you can see
there now. Notice that result has

39
00:02:55,570 --> 00:03:01,300
disappeared from the variables pane. Now
that's important - it no longer exists.

40
00:03:01,300 --> 00:03:05,080
I'm going to come back to this and talk
about scope in more detail, in the next

41
00:03:05,080 --> 00:03:09,970
video. So we're back on line 6, and the
code in our multiply function has been

42
00:03:09,970 --> 00:03:14,980
executed. It's returned a value and the
debugger is ready to proceed. When we

43
00:03:14,980 --> 00:03:21,220
press Step over again, which I'll do now,
line 6 finishes by binding the variable

44
00:03:21,220 --> 00:03:26,200
answer, to the value returned by our
function. We can now see answer in the

45
00:03:26,200 --> 00:03:31,780
variables pane, bound to the value 42.0.
Line seven's not very exciting. I'm gonna

46
00:03:31,780 --> 00:03:35,950
click on Step over again, to print the
value and terminate the program, and

47
00:03:35,950 --> 00:03:42,510
we can go to the console here, to confirm
the value 42.0 has been printed out.

48
00:04:18,100 --> 00:04:20,360
Step through this code, though, a few more times,

49
00:04:20,360 --> 00:04:22,700
until you're happy that you understand
what's happening,

50
00:04:22,700 --> 00:04:25,900
and I'll see you in the next video.

