1
00:00:00,800 --> 00:00:01,150
Hello.

2
00:00:01,160 --> 00:00:02,150
Welcome back.

3
00:00:02,150 --> 00:00:06,920
So in this lesson we going to see how to create functions and in Python.

4
00:00:07,340 --> 00:00:08,720
So this was our last project.

5
00:00:08,720 --> 00:00:16,850
Let's create a new project or a new script from our click file and come to New file then our press control

6
00:00:16,930 --> 00:00:22,240
is to save and I'll save this is number six under school functions like this.

7
00:00:22,820 --> 00:00:24,860
Once that is done we can close this now.

8
00:00:25,190 --> 00:00:32,300
So as we saw earlier Python doesn't use the curly braces we find in our programming languages like C

9
00:00:33,110 --> 00:00:40,100
actually Python uses blocks and it's able to use blocks through indentation and its pacing is the same

10
00:00:40,100 --> 00:00:47,540
with functions functions over here on surrounded by Kelly braces to indicate a block but rather indented

11
00:00:47,540 --> 00:00:49,020
in a particular way.

12
00:00:49,040 --> 00:00:51,080
So let's start with the first example.

13
00:00:51,170 --> 00:00:53,050
I'm going to put a comment up here.

14
00:00:53,070 --> 00:00:57,220
I'll call this example one example one and let's give it a name.

15
00:00:57,230 --> 00:01:00,370
This is going to be a simple function to create a function.

16
00:01:00,380 --> 00:01:07,400
We start with the keyword the F death and then space and then we type the function name so we can call

17
00:01:07,400 --> 00:01:16,190
this simple on the school function like this and we open and close the braces.

18
00:01:16,200 --> 00:01:17,900
Normal stuff here.

19
00:01:17,900 --> 00:01:19,640
So the function has no argument.

20
00:01:19,670 --> 00:01:22,080
So we open and close just like this.

21
00:01:22,130 --> 00:01:26,700
If we were to write this function in C we might put a void here or something.

22
00:01:26,860 --> 00:01:30,080
But in Python no argument to just open and close.

23
00:01:30,080 --> 00:01:36,290
Once that is done we put a call on here and then we hit enter when we hit enter it to automatically

24
00:01:36,380 --> 00:01:37,850
indent for us.

25
00:01:37,850 --> 00:01:40,840
So once this comes once we've defined the function.

26
00:01:40,970 --> 00:01:46,730
Now we can put the contents of the function what we want is function to do we want this function to

27
00:01:46,730 --> 00:01:50,910
print a simple statement we can say print.

28
00:01:51,350 --> 00:01:58,970
And over here we can say this is a simple function and we can press control as to save and then click

29
00:01:58,980 --> 00:02:00,890
right here to run and see what we have

30
00:02:03,550 --> 00:02:05,450
so nothing is happening.

31
00:02:05,480 --> 00:02:07,750
It's because we've not code to function.

32
00:02:07,750 --> 00:02:10,960
So it's quite a function we can just bring the function name here.

33
00:02:10,960 --> 00:02:13,630
Simple on the school function like this

34
00:02:16,330 --> 00:02:17,830
just like in any other language.

35
00:02:17,830 --> 00:02:22,840
This the way we call functions once that's done we hit enter and function as run.

36
00:02:22,840 --> 00:02:24,480
This is a simple function.

37
00:02:24,850 --> 00:02:31,760
If we had to add more content to the function we have to follow this indentation line this line we have

38
00:02:31,760 --> 00:02:33,800
to go vertically like this.

39
00:02:33,890 --> 00:02:40,250
But if we want to come out of the function we just come out like this would come out of the indentation

40
00:02:40,250 --> 00:02:46,340
as well so we can come out and say simple functions such that when we run it would automatically call

41
00:02:46,340 --> 00:02:54,110
the function like this and then we press control to save the click over her and a click here to run

42
00:02:54,110 --> 00:03:00,200
the module and I can see this is a simple function it's called the function automatically.

43
00:03:00,200 --> 00:03:01,610
Right.

44
00:03:01,700 --> 00:03:08,520
So we can clean this too let's go to example number two let's see a function that takes arguments to

45
00:03:08,900 --> 00:03:16,950
just bring this here I'll put a comment to your example number two and I'll call this function arguments.

46
00:03:17,120 --> 00:03:19,030
So we do the same way.

47
00:03:19,250 --> 00:03:28,940
We start by defining it de F and we can give this function a name such as a school in this country do

48
00:03:28,960 --> 00:03:36,230
for country on the score for let's say we want this function to perform its simple task we want to pass

49
00:03:36,230 --> 00:03:42,860
it to argument concrete and Copy tool and we want the function to print the capital of this country

50
00:03:42,980 --> 00:03:49,550
is this so we can just bring it here we can see the first argument is country and in the same way we

51
00:03:49,550 --> 00:03:55,340
don't need to give that type Yeah in other languages we might need to see if the argument number one

52
00:03:55,340 --> 00:04:02,030
is a string an integer a float but as we saw earlier in python you declare variables without assigning

53
00:04:02,030 --> 00:04:09,740
them any types so we can say come talk over here and put a semicolon here hit enter and we can come

54
00:04:09,740 --> 00:04:23,660
down here and a print and then we open our quotation marks the capital of percentage s is percent yes

55
00:04:23,840 --> 00:04:29,900
how about this we can come out of the sentence and put a percentage sign here and then open braces and

56
00:04:30,170 --> 00:04:40,620
say column tree comma copy capital like this and then let's see how it runs so let's call this function

57
00:04:40,620 --> 00:04:47,260
down here way to hit enter and hit enter again and come out of the function and see concrete.

58
00:04:47,560 --> 00:04:52,830
Now let's just go call it from the shell so right.

59
00:04:52,830 --> 00:04:55,260
Control as to save we run here

60
00:04:58,870 --> 00:05:05,910
and it's run so we can call our new function and see a country on the school info.

61
00:05:06,490 --> 00:05:15,280
And then we can see it over here let's say we pass it a country like Ghana and then capital of Accra.

62
00:05:15,690 --> 00:05:16,160
Okay.

63
00:05:16,200 --> 00:05:24,480
So what I just did is I did pass a string I've just passed you know a different type to pass this our

64
00:05:24,480 --> 00:05:30,490
function accepts strings although we didn't define country string country string capital.

65
00:05:30,490 --> 00:05:32,070
It does accept strings.

66
00:05:32,070 --> 00:05:42,400
So we have to pass it country on underscore underscore info and then put a country here and then comma

67
00:05:43,200 --> 00:05:49,850
put a copy to her and then we hit enter and I can see the capital of colonized Agra.

68
00:05:50,150 --> 00:05:52,510
And then we can do another one.

69
00:05:53,010 --> 00:05:57,510
We can say country strikes again.

70
00:05:57,660 --> 00:06:01,620
You can destroy it with any country.

71
00:06:02,910 --> 00:06:07,750
Just pull countries UK capital is London.

72
00:06:07,810 --> 00:06:08,950
Let's see.

73
00:06:09,160 --> 00:06:11,430
And indeed the capital of U.K. is London.

74
00:06:11,640 --> 00:06:14,770
So this this how to write functions with arguments.

75
00:06:14,940 --> 00:06:17,940
So let's see the third type of function you would see.

76
00:06:17,940 --> 00:06:19,170
We've seen the simple function.

77
00:06:19,170 --> 00:06:21,500
We've seen a function that accepts argument.

78
00:06:21,540 --> 00:06:27,450
The third function will be a function that returns values or returns something.

79
00:06:27,500 --> 00:06:28,500
If you may.

80
00:06:28,500 --> 00:06:32,640
So I'll just come down here and call this example number three

81
00:06:36,370 --> 00:06:44,080
quarters function with return value function with a return value like this

82
00:06:47,240 --> 00:06:49,340
and we can come here and define a function.

83
00:06:49,340 --> 00:06:55,880
So we say the F like always and this function we can see and underscore with this assuming we want to

84
00:06:55,880 --> 00:06:59,900
function that adds to numbers so we can pass the first number.

85
00:06:59,960 --> 00:07:05,780
And then the second number and then we'll come out here we put some called on hit enter and we can't

86
00:07:05,780 --> 00:07:08,090
see a return.

87
00:07:08,330 --> 00:07:10,670
A plus B.

88
00:07:10,820 --> 00:07:11,890
Right.

89
00:07:11,960 --> 00:07:15,130
So we'll press control as to save click Run.

90
00:07:15,200 --> 00:07:20,750
And then we run the module and we can see out of this.

91
00:07:20,750 --> 00:07:22,190
That's the name of the function.

92
00:07:22,280 --> 00:07:29,290
What we want to artless you want about 20 comma 30 let's hit enter.

93
00:07:29,290 --> 00:07:31,540
And of course the answer is 50.

94
00:07:31,540 --> 00:07:34,490
So this how to create functions in Python.

95
00:07:34,600 --> 00:07:37,030
Very simple very straightforward.

96
00:07:37,180 --> 00:07:42,190
And this all there is for this very short lesson if you have any questions at all do send me a message

97
00:07:42,190 --> 00:07:43,950
or leave it in the questions area.
