2.5 Python Functions - IP
Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or
Already have an account? Sign In »

Video Transcription
00:00
>> Hello, everyone and welcome to intro to
00:00
Python here on Cybrary On-demand.
00:00
I, as always, I'm your instructor
00:00
Joe Perry and I as always,
00:00
I'm very excited to have you here.
00:00
This is one of the Python lessons that I
00:00
really love to teach because
00:00
functions are the fundamental component
00:00
of really every programming language.
00:00
A lot of the things that we've touched on in
00:00
previous videos are going to make more sense
00:00
now and you're going to be able to do
00:00
just so much more with
00:00
Python after this video and after this lesson,
00:00
now that you will actually understand what functions are,
00:00
how they [NOISE] work, how they're used and
00:00
all the incredible things you can do with them.
00:00
The objectives of this lesson,
00:00
we have three of them.
00:00
We're going to learn to write Python function.
00:00
Instead of working in our shell,
00:00
we've actually been spent a lot of time
00:00
working on an actual Python file.
00:00
To do that, we're going to learn
00:00
about input variables which
00:00
are called arguments and we're going to learn
00:00
about the output from functions,
00:00
which is called the return.
00:00
We've got our terminal open, we've got our
00:00
VM open, we're going to do vim.
00:00
We're going to make sure we're on the right lesson here.
00:00
The right lesson folder,
00:00
because I don't think we are, we are not.
00:00
CD, this is not important to those
00:00
of you working at home unless you've
00:00
reconstructed my file structure.
00:00
But I like to be in the right lesson folder,
00:00
it just makes me feel a little bit better.
00:00
Less than five PWD, right place, cool.
00:00
We're going to do vim, and we're going to do funcs.py.
00:00
First thing as always,
00:00
we're going to add our shebang line/user/bin/python3.
00:00
Now, I mentioned that
00:00
there are inputs and there are outputs to functions.
00:00
The way we create a function in Python,
00:00
the first thing we're going to do is we're going to use
00:00
what is called the def keyword.
00:00
What deaf says it's
00:00
a shorthand of the Python interpreter that says hey,
00:00
then the rest of this line is defining a functions.
00:00
Def means defined.
00:00
Def func_1.
00:00
Function is a keyword in Python that
00:00
can get a little bit weird if you use it too much.
00:00
We'll call this func_1. Then we're going to give
00:00
it just a pair of parentheses with no other information.
00:00
Then we're going to have this return line.
00:00
I'm going to break this out piece by piece.
00:00
[NOISE] Def, function definition,
00:00
keyword, func_1, function name.
00:00
Again, just like with any other variable name,
00:00
this function name can be whatever you want to call it.
00:00
You can call it foo, bar,
00:00
baz, whatever you want it to be named.
00:00
That's just the name of your function.
00:00
Parenthesis, they are
00:00
the indicator that there is a function and
00:00
the values inside of them are called the arguments.
00:00
Here we'll actually go ahead and put in
00:00
a couple of arguments so that you can
00:00
see what those look like.
00:00
A and B.
00:00
These variables are just like any other Python variable.
00:00
They are Tupperware for your brain.
00:00
They're just an abstract data storage location.
00:00
What's really useful about
00:00
function arguments is that they are used to tell
00:00
your function what logic you're going to be
00:00
performing and what you're going to be
00:00
performing that logic against.
00:00
Here now that I've identified that those are arguments,
00:00
we can see that for example,
00:00
we might want to say c equals a plus b.
00:00
Now this function, what it's actually going
00:00
to be able to do is
00:00
get these arguments from
00:00
whoever is calling this function,
00:00
whoever is actually using this function.
00:00
It'll take those arguments,
00:00
it will perform logic against them,
00:00
and it will give you information based upon them.
00:00
You've got c equals a plus b,
00:00
but the problem is that we can't use that
00:00
right now because of
00:00
the fact that it's still in that function,
00:00
we are not printing or we're not doing anything with it.
00:00
We need to use our return argument.
00:00
Now, return by itself is
00:00
a keyword that you can use just to say,
00:00
this function is done executing.
00:00
It is the indicator [NOISE] of end of function.
00:00
But additionally, you can give
00:00
return an argument essentially on that line,
00:00
even though it's not in
00:00
parentheses like a function would be.
00:00
But you give it a statement on that line, for example,
00:00
return c. In this case,
00:00
returns c means provide the value of c to the color.
00:00
The color is what we referred to whatever line of
00:00
code actually executes this function.
00:00
The way you're going to call a function
00:00
is just by addressing the way we've done with
00:00
print and with a couple of
00:00
other functions and a bunch of other videos.
00:00
You're just going to say the function's name,
00:00
func_1 with parentheses and the arguments.
00:00
In this case we're going to say func_1
00:00
with the arguments of 1 and 2.
00:00
Because we're in our script,
00:00
we're going to go ahead and use print
00:00
here to print the result.
00:00
Now it's worth noting,
00:00
what you can see here is that printing is
00:00
now a function that's got a function inside of it.
00:00
The way this is going to happen is it's going to execute
00:00
this internal function and the it's going to call
00:00
print with the return value.
00:00
You could also see this as ret_val
00:00
equals func_1, print, ret_val.
00:00
Here I'm going to tie
00:00
this down a little bit so you can see it.
00:00
That's how you assign the value of
00:00
a function's return to another variable.
00:00
You just give that variable and you'd put
00:00
an equal sign and then you call the function.
00:00
Here we have print, ret_val,
00:00
and then we're just going to close this script and
00:00
we're going to execute it and see what it does.
00:00
There you can see it prints the number 3.
00:00
We can go back and we can look and
00:00
logically understand how that's happening.
00:00
We give it the arguments of one and two.
00:00
The internal logic of the function is,
00:00
sorry, my Vim is jumping around,
00:00
c equals a plus b,
00:00
return c. We store that return in our ret_val,
00:00
and we print the ret_val.
00:00
Now it's important to note that in Python,
00:00
if you have this function,
00:00
but you never actually call it,
00:00
[NOISE] nothing is going to happen.
00:00
Because of the fact that your Python interpreter is
00:00
going to open this script and say,
00:00
yeah, I see a function defined,
00:00
but I never said that function called or used anywhere.
00:00
Nothing is going to be executed.
00:00
In Python and in most programming languages there
00:00
is a standard,
00:00
a commonly used function called main.
00:00
What main is, generally speaking,
00:00
is going to be the actual
00:00
primary logical flow control for your program.
00:00
With main, [NOISE] you might call
00:00
your other functions with some number of arguments.
00:00
You might repeatedly call them,
00:00
you might do something with them.
00:00
But generally speaking, main is going to be what
00:00
controls the flow of your program.
00:00
At the very bottom of your program,
00:00
we're just going to write main and actually code it.
00:00
Now, the reason why you use functions is actually
00:00
really well illustrated by these two lines of code here.
00:00
Now, I'll comment these out real fast and show you.
00:00
I could have done print 1 plus 3,
00:00
print 2 plus 4.
00:00
I would generally get the same result as
00:00
printing these two return variables.
00:00
But that only works because of the fact that we're doing
00:00
a very simple piece of work here.
00:00
If we were, for example,
00:00
trying to do c equals a plus b,
00:00
d equals a minus b,
00:00
and e equals c to the power of
00:00
d. Those three lines of code,
00:00
we don't really want to write every
00:00
single time we're trying to put them.
00:00
We don't want to have to rewrite it.
00:00
That's the actual true use and power of functions,
00:00
is that they create reusable,
00:00
discrete chunks of code that
00:00
will do all of the work for us.
00:00
You only have to write your code
00:00
once if you put it in a function,
00:00
instead of having to continually rewrite it over
00:00
and over again just to perform some operation.
00:00
We have our return values,
00:00
we have our arguments, and we
00:00
have our actual functional code.
00:00
We now understand the concept
00:00
of writing logic inside of a function,
00:00
using the arguments and then
00:00
returning the result of that logic.
00:00
In your lab, which is going to be
00:00
your next opportunity to really work with this,
00:00
we're going to be going over Lessons 1
00:00
through 5 of Module 2.
00:00
These variables are going to learn to implement loops,
00:00
if statements and functions in Python.
00:00
Again, if you're an inside or pro,
00:00
you're going to get to do that in our next tech lab,
00:00
which is going to be right next to me.
00:00
Well, I guess I can't point
00:00
now because I'm not onscreen,
00:00
but it'll be in our supplemental materials.
00:00
If you're not an insider Pro
00:00
in that supplemental materials,
00:00
you will see the lab assignment,
00:00
and you will see my solution code.
00:00
Even if you're not an insider Pro,
00:00
you can still perform the assignment.
00:00
But I do highly recommend if you can then
00:00
use the next tech lab that I created for this course.
00:00
It's just absolutely spectacular and it can be very,
00:00
very useful for performing this code.
00:00
Once you come back from doing that,
00:00
we're going to start in on the second half of
00:00
this module in which we're going to be performing
00:00
deep dives of all the Python datatypes that we
00:00
very briefly addressed in Module 1.
00:00
You're going to learn all about them.
00:00
Next lesson is specifically going to be
00:00
about using strings in Python.
00:00
I'm very excited to teach you as always,
00:00
I have been your instructor, Joe Perry.
00:00
Thank you for watching intro to Python
00:00
here on Cybrary On-demand.
Up Next
Similar Content