Hey everybody top of them muffin to you today.
Mark Price at the slopes dot com.
And today we are going to learn about functions.
Open up your X code 8 and create a new playground.
My playground we're going to call this up town function you
and I'm going to save this where I'm going to save this Iowas 10 course and swift and new folder functions
you can save yours where ever you want.
OK.
So a function is a core component in programming core building block building block in building functional
applications applications that can actually do things.
OK so here's a perfect example of why we need to use a function.
Let's say that I want to get the area of a shape or a rectangle or square.
OK so say bar length equals five bar with equals and say 10 and then bar area equals length times with.
Now you've probably done something similar to this.
Written wrote some variables.
That's math operation.
We now have the area right.
Well this is cool.
Maybe you don't see what's wrong with this.
Well let's do this now.
Now we have a new shape.
So this was shape number one.
All right.
Oh no.
We've got a new shape now.
OK.
It's another rectangle with a different size so now we're going to say OK length two equals six var
with two equals 12 and var area two equals length two times with two.
OK.
Well that's not so bad.
So this is a shape too but oh no.
Now our program requires a third shape shape 3.
And if you're asking me Is it really going to type this out again.
The answer is yes because I want you to see the brutal pain of not working with function Savar length
three equals three are with three equals eight and bar area 3 equals 0 length 3 times with 3 K.
This is lame.
This is really lame.
And anybody who programming can probably realize that this is not the best way.
You don't have to be an advanced programmer to realize something smells wrong.
This is called a code smell a code smells when something looks or smells wrong with your code.
You know maybe don't know what it is but it just doesn't feel right.
Well this violates a principle.
It violates the dry principle which means don't repeat yourself.
And we are definitely repeating ourself here.
So this is how functions can come in handy functions are a way to perform an operation over and over.
You can pass data into it return things.
In fact let's see what our handy dandy internet says about functions.
Define function in programming in computer science.
Functional Programming is a programming paradigm a style of building the structure and elements of a
computer program that treats computation as the evaluation of mathematical functions and avoids changing
state and mutable data.
So this is functional programming.
It's not functions in general but there actually is some merit to functional programming functional
programming basically means you want to create functions that can repeat operations in a mathematical
sense and don't do a whole bunch of things and manipulate all kinds of data.
They do one thing and they do it well and so that's kind of how you should be designing functions anyway.
But that's not exactly what we're looking for.
Let's go over to Wikipedia and if you're wondering why does Mark Price always pull up documentation
always go to these things because you should not be just copying and pasting code.
I'm going to teach you how to become a programmer and programmers the good programmers always learn
they read the documentation they go underneath the hood and that's what we're going to do here.
And if you don't like it just put me on to x speed and you can listen to me talk really fast just like
this.
OK so let's take a side in a function OK.
And
this is a good one too.
A function is a relation between a set of inputs and a set of permissible outputs with the property
that each input is related to exactly one output.
This is really cool how does the word function it translates into different things in math but it all
makes those very similar.
Look at this diagram right here.
A function takes input and it can also provide output doesn't always have to though and this can be
the same thing in programming.
And I would encourage you to do some more searches on an functional math functional programming.
What is a function.
Really good idea of what's going on and I'm going to show you hands on now.
OK we just took a glance at some of the things that are on the Internet.
It's always fun to do that to start educating yourself.
And so what I want you to do is create a function just like I'm going to do.
Func is how you do it in swift.
And we're going to call this calculate area.
OK so we have a function and it's going to do something.
So what we're going to do is we're going to put in two parameters and we're going to say length of time.
This can just be an int and width of type.
So this is the input.
Just like in that diagram that you saw or something go into the function then it does something and
something can come out.
We're can do that exact same thing here.
OK very similar to what you might do in some paper math and length and width and so what's going to
happen is we're going to pass in the length and passing it with it's going to calculate it and it's
going to return it's going to give us back of value.
So this is the syntax for a function.
You start off with the func keyword.
OK and then you give it a name and then you create parentheses.
OK you don't have to have parameters in.
It can be OK that it's just not doing what I want it to do.
You don't have to print this or stuff in the parentheses it can be empty like that.
But good programming or the functional programming that you want to start gravitating towards takes
inputs and returns output without necessarily modifying what you would call your variables or your properties.
And if this doesn't make any sense to set that's absolutely fine because when you watch this video again
you probably should start making more sense.
So we're going to pass in a length passen with these are parameters which means you can put things in
them and then what we're going to do is we're going to put a dash and then a greater than sign.
Kind of like an arrow right.
This is what you're going to return what data type Do we want to return where we want to return an int.
When we say let.
When a critic constant area equals length times with no that's just a simple math operation right.
Length times with.
But then what we're going to do.
We're going to return the area.
So what does this all mean.
OK what's going on here.
Well you don't know because we haven't put it into action yet so what we're going to do is we're going
to say var actual say let.
Area equals calculate area so we're going to call our function.
Notice how it's appearing here on the autocomplete.
It is appearing it's blue.
It has our parameters in it.
This is great so we're going to say calculate area and we're going to pass in a length and width.
So let's say 5 and then with this for k Kukla air with five and four.
And we already have an area means that it doesn't like that because we already have an area I'm going
to call this new area.
OK.
So let new area calculate area and look at this.
It's giving us the proper result of what is happening here what is going on.
Well let's see if I can show you.
So we're calling a function or passing in a 5.
So the second that we pass it to 5 it's taking that and dropping it here in this bucket and calling
it length so it's taken this value in putting it in a new variable called length.
Actually this is a constant you can't change these values OK once they're set in there and then it's
taking this one and it's passing it in here and giving it a name.
So the values you pass in it gives it a new name puts it in a new constant.
You can use inside of your function and then we take the length and the width and it performs a math
operation and stores the result here in this constant and then using the return keyword.
OK.
We actually return something.
So what happens is this is called and then goes right into here.
OK so we pass in the length we pass in the with it performs an operation here and then it returns that
operation or the value of the operation it returns here into our variable.
So now we can use that.
And this is really really cool.
And why is this cool.
Because we don't have to write all this crazy code now pay.
We don't.
The more you have to write code the more chances you have of screwing up.
So what if I was writing the third area here and actually did a minus sign.
Well that's no longer the area is it.
I just broke our code because I wasn't paying attention maybe a sneeze or whatever.
I had to write more code and it created more possibilities for mistakes.
So now if I want to get three different shapes that let's just call this shape one OK let shaped two
we can just now call the function and all the code inside that program just crash.
But all the code inside
you don't ever have to write again.
And that's the beauty of functions open recent.
Now we go so calculate area and this and we're going to say 6 and 2.
OK.
Cool stuff.
So now we've only written the code once inside and it's been used multiple times.
So you're already starting to see the value of a function.
It lets you take input perform operations mathematical or other operations on that input and returning
the result of that so somebody can use it and this is a nice clean function.
And if we really want to clean things up and save some space we don't even have to store this in a constant
like this.
We can actually just say return length times with like so and it's going to perform the operation and
then it's going to return automatically.
And so we don't have to change anything else.
Behere we just cleaned up a little bit of code and this is more common for simple operations.
If you have multiple multi-line operations you have to do then of course stored in the content like
we had before.
So again you take values you pass it into these parameters here also.
That's the keyword.
OK.
Write this down if you're new to programming write this down.
These are parameters also known as arguments.
Some nice people use those terms interchangeably.
Also another thing to write down functions are called functions in some languages and methods in other
languages.
OK very important you might write that keyword down as well too.
Over here on the right hand side is the return statement and executing that return statement the return
type.
OK this is where you specify the return type.
And this is the type here dash greater then sign like an arrow.
And then if you are returning something you must use the return keyword to return a value of that type.
Notice how we couldn't return a string if we wanted to I couldn't say hey you let's try that.
Cannot convert return expression of type string to return type and it must be an integer.
OK this is no bueno.
No good.
OK.
And so we've already saved ourselves some errors by using a function here which is pretty cool and you've
just learned really the core features of a function of what you would use it for and these are used
for everything.
Anything you can think of.
For instance let's talk about bank accounts.
So let's say var say bank account balance equals five hundred zero zero which is a double.
OK.
And then what we do is we say var what you want to.
What do we want to buy something cool.
So yeah.
So the other day get this the day Reebok announces the release of you know the alien Stomper shoes the
shoes that Sigourney Weaver wore in the alien aliens movie.
OK.
And also the Android war of them.
You know 30 years later they're rereleased in the shoes right.
I was all excited ready for it.
Went on the one onto the Web site the second and went live.
And then guess what happened because there's only 2000 shoes.
A bunch of nerds that should have been me because I'm a nerd.
Many nerds with their bots bought up every shoe and then sold them on eBay for like 10 times the amount
of money.
That's all irrelevant.
So my point is that's what we're going to buy today is
you know we're going to buy Sigourney Weaver Alien Stomper shoes.
OK you got $350 now.
She's OK.
So we want to buy these items right.
So you know we could write out some logic here that says you know if you have enough money buy the shoes
and then do another item but what if we had a function that could just purchase an item.
OK.
So let's do that now.
Funk where you say purchase item.
OK.
And what we're going to do purchase item we're going to pass in the current balance type double.
And the item price of type double.
Okay.
And I'll come back to the return statement later.
What we're going to do is say if item price is less than or equal to current balance.
Okay
that's what I'm looking for.
Well we're going to we're going to return.
We're going to return the new balance that you have because you just purchased the items.
Maybe we'll print something too just for fun.
But anyway this is generic It doesn't matter if it's Sigourney Weaver Alien shoes or if it's you know
Teletubbies limited edition lunchpail box.
OK.
From Nineteen ninety eight.
And anyway if item price is less than or equal to current balance you guys are like Do you even hear
the words come out of your mouth Mark.
No.
The answer is No I just say whatever's there.
So now what we can do is return if the prices aren't equal to current balance we can return current
balance minus item price returned the New Balance.
Okay.
And then let's make a return statement.
We're going to make this type double OK.
Like so and then what we do is we're going to say if you can't.
So if it's not less than or equal to then we're going to just return the current balance as it is.
OK.
And what we can do here is say print purchased item for and we can use string interpolation which is
a key word you should have memorized.
Purchased item for item price and we can even put a little dollar sign in front of it.
That's kind of cool I like that.
And otherwise here
you are broke.
You should learn how to save money.
OK.
And so this is cool.
Now what we can do is we can call that function we can say purchase items so purchase item and we can
pass in the narratives we want our current balance so we're going to call it we're going to pass and
bank account balance and then also wants the price of the item.
So this is Sigourney Weaver Alien Stomper shoes.
OK.
And so.
And it's always going to return to us our current balance and now we can say let New Balance equals
not to be confused with the shoe new balance because clearly we're buying Reebok's Reebok so purchased
item for $350.
And notice how the balance is now changed.
Now we're we're being smart.
We're working with functions we're putting in input it's returning things.
And now you know that's our new balance right there are we could well we could have done is instead
of just saying New Balance we could have just said bank account balance we can override it.
Now equals purchased item and then it again remember this is returning something.
OK let me just show you again in case you forgot.
So this return statement right here is returning a value and that's coming in right here.
Okay.
Also keep in mind whenever you call return that is the end that is the end of that function nothing
else will be called after return.
So if it gets here nothing else is going to be called after it.
So if I write code here you know like a print statement you know.
Hello Nurse.
All right.
That is never ever ever ever ever going to be called and he even gives me a warning code after return
will never be executed.
OK return statements and your function.
OK.
So now now we're being smart you know.
And so now we can buy whatever we want.
You know we could save our you know retro lunch box you know equals you know $40.
All right.
So now we have to do is save bank account balance equals purchase item pass in the current bank account
balance and then the item price is you know the retro lunchbox and boom.
OK.
Our bank account balance has now been updated and it's $110.
And we bought we bought her item.
See how it's reusing the code.
We don't have to rewrite things over and over and over again.
That is the beauty of working with functions.
OK now one more thing I want to show you.
This is a little more on the advance side but I just think it be good to know is that you know how I
said these are constants right.
You can not modify these values for instance I cannot say current balance equals zero.
Let's see what happens if I do that.
He yells at us can assign value to current balance is a letter or constant.
Well what if I may not recommend doing it for the specific function function but what if I didn't want
to pass in the current balance.
OK what if or what if I didn't want to return the new balance because it just sounds silly.
I'm purchasing that item why you give me the new balance seems a little bit silly so well we can do
is return the or remove the return statement.
OK.
So instead of returning this here and returning this here what we can do is we can use a keyword here
called in out.
OK current balance in out what it's going to do is it's actually going to modify the original variable.
Okay I'm going to get rid of these 2 x we don't need to assign a new value anywhere.
Okay purchase item and we'll come back to those in a second so we can use the in out keyword which will
allow us to modify the bank account balance directly which is kind of cool.
Now you want to use it sparingly.
But I'm just showing you you can use this.
And so what we can do is we can put the same math we had before except just adjust the existing parameter
here.
Remember how it was a constant we couldn't change it but now we can and that's done by putting the in
out before the type right there.
So now we can say current balance equals current balance minus item price.
Okay.
And so it allows us to modify the variable directly.
Pretty awesome right then right here.
We don't have to do anything except print.
We don't have to return any things is great.
And then over here we can reduce our functions or purchase item.
And what we're going to do is we're going to pass in the bank account balance or the the bank account
balance current balance is what is going on here.
No silly it's being silly bank account balance and item price is going to be the Sigourney Weaver issue
right.
Pretty cool right.
And so all you do is you put that ampersand symbol here.
When you're when you're doing it now when you're doing it out OK put the ampersand symbol before the
variable you pass in and that lets you modify the variable directly.
So we didn't even have to return anything.
So it just whatever you put in here it modifies this directly.
OK.
Again there is use cases for.
I don't recommend using this all the time but I want to show you that it's in your toolbelt of things
that you can do.
And otherwise you know you probably want to go back the way we had it which is just fine and that's
functions.
Creating something that's reusable that performs an operation over and over again.
It's it's something that you're going to use every single day and it's going to take a while to get
this down.
I know that if you especially if you're new to programming and it's fine.
Just keep practicing you'll get better with it and that's it functions.
I'm trying to think of anything else I could say about it but I doubt it.
Ninety nine point nine percent of everything you'll do every single day.
We just did.
So that's it.
Mark Price of slopes dot com.
See you next time.