You've learned about Python's built-in functions for common tasks.
This time, we're going to take a closer look at how to create your own custom functions
for your specific needs and projects.
By the end of this video, you will be able to explain live code examples of functions
with arguments and return values, as well as explain how to call them within the Python script.
To briefly review, functions are self-contained blocks of code that perform specific tasks.
They allow you to encapsulate a series of instructions into reusable chunks, like mini-programs,
that can be reused in your program as often as you need.
Python also provides you with the ability to write your own custom functions.
The first thing you know about writing your own functions is that you need to provide
them with inputs and receive outputs from them.
These very important components are arguments and return values.
Let's take a closer look at each of these components.
Arguments are the values you pass into a function when you call it.
In other words, they provide the necessary data for the function to perform its task
and adapt to different scenarios.
If you think of a function as a machine, you need to provide inputs like the electricity
to power it and the buttons to turn it on and off.
These inputs are the arguments.
An example of this would be a function designed to calculate an employee's net salary.
The arguments for this function might include the employee's gross salary, tax rate, and
any deductions.
By providing these arguments, the function can perform the necessary calculations and
return the final net salary.
Once your function has processed its arguments and performed its task, it might need to communicate
the results back to the main program.
The results are the return values.
A return value is the output or result generated by your function.
It's like the product the machine produces.
An example of this would be a function that checks if a user's entered password meets
specific security criteria.
The function would take the password as an argument, perform various checks, length,
complexity, etc., and then return a true or false value to indicate whether the password
is valid.
This return value can then be used by the main program to decide whether to grant the
user access or to return an error message.
So how do you write a function in Python?
Let's go through it step by step.
Step 1.
Define the function.
Start with the keyword def, followed by the function name.
If it's more than one word, be sure to use an underscore.
Immediately after the name, add parentheses.
That's where you'll put and arguments, and a colon.
Step 2.
Add a docstring.
This is a clear and concise description of what the function does, its purpose, arguments,
and return value.
Make sure to put the docstring between triple quotes.
This is the documentation for yourself and other developers.
Step 3.
Write the code.
In the indented block following the function definition, write the code that performs the
desired task using any provided arguments.
Step 4.
Return a value.
This is optional, but if your function needs to produce an output, use the return statement
to send a value back to the calling code.
The last step, step 5, is saving the function.
Although this seems like an obvious step, a lot of good work is often lost because people
are in a hurry and forget.
After you have defined your function, you can call it from other parts of your program
to execute the code encapsulated in it.
This is how you reuse the function.
For example, say you have a sales team that needs to generate weekly, monthly, and yearly
sales reports.
You can create a function that takes the desired time period as an argument and generates the
corresponding report.
The last thing we want to talk about is calling functions.
A function is a powerful tool, but it doesn't actually do anything until you call it.
Calling the function triggers its execution, allowing the code within the function to run
and perform its designated task.
Calling a function showcases its reusability, so you can use the same logic multiple times.
It also enhances modularity, breaking down your code into smaller, more manageable units
that are easier to understand, debug, and modify.
Finally, it gives them the flexibility to do their job in a variety of situations, as
well as return values your program can use to make decisions.
Understanding and being conversant in writing and using functions is an important skill
to develop for Python programmers.
Using functions enables you to build more structured, efficient, and adaptable code,
which helps you become a more proficient and versatile developer.