All right.
Let's talk about functions now functions are what I like to think of as many programs.
It's like this organized block of code that you you define and then you can call it later and makes
a lot simpler than typing everything out.
So it's a lot easier to visualize this than me to just talk.
The theory of it.
So let's go ahead and let's g r our script again our script up high and then we're going to Ampersand
this one this time and we should have our python script from before I'm Mrs. scroll down to the bottom
and I'm going to add in a new section here in that section and just going to be called functions.
So if we want to print out we can print out a new line to kind of put this together and then maybe when
it's all said and done we can print a function or make a function that just prints and make it a little
bit easier for us.
So we're going to print a line here defined functions and maybe we'll just print out something that
says Here is an example.
Function so this is really just going to define the area.
If we save it while you're typing years I'm just going to print mine so well Python 3 script that pi
and you see we've got the new line here and we see here is an example function and we just start from
here.
So let's take a look at a function using something that we've already done before.
So we've got this section here and we've got name age GPA we're going to kind of reuse this and this
print my name is.
So let's go ahead and reuse this.
We're gonna say function here.
Right.
So we're going to define kind of.
Who am I.
So we're going to define this and then we're going to do two parentheses and then a colon here.
So it's going to look like this.
And what's important now is that we use indentation.
Now we haven't had to talk about indentation to this point but python is very very critical on utilizing
indentation.
If you don't use it in the right places then your program will not function.
So it's very important to know when to use indentation into indent properly so to indent.
I'm just going to tab here and I'm going to redefine everything again so we can just say name equals
Pete and we can just say age equals 30 type that wrong there and then we can just copy this print right
here so we don't have to type it all out again.
Copy and paste it and you can see define who am I.
And we're going to define in this function a name variable and age variable and then we're gonna print
this out.
So if we were to just go ahead and save this and print this Nothing nothing's here.
All we've done so far is just define this function.
We have done nothing to actually call it or or do anything and let's make a note here that this is a
function.
Right.
So when we get down towards the bottom let's go ahead and just try to call this function.
So to call a function all we got to say is Who am I and let's go ahead and save this.
Run it again and look.
It says My name is Heath and I am 30 years old.
Well what's happening here.
OK.
So we're defining a set of things to do.
Right.
This is a mini program inside this mini program.
We've defined our variables and we define an action here to take which is printing out this string.
So what it's doing is when we call this it's saying Hey I'm gonna go ahead and take all this and I'm
going to run this program and give you the result.
Now what's important to know is whatever is stored in these variables here is only stored there inside
the function.
If we were to print age from the last video it should still be thirty two I do believe.
So it's print and you'll see it's 32 even though we declared age up here as 30 that only lives inside
this function.
So I'm going to go ahead and delete this.
So again everything is living inside this function and it's a mini little program that we have here
and we just call it at a later time.
So let's go ahead and build out another function and we can kind of start to make sense of this.
So let's start with adding what are called parameters.
And will this do adding parameters here so let's make a function and this function is called add one
hundred like this OK and then the parameter I'm going to actually add inside instead of giving empty
parentheses I'm going to add in something called num.
It's just going to be for a number and then we're gonna go ahead and use our colon it enter you a tab.
And then what I want to do is I want to print no floss one hundred.
So what am I doing here.
I'm taking a number and I'm adding 100 to it.
So my function of adding one hundred makes logical sense.
OK.
So what's going to happen then if I say ad one hundred and then I specify a number in the parameter.
Well I'm going to say what if I want to add one hundred to one hundred.
Well then I should in theory print out two hundred right.
And there you go.
We have successfully printed out two hundred so we can build upon this even more so what if we want
to use multiple parameters.
OK let's say we want to do multiple parameters and we want to define an add function.
We're just gonna make an ad even that's built into Python we're gonna make our own.
So we're just going to say add x and y.
So now we have two parameters here going add our colon.
Make sure we indent and then we're just gonna say print x plus y.
Now if we go in and we add seven and seven we should get 14 and there's 14.
So hopefully this is starting to make sense.
All we're doing is building these mini programs and we can have no parameters as we saw here.
We can have single parameters or multiple parameters depending on what we need.
So let's go ahead and build a couple more out.
Just to make sense of it.
So what if we wanted to define multiply and we'll just say x y again.
What do you think that's going to look like well instead of saying print what if we say return and throw
something new at you going to say return x and y OK.
Now what happens if I call multiply and I say 7 7 again what happens here.
Go ahead and save it.
Well nothing happened.
We're not printing out to the screen.
All we're doing is returning.
So when I'm pulling this multiply in it's returning a certain number.
So I'm calling it here and it's saying hey seven times seven.
That's forty nine.
But I have no idea what you want me to do with it.
Now I could say branch multiply here and then we can see what happens.
And now you see it's forty nine.
So the return feature just allows us to return the number that is back to us.
Right.
We don't have to always print it out if we don't want to.
We can store it for later.
So that's what we can do here.
So we don't always have to print it.
We can.
We can return the number and then call it later.
So that's one way of looking at it.
Let's do one more.
How about we define square it
and we only take one number here.
So how would we do the square root of something.
Well let's go ahead and just print out x and then we're going to do by point five.
Remember the exponent with two multiplications here and then we're doing the square root.
So we're going to take point five instead of squared which would be two.
Right.
So we're taking the square root do point five.
And let's take an easy one Let's return the square root of sixty four.
So let's say square root of sixty four and see what happens.
And it returns eight for us.
Now we can make that integer if we want to and go from there.
But that's just a nice base example so lastly I mentioned that we can make a function for a new line
because we've been sitting in here and we've been just typing print and I am slow at it.
I have to sit here and think about it and then I find the correct letters and this is how you print
new line.
OK.
I'm not the best at it.
But what if instead we just did something like define new line and then we just put this here.
So then we can just call a new line from here on out.
We could say a new line.
And then when this prince will print out a new line and while it's the same kind of length ish of this
we don't have to type in these special characters look for them we could even shorten this to like an
L something like an L and then make it really simple and then we save time.
And this is where these programs come in and they save time.
So good example of a use of a function.
So I'm going to save this if you want to use this for moving forward in going on in the next videos.
Absolutely welcome to.
So if you're following along go ahead and just leave your text editor open and I will catch you over
in the next video as we start to talk about boolean expressions.