1
00:00:04,990 --> 00:00:09,130
Let's talk about debugging with
parameters. If you've used functions in

2
00:00:09,130 --> 00:00:12,700
other languages, you may want to play
this video at double speed. It's

3
00:00:12,700 --> 00:00:16,689
something that a lot of new programmers
find confusing at first, which is why I'm

4
00:00:16,689 --> 00:00:20,520
going to take the time to demonstrate
what happens.

5
00:00:57,640 --> 00:01:01,120
and the link's really good for a
description of call by sharing that was

6
00:01:01,120 --> 00:01:06,200
first introduced in the programming
language, CLU. The Python documentation

7
00:01:06,200 --> 00:01:10,580
uses the term call by assignment, but
it's basically the same thing.

8
00:01:10,580 --> 00:01:14,460
Now this entry in the Python 3 FAQ, which
I'm about to put on screen, is also

9
00:01:14,460 --> 00:01:18,400
interesting, if you wanted to produce the
effect of call by reference.

10
00:01:18,400 --> 00:01:21,740
Check that out as well. If none of that
makes any sense,

11
00:01:21,759 --> 00:01:25,420
and you've never written a function
before, let's step through the code

12
00:01:25,420 --> 00:01:30,740
and see what's going on. Alright, so we've
still got a breakpoint set on line 6,

13
00:01:30,740 --> 00:01:34,480
So let's run the program in the debugger,
and see how our parameters get replaced,

14
00:01:34,480 --> 00:01:38,700
with the values we pass as arguments.

15
00:01:41,140 --> 00:01:43,780
The debugger the stops on line 6, ready to enter

16
00:01:43,780 --> 00:01:47,830
our multiply function. We're going to use
the Step into my code button, to enter

17
00:01:47,830 --> 00:01:52,130
the function.
The debugger's now stopped on line two.

18
00:01:52,130 --> 00:01:56,600
Before we go any further, have a look now
at the variables pane down the bottom.

19
00:01:56,600 --> 00:02:01,340
On line six, we passed the arguments 10.5
and 4 to the function.

20
00:02:01,340 --> 00:02:07,020
On line one, and in the variables pane, you
can see that x has the value 10.5,

21
00:02:07,020 --> 00:02:13,220
and y is 4. What we have here, are
two ordinary variables that only exist

22
00:02:13,220 --> 00:02:18,530
inside the function. That's all that a
parameter is - it's a variable that exists

23
00:02:18,530 --> 00:02:22,940
inside the function. Within the function,
you can use them just like you'd use any

24
00:02:22,940 --> 00:02:27,170
other variable- there's nothing special
about them. When an argument is passed to

25
00:02:27,170 --> 00:02:32,280
the function, like we do on line six, the
parameter gets the value of the argument

26
00:02:32,280 --> 00:02:37,580
that's passed to the function. On line
six, the first argument is 10.5.

27
00:02:37,580 --> 00:02:43,040
The first parameter on line one is x, so
x is therefore bound to the value 10.5.

28
00:02:43,040 --> 00:02:47,660
The second argument passed to
the function is 4, the second parameter

29
00:02:47,700 --> 00:02:53,660
is y, so y is bound to the value 4. the
values of the arguments are assigned to

30
00:02:53,700 --> 00:02:58,600
the parameters in order. Each argument
is assigned to the parameter in the same

31
00:02:58,600 --> 00:03:03,720
position. Our first argument is 10.5, 
and that gets assigned to the first

32
00:03:03,739 --> 00:03:10,270
parameter x. For that reason, these
arguments are positional arguments.

33
00:03:30,500 --> 00:03:32,750
Alright, so let's continue step 
you through the code. I'm going to use

34
00:03:32,750 --> 00:03:37,430
Step over, to check that the result had
the value 42,

35
00:03:37,430 --> 00:03:42,760
and you can see that it has there. Now
watch what happens when I step over again.

36
00:03:42,760 --> 00:03:46,980
The variables x, y and result no longer exist.

37
00:03:46,980 --> 00:03:50,349
IntelliJ is no longer showing
their value on line one, and they also

38
00:03:50,349 --> 00:03:56,440
don't appear in the variables pane. Their
scope is the function they're defined in.

39
00:03:56,440 --> 00:03:59,980
That's all scope means when you come
across that term. It's where a variable

40
00:03:59,980 --> 00:04:04,959
exists. I'll be talking more about scope,
later in the course. Alright, so let's step

41
00:04:04,959 --> 00:04:08,700
over twice more to get to line 9.

42
00:04:09,110 --> 00:04:13,370
This time, our arguments are six and
seven. You should be able to work out the

43
00:04:13,370 --> 00:04:17,149
values that x and y will have, when we step
into the function. Make sure you work

44
00:04:17,149 --> 00:04:22,400
that out before clicking Step into my
code. So we're passing the values six and

45
00:04:22,400 --> 00:04:26,540
seven in that order. That means x should
get the first value, because x appears

46
00:04:26,540 --> 00:04:32,920
first in the parameter list, on line one.
I'm going to click Step into my code,

47
00:04:33,920 --> 00:04:40,419
and we were right; x is 6 and y is 7. The
function's about to multiply x by y, and bind a

48
00:04:40,419 --> 00:04:43,840
variable called result, to the answer.
We're going to click Step over

49
00:04:43,840 --> 00:04:49,760
to execute that line. We now have x and y
and result in the variables pane.

50
00:04:49,760 --> 00:04:54,680
We're going to step over four more times now,
to take us to line 14.

51
00:04:55,840 --> 00:05:00,940
Before we continue, check out the
variables pane, now. answer was created, on

52
00:05:00,940 --> 00:05:05,110
line six, when it was bound to the value
returned by the first call to the

53
00:05:05,110 --> 00:05:10,419
multiply function. forty_two
was created on line nine. These two

54
00:05:10,420 --> 00:05:15,460
variables exist throughout our program.
Their scope is the entire program.

55
00:05:15,460 --> 00:05:20,020
The variables x, y and result don't appear in
the variables pane, because they no

56
00:05:20,020 --> 00:05:25,400
longer exist. Their scope is the multiply
function, and they don't exist outside it.

57
00:05:25,400 --> 00:05:30,520
Alright, so we're about to create a new
variable here, called val. The name val,

58
00:05:30,520 --> 00:05:36,060
will be bound to each of the numbers 1
to 4, in turn, as the for loop executes.

59
00:05:36,070 --> 00:05:40,360
I'm gonna click on Step over, and
we can see val appears in our

60
00:05:40,360 --> 00:05:45,640
variables pane. IntelliJ is also showing
its value at the end of line 14.

61
00:05:45,640 --> 00:05:50,820
We're now on line 15 and about to enter the
multiply function again. This time we're

62
00:05:50,820 --> 00:05:55,480
passing a variable for the second
argument. We've included this example,

63
00:05:55,480 --> 00:05:59,700
to make it clear that you don't have to
pass just literal values. You can use any

64
00:05:59,710 --> 00:06:04,930
expression that evaluates to the correct
type for the parameter. In this case, our

65
00:06:04,930 --> 00:06:10,100
function expects a number, so you can use
anything that evaluates to be a number.

66
00:06:10,100 --> 00:06:14,680
We're using the variable val, which
currently has the value 1. So we come

67
00:06:14,680 --> 00:06:19,000
over here and click on Step in to my
code, and you shouldn't be surprised,

68
00:06:19,000 --> 00:06:25,060
therefore, to see that x got the value 2
and y got the value of val - that's 1.

69
00:06:25,060 --> 00:06:28,240
We don't need to see 2 being multiplied by
1, so I'm going to show you another one

70
00:06:28,240 --> 00:06:32,289
of the debugger functions - that's the
upwards pointing arrow Step out, over

71
00:06:32,289 --> 00:06:36,669
here. It can be useful if you step into a
function by mistake, or if you've seen

72
00:06:36,669 --> 00:06:40,380
everything you want in a function, and
you want to go back to the main code.

73
00:06:40,380 --> 00:06:44,760
Step out will execute the remainder of
the code in the function, and return to

74
00:06:44,770 --> 00:06:49,479
the point where the function was called.
In our case, the function was called on

75
00:06:49,479 --> 00:06:53,979
line 15. We cannot currently see that, but
when I click on Step out, we'll get back

76
00:06:53,979 --> 00:06:58,050
to line 15. Alright, so the value 2 will have

77
00:06:58,050 --> 00:07:03,379
been returned from the function, and when
I step over once more now,

78
00:07:03,759 --> 00:07:09,699
the variable two_times, now, is
bound to 2. I'm going to step over once

79
00:07:09,699 --> 00:07:13,010
more to print out two_times.

80
00:07:13,010 --> 00:07:18,940
We get back to line 40. We can check
in the console that 2 has been output.

81
00:07:18,940 --> 00:07:23,249
Back to the debugger. Right, so we're
on line 14, and we can see that's going

82
00:07:23,249 --> 00:07:27,270
to bind val to the next number in the
sequence. I step over and val should now

83
00:07:27,270 --> 00:07:32,249
have the value 2, and we can see that
is the case. This time when we step into

84
00:07:32,249 --> 00:07:36,340
my code, you can see that both parameters now

85
00:07:36,340 --> 00:07:39,550
have the value 2. You should now
understand what happens when you call a

86
00:07:39,550 --> 00:07:43,990
function in your code. Python will
execute the code in your function, then

87
00:07:43,990 --> 00:07:48,580
return to the point where your function
was called. I'll stop this video here.

88
00:07:48,580 --> 00:07:53,060
Continue stepping into the multiply
function, and check that the parameter y

89
00:07:53,060 --> 00:07:56,940
gets each value of val, each time around the loop.

90
00:07:56,940 --> 00:08:00,600
And I'll see you in the next video.

