1
00:00:02,280 --> 00:00:07,620
Hey, let's run a few code experiments now for a better understanding of functions.

2
00:00:07,860 --> 00:00:13,860
Specifically, you want to understand the difference between function definitions and function calls.

3
00:00:13,950 --> 00:00:19,420
And that is what the first experiment is about, the difference between function calls and definitions.

4
00:00:19,440 --> 00:00:27,480
So if I remove all these calls here and I just leave the function there and I run the codes, nothing

5
00:00:27,480 --> 00:00:29,340
will be returned as an output.

6
00:00:30,770 --> 00:00:33,620
Well, you could say there is no print function here.

7
00:00:33,620 --> 00:00:35,960
That's why we didn't get any outputs.

8
00:00:36,230 --> 00:00:39,140
But I can provide a print function here.

9
00:00:39,380 --> 00:00:41,150
Let's say, Hey, hey.

10
00:00:42,440 --> 00:00:44,360
And I run the code again.

11
00:00:44,360 --> 00:00:45,410
No outputs.

12
00:00:46,550 --> 00:00:48,020
So why is that?

13
00:00:48,110 --> 00:00:53,930
Well, that's because this is just a recipe of the process.

14
00:00:55,160 --> 00:00:58,880
If we wanted to use our coffee making example again.

15
00:00:59,480 --> 00:01:08,090
So when you go to order a coffee in a bar and you go to the bartender and you ask them, I want a coffee.

16
00:01:09,000 --> 00:01:11,160
They have a recipe in mind.

17
00:01:11,160 --> 00:01:13,980
Maybe in their mind, maybe they have written it somewhere.

18
00:01:14,160 --> 00:01:17,520
So the recipe is the function definition.

19
00:01:17,520 --> 00:01:19,450
That's the function definition.

20
00:01:19,470 --> 00:01:24,960
The recipe itself has to be executed by someone.

21
00:01:24,990 --> 00:01:26,160
The bartender.

22
00:01:27,230 --> 00:01:34,130
So the function also has to be executed in order to get the output and we execute the function like

23
00:01:34,130 --> 00:01:34,670
this.

24
00:01:34,670 --> 00:01:36,050
So greet.

25
00:01:40,000 --> 00:01:42,640
So that's the variable and that's the value.

26
00:01:43,680 --> 00:01:51,810
So at this point this will be equal to Halo with first letter capitalized.

27
00:01:53,370 --> 00:01:57,780
But if we run this again, what we get is, hey, hey.

28
00:01:58,170 --> 00:02:01,980
So the function was called and that proves it.

29
00:02:02,010 --> 00:02:03,030
We get hey, hey.

30
00:02:03,030 --> 00:02:06,270
So the function was called because this was executed.

31
00:02:06,510 --> 00:02:08,590
That's how we know the function was called.

32
00:02:08,610 --> 00:02:15,480
We don't get Halo printed out because we simply got a low and we stored it in greetings.

33
00:02:15,480 --> 00:02:21,450
So if we want hello to be print out, we say print greeting.

34
00:02:22,080 --> 00:02:24,840
You run again the code and then you get say hey.

35
00:02:24,840 --> 00:02:26,010
And also hello.

36
00:02:27,400 --> 00:02:35,110
So it's very important to realize that the function call is equal to a value.

37
00:02:35,140 --> 00:02:40,000
The value of that is always whatever is returned in here.

38
00:02:40,630 --> 00:02:43,210
So it could be a string, it could be a number.

39
00:02:44,310 --> 00:02:48,750
It could be any object that the function returns.

40
00:02:50,330 --> 00:02:55,250
So I hope that helps you understand the difference between the function definition and the function

41
00:02:55,250 --> 00:02:55,820
call.

42
00:02:56,270 --> 00:03:01,370
Now, the next experiment, it is about the return statement.

43
00:03:01,460 --> 00:03:06,290
So try to remove that line and print out the code.

44
00:03:09,470 --> 00:03:11,150
This is what we get this time.

45
00:03:11,840 --> 00:03:12,130
Hey.

46
00:03:12,130 --> 00:03:12,400
Hey.

47
00:03:12,410 --> 00:03:13,210
And none.

48
00:03:13,220 --> 00:03:17,900
So again, the interpreter goes and executes the blueprints.

49
00:03:17,930 --> 00:03:19,490
It doesn't do anything.

50
00:03:19,760 --> 00:03:21,890
It just looks at the recipe.

51
00:03:22,770 --> 00:03:24,740
And then you execute the recipe here.

52
00:03:25,560 --> 00:03:32,310
But the problem is that this time the recipe has not instructed what to return.

53
00:03:32,940 --> 00:03:41,220
So the coffee was prepared, but the recipe didn't say, Oh, give that coffee to.

54
00:03:41,970 --> 00:03:43,020
The client.

55
00:03:45,260 --> 00:03:53,740
And when that doesn't happen implicitly, the function definition will return a non object.

56
00:03:53,750 --> 00:03:59,210
So just like we have integer strings, we also have this special object called None.

57
00:03:59,930 --> 00:04:03,800
None is basically to represent nothing.

58
00:04:05,560 --> 00:04:11,310
So in Python, you can actually in the console you can just say none.

59
00:04:11,320 --> 00:04:14,710
So that's recognized as a value.

60
00:04:15,190 --> 00:04:18,550
So it's not a string, it doesn't have quotes.

61
00:04:18,550 --> 00:04:21,459
So this would be different from that.

62
00:04:22,270 --> 00:04:23,140
That is a string.

63
00:04:23,140 --> 00:04:25,210
That is a special object.

64
00:04:25,660 --> 00:04:29,920
So that's here is equivalent of having return none.

65
00:04:30,310 --> 00:04:32,880
If we run this, we're going to get the same error.

66
00:04:32,920 --> 00:04:35,460
Hey, sorry, the same output.

67
00:04:35,470 --> 00:04:36,430
It's not an error.

68
00:04:36,460 --> 00:04:37,180
The same.

69
00:04:38,080 --> 00:04:38,320
Hey.

70
00:04:38,320 --> 00:04:38,650
Hey.

71
00:04:38,650 --> 00:04:39,730
And none.

72
00:04:42,800 --> 00:04:48,680
So you normally want to return something here in this case is new message.

73
00:04:49,920 --> 00:04:56,640
Some people what they will do, especially beginners, they will confuse print with return.

74
00:04:56,640 --> 00:05:03,180
So instead of returning something, they will say a print new message and let's see what we get this

75
00:05:03,180 --> 00:05:03,720
time.

76
00:05:05,350 --> 00:05:06,010
So.

77
00:05:08,610 --> 00:05:09,810
This is what happens.

78
00:05:09,990 --> 00:05:13,860
The interpreter reads the recipe and then it executes the recipe.

79
00:05:13,860 --> 00:05:22,020
So messages associated to the hello string and the new message is associated to hello with the first

80
00:05:22,020 --> 00:05:23,180
letter capitalized.

81
00:05:23,190 --> 00:05:25,560
So far, so good than print.

82
00:05:25,590 --> 00:05:30,300
Hey, so this is printed out in here then Print message.

83
00:05:30,870 --> 00:05:32,190
Message is printed out.

84
00:05:32,220 --> 00:05:32,970
Hello.

85
00:05:34,890 --> 00:05:36,720
You see with the letter cabinet lights.

86
00:05:36,720 --> 00:05:38,160
So, so far, so good.

87
00:05:38,280 --> 00:05:40,010
And then none is printed out.

88
00:05:40,020 --> 00:05:41,880
Why is not printed out?

89
00:05:41,910 --> 00:05:49,440
Well, because I just told you that when you don't have that return statement, the function will always

90
00:05:49,440 --> 00:05:51,210
return none.

91
00:05:51,570 --> 00:05:54,870
So implicitly, the function again is doing return none.

92
00:05:57,930 --> 00:06:01,800
That's why we get none here.

93
00:06:02,880 --> 00:06:08,180
So in other words, grits will be equal to none to that object.

94
00:06:08,190 --> 00:06:09,300
And that's why.

95
00:06:10,040 --> 00:06:11,000
When it prints.

96
00:06:11,000 --> 00:06:19,070
Here we are printing the non object, the object that the function greets returns.

97
00:06:19,310 --> 00:06:22,100
So you don't want to confuse print with return.

98
00:06:24,850 --> 00:06:28,750
Always try to return the output instead of printing it out.

99
00:06:31,180 --> 00:06:36,070
So if I run the script now, we get the correct expected output.

100
00:06:36,100 --> 00:06:40,910
Hey, hey, print it out by this and hello by that.

101
00:06:40,930 --> 00:06:45,190
So why is it important to return and not print?

102
00:06:45,460 --> 00:06:49,720
Well, because perhaps you want to do something to that.

103
00:06:50,830 --> 00:06:52,340
Value, which is Hello.

104
00:06:52,360 --> 00:06:55,620
Perhaps you want to print out the length of Hello.

105
00:06:55,630 --> 00:07:02,050
So the length would be Len greeting you run this and you get five.

106
00:07:02,560 --> 00:07:04,690
But if you had print here.

107
00:07:08,640 --> 00:07:09,870
See what we get now.

108
00:07:10,500 --> 00:07:18,330
It's an error because you're trying to apply Len to non to the non type object, and that gives an error

109
00:07:18,330 --> 00:07:24,600
because the non object doesn't have a length, doesn't have characters like a string has or at least

110
00:07:24,630 --> 00:07:25,440
has.

111
00:07:27,020 --> 00:07:33,740
So again, I cannot stress it enough that you should return instead of printing.

112
00:07:35,480 --> 00:07:40,340
So this was about functions for this code experiments for today.

113
00:07:40,520 --> 00:07:41,210
Thanks a lot.

114
00:07:41,240 --> 00:07:41,660
See you.

