Hello, everyone. And welcome to intro to Python here on Cyber Eri On demand I has always in your instructor Joe Perry and I as always and 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. And a lot of the things that we've touched on in previous videos are going to make more sense now. And you'll be able to do just so much more with Python after this vis after this video And after this lesson, now that you will actually understand what functions are, how they work, how they're used in all the incredible things you can do with the
So the objectives of this lesson we have three of them were going to learn to write python function. So instead of working in our shell, were actually spent a lot of time working on an actual python file. To do that, we're gonna learn about input variables which are called arguments, and we're gonna learn about the output from functions which is called the return.
So we're we've got our terminal open. We got our bmo what we're gonna do vent what? We're gonna make sure the right lesson yet, right? Like the right lesson, Boulder, Because I don't think we are. We are not
This is not important to those of you working at home unless you have reconstructed my file structure.
I like to be in the right lesson. Folder it It just kind of makes me feel a little bit better. So less than five feet of beauty, right place. Cool.
We're going to do them, and we're going to do funks not.
First thing is always we're going to add our shebang line. User Been Python
I mentioned that their inputs and there are outputs to functions the way we create a function of python. The first thing we're going to do if we want to use what is called the death keyword and what death says it's a shorthand to the Python interpreter that says Hey than the rest of this line is defining of functions. Death means defined.
uh, actually call it Funk. One function is a key word in python that could get a little bit weird if you use it too much. So we'll call this Funk one,
and then we're going to give it just a pair of parentheses with no other information. And then we're going to have this return line.
I'm going to break this out piece by piece. So
function name and again, just like with any other variable name this function name could be. Whatever you wanna call it, you can call it food, Barb as whatever you want it to be named. That's just the name of your function
They are the indicator that there is a function, and the values inside of them
are called the argument.
And here will actually go ahead and put in a couple of arguments that you can see what those look like
So these variables are just like any other python variable there, Tupperware for your brain. They're just an abstract data storage location
was really useful about function arguments
is that they're used to tell your function what logic you're going to be performing and what you're gonna be performing that logic against.
So here, now that we've seen what? 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 gonna be able to do is get these arguments from whoever is calling this function. Whoever's actually using this function, it will take those arguments. It will perform logic against them, and it will give you information based upon thumb.
So you've got C equals a plus B.
But the problem is that we can't use that right now because the fact that it's still in that bunk about printing, we're not doing anything with it. So we need to use
now. Return by itself is a key word that you can use just to say OK, this function is done. Executing it is the indicator
you can give return a second, an argument essentially on that line, even though it's not impressive things like a function would be. But you give it a statement on that line, for example, Return, see,
and in this case, were turned see
means provide the value of C
to the caller and the caller is what we refer to. Whatever line of code actually executes this function. And the way you're gonna 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.
And you're just going to say the functions name funk, one with parentheses and the argument. So in this case, we're gonna say bunk one of the arguments of one and two. And because we're in our script, we're gonna go ahead and used 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 in sight of it. The way this is gonna happen is it's gonna execute this internal function, and that is going to call print with the return value. So you could also see this, as
And that's the way you assign the return of the function here.
Tablets down a little bit so you can see it.
Uh, that's how you assign the value of a functions. Return to another variable. You just give that variable and you put an equal sign and then you call the function
here we have print ready, Val,
and then we're just going to close the script. We're gonna execute it and see what it does
there. You could see it prints the number three and 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 C equals a plus.
Sorry, my name is jumping around Siegel's a Plus B returned. See, we store that return in a red vow
and we print the Red Bell.
Now it's important to note that in python, if you have this function,
but you never actually call it,
nothing is going to happen because of the fact your Python interpreter going to open this script and say, Yeah, I see a function defined, but I never see that function called or used anywhere.
Nothing is going to be executed
in python, and in most programming languages there is a
a standard sort of ah commonly used function called Maine,
and what Maine is generally speaking is going to be the actual primary logical flow control for your program. So with main, you might call,
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, Maine is gonna be what controls the flow of your program. And then at the very bottom of your program, you're just gonna write Main and actually called.
the reason why you use function is actually really well illustrated by these two lines of code here
because I could have done
no comment these out real fast and show you
Prince two plus four.
I would generally get the same result is printing these to return variables?
that only works because in fact, we're doing a very simple piece of work here.
If we were, for example, trying to do
d equals a minus being
and e equals See the power of D,
those three lines of code we don't really want to, right. Every single time we're trying to put them, we don't have to rewrite it. And 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 continually re write it over and over and over again just to perform some operation.
So 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 we're turning the result of that logic
in your lab, which is gonna be your next sort of opportunity to really work with this. We're gonna be going over lessons one through five of module to you're gonna learn to implement these variables. Gonna learn to implement loops if statements and functions in python again. If you're an insider pro, you're gonna get to do that in our next tech lab, which is gonna be right next to me.
Additionally. Well, I guess you can't point now that I'm not on screen.
It will be in our supplemental materials. If you're not any insider pro in that supplemental materials, you will see the lab assignment and you will see my solution code. So even if you're not an insider pro, you can still
performed the assignment. But I do highly, highly, highly recommend. If you can use the next step next tech lab that I created for this course. It's just absolutely spectacular is to 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 data types that were very, very briefly addressed in module one gonna learn all about him. Next lesson is specifically gonna be about using strings and python. I'm very excited to teach you. As always, I have been your instructor. Joe Perry.
Thank you for watching Internet Python here on Cyber eri on demand