1
00:00:07,000 --> 00:00:11,800
You've learned about Python's
built-in functions for common tasks.

2
00:00:11,800 --> 00:00:16,000
This time, we're going to take a closer
look at how to create your own custom functions

3
00:00:16,000 --> 00:00:18,879
for your specific needs and projects.

4
00:00:18,879 --> 00:00:23,319
By the end of this video, you will be able
to explain live code examples of functions

5
00:00:23,319 --> 00:00:29,738
with arguments and return values, as well as
explain how to call them within the Python script.

6
00:00:29,739 --> 00:00:35,860
To briefly review, functions are self-contained
blocks of code that perform specific tasks.

7
00:00:35,860 --> 00:00:41,860
They allow you to encapsulate a series of
instructions into reusable chunks, like mini-programs,

8
00:00:41,860 --> 00:00:45,819
that can be reused in your
program as often as you need.

9
00:00:45,819 --> 00:00:50,560
Python also provides you with the ability
to write your own custom functions.

10
00:00:50,560 --> 00:00:54,060
The first thing you know about writing
your own functions is that you need to provide

11
00:00:54,060 --> 00:00:57,500
them with inputs and
receive outputs from them.

12
00:00:57,500 --> 00:01:03,180
These very important components
are arguments and return values.

13
00:01:03,180 --> 00:01:06,980
Let's take a closer look at
each of these components.

14
00:01:06,980 --> 00:01:10,400
Arguments are the values you pass
into a function when you call it.

15
00:01:10,400 --> 00:01:14,699
In other words, they provide the necessary
data for the function to perform its task

16
00:01:14,699 --> 00:01:17,260
and adapt to different scenarios.

17
00:01:17,260 --> 00:01:21,580
If you think of a function as a machine,
you need to provide inputs like the electricity

18
00:01:21,580 --> 00:01:24,879
to power it and the buttons
to turn it on and off.

19
00:01:24,879 --> 00:01:27,379
These inputs are the arguments.

20
00:01:27,379 --> 00:01:31,940
An example of this would be a function designed
to calculate an employee's net salary.

21
00:01:31,940 --> 00:01:36,419
The arguments for this function might include
the employee's gross salary, tax rate, and

22
00:01:36,419 --> 00:01:38,239
any deductions.

23
00:01:38,239 --> 00:01:42,739
By providing these arguments, the function
can perform the necessary calculations and

24
00:01:42,739 --> 00:01:45,620
return the final net salary.

25
00:01:45,620 --> 00:01:50,459
Once your function has processed its arguments
and performed its task, it might need to communicate

26
00:01:50,459 --> 00:01:52,819
the results back to the main program.

27
00:01:52,819 --> 00:01:55,419
The results are the return values.

28
00:01:55,459 --> 00:01:59,980
A return value is the output or
result generated by your function.

29
00:01:59,980 --> 00:02:02,739
It's like the product the machine produces.

30
00:02:02,739 --> 00:02:06,819
An example of this would be a function that
checks if a user's entered password meets

31
00:02:06,819 --> 00:02:09,300
specific security criteria.

32
00:02:09,300 --> 00:02:14,259
The function would take the password as an
argument, perform various checks, length,

33
00:02:14,259 --> 00:02:19,220
complexity, etc., and then return a true or
false value to indicate whether the password

34
00:02:19,220 --> 00:02:20,619
is valid.

35
00:02:20,619 --> 00:02:24,979
This return value can then be used by the
main program to decide whether to grant the

36
00:02:24,979 --> 00:02:28,860
user access or to return an error message.

37
00:02:28,860 --> 00:02:31,500
So how do you write a function in Python?

38
00:02:31,500 --> 00:02:34,460
Let's go through it step by step.

39
00:02:34,460 --> 00:02:35,460
Step 1.

40
00:02:35,460 --> 00:02:36,899
Define the function.

41
00:02:36,899 --> 00:02:40,500
Start with the keyword def,
followed by the function name.

42
00:02:40,500 --> 00:02:44,179
If it's more than one word,
be sure to use an underscore.

43
00:02:44,179 --> 00:02:47,300
Immediately after the name, add parentheses.

44
00:02:47,300 --> 00:02:51,039
That's where you'll put and arguments, and a colon.

45
00:02:51,039 --> 00:02:52,100
Step 2.

46
00:02:52,100 --> 00:02:53,500
Add a docstring.

47
00:02:53,500 --> 00:02:58,699
This is a clear and concise description
of what the function does, its purpose, arguments,

48
00:02:58,699 --> 00:03:00,460
and return value.

49
00:03:00,460 --> 00:03:03,619
Make sure to put the docstring
between triple quotes.

50
00:03:03,619 --> 00:03:08,020
This is the documentation for
yourself and other developers.

51
00:03:08,020 --> 00:03:09,020
Step 3.

52
00:03:09,020 --> 00:03:10,139
Write the code.

53
00:03:10,139 --> 00:03:14,539
In the indented block following the function
definition, write the code that performs the

54
00:03:14,539 --> 00:03:18,539
desired task using any provided arguments.

55
00:03:18,539 --> 00:03:19,779
Step 4.

56
00:03:19,779 --> 00:03:21,339
Return a value.

57
00:03:21,339 --> 00:03:25,899
This is optional, but if your function needs to
produce an output, use the return statement

58
00:03:25,899 --> 00:03:28,899
to send a value back to the calling code.

59
00:03:28,899 --> 00:03:32,740
The last step, step 5, is saving the function.

60
00:03:32,740 --> 00:03:36,699
Although this seems like an obvious step, a
lot of good work is often lost because people

61
00:03:36,699 --> 00:03:38,839
are in a hurry and forget.

62
00:03:39,839 --> 00:03:43,500
After you have defined your function, you
can call it from other parts of your program

63
00:03:43,500 --> 00:03:46,360
to execute the code encapsulated in it.

64
00:03:46,360 --> 00:03:49,220
This is how you reuse the function.

65
00:03:49,220 --> 00:03:53,899
For example, say you have a sales team that
needs to generate weekly, monthly, and yearly

66
00:03:53,899 --> 00:03:55,539
sales reports.

67
00:03:55,539 --> 00:04:00,179
You can create a function that takes the desired
time period as an argument and generates the

68
00:04:00,179 --> 00:04:02,520
corresponding report.

69
00:04:02,520 --> 00:04:06,059
The last thing we want to talk
about is calling functions.

70
00:04:06,059 --> 00:04:11,539
A function is a powerful tool, but it doesn't
actually do anything until you call it.

71
00:04:11,539 --> 00:04:15,759
Calling the function triggers its execution,
allowing the code within the function to run

72
00:04:15,759 --> 00:04:19,079
and perform its designated task.

73
00:04:19,079 --> 00:04:24,720
Calling a function showcases its reusability,
so you can use the same logic multiple times.

74
00:04:24,720 --> 00:04:29,820
It also enhances modularity, breaking down
your code into smaller, more manageable units

75
00:04:29,820 --> 00:04:33,600
that are easier to understand,
debug, and modify.

76
00:04:33,600 --> 00:04:39,040
Finally, it gives them the flexibility to
do their job in a variety of situations, as

77
00:04:39,040 --> 00:04:43,799
well as return values your program
can use to make decisions.

78
00:04:43,799 --> 00:04:47,660
Understanding and being conversant in writing
and using functions is an important skill

79
00:04:47,739 --> 00:04:50,660
to develop for Python programmers.

80
00:04:50,660 --> 00:04:55,260
Using functions enables you to build more
structured, efficient, and adaptable code,

81
00:04:55,260 --> 00:04:58,059
which helps you become a more
proficient and versatile developer.
