1
00:00:00,510 --> 00:00:09,840
Hello and welcome to this video functions are an important part of programming in Python.

2
00:00:09,840 --> 00:00:21,500
They help us keep our programs organized and makes it possible for us to reuse the code we write.

3
00:00:21,510 --> 00:00:29,320
There is a principle in software development which all good programmers tried to follow.

4
00:00:29,340 --> 00:00:38,400
It is called DRY principle D R Y which stands for don't repeat yourself.

5
00:00:38,550 --> 00:00:42,880
In other words it means don't write the same code twice.

6
00:00:43,020 --> 00:00:50,550
Now functions can help you avoid writing the same code twice.

7
00:00:50,550 --> 00:00:59,940
So what are functions functions are basically a collection of computer code which can be executed anywhere

8
00:01:00,480 --> 00:01:05,010
in your program and also use over and over again.

9
00:01:05,010 --> 00:01:15,090
So they are reusable and these block of code always do something specific.

10
00:01:15,120 --> 00:01:20,900
The beauty about functions is that they are reusable.

11
00:01:21,330 --> 00:01:28,830
The execute or run when they are called and you usually call them by their name.

12
00:01:28,830 --> 00:01:37,470
So when you create a function in order to activate it you need to run it or call it by the name you

13
00:01:37,470 --> 00:01:39,110
have given it.

14
00:01:39,180 --> 00:01:49,800
They can have parameters parameters are basically variables that you define inside a function parentheses

15
00:01:50,280 --> 00:01:53,190
and also they can have arguments.

16
00:01:53,190 --> 00:02:00,530
Arguments are the actual value that you give to the function when you call the function.

17
00:02:00,960 --> 00:02:05,140
They can return data as a result.

18
00:02:05,140 --> 00:02:09,810
So if you want the functions to return data they can also do that.

19
00:02:09,810 --> 00:02:18,130
Functions are built in and they can also be cost Tom created.

20
00:02:18,210 --> 00:02:24,990
Example of the built in functions we have use so far is print.

21
00:02:25,020 --> 00:02:30,270
The print is a function which allows you to print stuff on the console.

22
00:02:30,300 --> 00:02:38,550
You can also create your own custom functions which are called user defined functions.

23
00:02:38,550 --> 00:02:42,250
Creating a function is fairly straightforward.

24
00:02:42,360 --> 00:02:50,220
Before you create a function it's good to think of a descriptive name that you want your function to

25
00:02:50,310 --> 00:02:51,210
have.

26
00:02:51,360 --> 00:03:01,770
The naming convention for a function is the same as in naming convention for variables to create a function.

27
00:03:01,770 --> 00:03:08,640
You start with the D If keyword which indicates you are about to create a function and then you give

28
00:03:08,640 --> 00:03:17,220
the function a name followed by parentheses and then the call on the colon basically tells the Python

29
00:03:17,220 --> 00:03:20,780
interpreter that you're about to start a new line of code.

30
00:03:20,790 --> 00:03:30,240
So whatever code you write here is the block of code that gets executed when the function is activated.

31
00:03:30,630 --> 00:03:38,490
When you define functions inside the parenthesis the parenthesis can be empty or you can passing what

32
00:03:38,490 --> 00:03:40,530
is known as parameters.

33
00:03:40,530 --> 00:03:46,920
Parameters are basically variables that you define inside the function.

34
00:03:46,920 --> 00:03:56,490
When you activate your function you can also give it a value that matches the parameter you set.

35
00:03:56,580 --> 00:04:00,800
That value is referred to as an argument.

36
00:04:00,990 --> 00:04:02,450
So I've already got a directory.

37
00:04:02,450 --> 00:04:08,390
I'm just gonna right click on the directory click on new and click on python file.

38
00:04:08,430 --> 00:04:17,310
I'm going to call it functions and it will automatically append it with a dot P Why file extensions

39
00:04:17,320 --> 00:04:18,930
are click Okay.

40
00:04:19,380 --> 00:04:26,680
That was created the file called functions got P Y create a function you start with the keyword d e

41
00:04:26,690 --> 00:04:31,120
f you do space and then you give the function a name.

42
00:04:31,130 --> 00:04:35,670
I'm going to call my function sum and then you add the parent.

43
00:04:35,660 --> 00:04:38,430
This is inside the parent theses.

44
00:04:38,490 --> 00:04:43,440
You can add parameters or you can leave the parentheses empty.

45
00:04:43,440 --> 00:04:50,100
So I'm going to add a couple of parameters which are variables I'm going to call this X I'll do a comma

46
00:04:50,730 --> 00:04:53,120
and I'll call this Y.

47
00:04:53,160 --> 00:04:57,200
Once you're done you add a call on the column.

48
00:04:57,210 --> 00:05:03,880
Basically tells Python that you want to start a new line of code.

49
00:05:03,930 --> 00:05:07,410
So here now is what is called the function body.

50
00:05:07,410 --> 00:05:12,570
This is the code that will execute when the function is activated.

51
00:05:12,570 --> 00:05:18,890
So I'm just going to say X plus why.

52
00:05:20,130 --> 00:05:21,390
So that is it.

53
00:05:21,390 --> 00:05:24,570
We have created a function.

54
00:05:24,930 --> 00:05:29,630
Now the X and Y are known as parameters.

55
00:05:29,640 --> 00:05:30,320
Okay.

56
00:05:30,540 --> 00:05:39,390
So when this function activates it will print the value of x plus the value of y.

57
00:05:39,990 --> 00:05:48,270
Once you have created your function the function does nothing until the function is activated.

58
00:05:48,270 --> 00:05:54,180
Now the process of activating the function is called calling the function.

59
00:05:54,180 --> 00:06:00,650
It also has other names you can call it execute in the function or invoking the function.

60
00:06:00,660 --> 00:06:09,300
So any phrase you hear relating to call running execute in invoking they all mean the same thing.

61
00:06:09,390 --> 00:06:17,460
And when you call it function you call it by its name and you add the parentheses.

62
00:06:17,460 --> 00:06:25,980
Now if you have defined some parameters during the function definition then you need to parse seen some

63
00:06:26,100 --> 00:06:35,880
arguments when you core the function those arguments are the values that you give for the parameters

64
00:06:35,910 --> 00:06:39,990
you have set in the function definition.

65
00:06:39,990 --> 00:06:46,980
So to activate or call this function that we've created we need to call it by his name and the name

66
00:06:47,100 --> 00:06:47,860
is sum.

67
00:06:47,870 --> 00:06:52,670
So say some and then we had the parentheses.

68
00:06:52,680 --> 00:06:57,190
Now notice that we had two parameters defined inside the function.

69
00:06:57,330 --> 00:06:58,500
We've got X and Y.

70
00:06:58,530 --> 00:07:05,450
So now we need to supply the values for x and y those values are known as arguments.

71
00:07:05,460 --> 00:07:16,170
So if I say for comma five and that basically cause the function parsing those two values for the para

72
00:07:16,350 --> 00:07:20,970
methods I have set and then it will execute the function.

73
00:07:20,970 --> 00:07:29,940
So let me save that and then play the file so I click on play and you can see here is return the value

74
00:07:29,940 --> 00:07:31,140
of nine.

75
00:07:31,230 --> 00:07:31,680
Okay.

76
00:07:31,680 --> 00:07:35,910
So is added because here I've said print x plus y.

77
00:07:35,930 --> 00:07:46,950
So when I call the function are posting two values for 4 x and 5 for y and then returned the value of

78
00:07:47,250 --> 00:07:47,610
9.

79
00:07:47,970 --> 00:07:51,100
So that's how you call a function.

80
00:07:51,150 --> 00:07:59,580
Once you have defined the function it is good to know the difference between parameter and argument.

81
00:07:59,640 --> 00:08:08,280
Sometimes this can confuse begin as a parameter is actually like a variable that you define inside the

82
00:08:08,310 --> 00:08:17,250
functions parentheses while an argument is the actual value that you pass or give to the function when

83
00:08:17,250 --> 00:08:19,960
you call the function.

84
00:08:19,980 --> 00:08:29,460
So in our function x and y are the parameters while the number 4 and 5 are the actual arguments for

85
00:08:29,460 --> 00:08:31,070
those parameters.

86
00:08:31,100 --> 00:08:32,700
So that's it for this video.

87
00:08:32,700 --> 00:08:33,900
Thanks for watching.

88
00:08:33,900 --> 00:08:34,590
Bye for now.
