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