Time
2 hours 57 minutes
Difficulty
Beginner
CEU/CPE
3

Video Transcription

00:00
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
00:14
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
00:33
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.
00:49
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
00:59
so seedy.
01:00
This is not important to those of you working at home unless you have reconstructed my file structure.
01:04
Um,
01:04
but, you know,
01:07
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.
01:12
We're going to do them, and we're going to do funks not.
01:15
Hi.
01:17
First thing is always we're going to add our shebang line. User Been Python
01:23
Python three.
01:26
No,
01:27
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.
01:45
So death
01:46
function
01:47
one,
01:49
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,
01:57
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.
02:02
I'm going to break this out piece by piece. So
02:07
death
02:08
function, definition,
02:12
key work,
02:15
funk, one
02:16
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
02:28
Parentheses.
02:29
They are the indicator that there is a function, and the values inside of them
02:32
are called the argument.
02:40
And here will actually go ahead and put in a couple of arguments that you can see what those look like
02:49
A and B.
02:55
So these variables are just like any other python variable there, Tupperware for your brain. They're just an abstract data storage location
03:04
was really useful about function arguments
03:07
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.
03:14
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.
03:23
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.
03:35
So you've got C equals a plus B.
03:38
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
03:45
our return argument
03:46
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
03:54
of end of function,
03:58
but additionally,
04:00
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,
04:10
and in this case, were turned see
04:13
means provide the value of C
04:16
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.
04:30
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
04:44
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
05:02
Brett vowed equals
05:05
funk. One
05:10
print Brett
05:12
Go.
05:13
And that's the way you assign the return of the function here.
05:16
Tablets down a little bit so you can see it.
05:18
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
05:28
here we have print ready, Val,
05:30
and then we're just going to close the script. We're gonna execute it and see what it does
05:42
there. You could see it prints the number three and we can go back and we can look and logically understand
05:47
how that's happening.
05:48
We give it the arguments of one and two. The internal logic of the function is C equals a plus.
05:55
Sorry, my name is jumping around Siegel's a Plus B returned. See, we store that return in a red vow
06:00
and we print the Red Bell.
06:02
Now it's important to note that in python, if you have this function,
06:08
but you never actually call it,
06:18
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.
06:28
Nothing is going to be executed
06:30
in python, and in most programming languages there is a
06:33
There is a
06:35
a standard sort of ah commonly used function called Maine,
06:41
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,
06:50
um,
06:58
you might call your other functions
07:00
with some number of arguments
07:11
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.
07:20
Now,
07:21
the reason why you use function is actually really well illustrated by these two lines of code here
07:28
because I could have done
07:30
no comment these out real fast and show you
07:32
I could have done
07:39
Prince
07:40
one plus three
07:42
Prince two plus four.
07:44
I would generally get the same result is printing these to return variables?
07:47
But
07:48
that only works because in fact, we're doing a very simple piece of work here.
07:53
If we were, for example, trying to do
07:56
C equals a plus, B
07:58
d equals a minus being
08:01
and e equals See the power of D,
08:05
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.
08:22
You only have to write your code once if you put it in a function,
08:24
instead of having continually re write it over and over and over again just to perform some operation.
08:33
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
08:45
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.
09:05
Additionally. Well, I guess you can't point now that I'm not on screen.
09:07
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
09:18
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.
09:31
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.
09:50
Thank you for watching Internet Python here on Cyber eri on demand

Up Next

Intro to Python

This is an introductory course on Python for cyber security, giving students the ability to understand the basics of the language, solve problems with scripts and identify useful Python modules.

Instructed By

Instructor Profile Image
Joe Perry
Senior Technical Instructor at FireEye, Inc
Instructor