Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We've focussed on objects, classes and operators so far, but a major powerhouse is lurking just around the corner. Let's invite methods into our code and see how we can give functionality to those classes. We'll cover topics like return types, parameters and arguments as we begin exploring this critical construct.
-
0:00
For much of our journey so far, we focused on the things or the nouns of programming.
-
0:05
That is to say, things like integers and strings or arrays and
-
0:08
dictionaries, classes and objects.
-
0:10
These data types are critical to writing code and
-
0:13
in our woodworking analogy from long ago, they would represent the wood nails and
-
0:18
screws that might be used to construct a bird house.
-
0:21
We also spent quite a bit of time learning about operators such as addition or
-
0:25
subtraction, ifs and ors.
-
0:28
Those can be thought of as hammers and saws.
-
0:30
They're are simple tools that allow us to introduce verbs, or
-
0:33
do things to our nouns.
-
0:35
They allowed us to drive in nails and
-
0:37
to cut wood that was certainly a jump in capability, but quickly we started linking
-
0:42
those operators together to create greater functionality like calculating a discount.
-
0:48
As you might guess, doing things like calculating a single discount
-
0:51
are great for learning the ropes, and they do embody many real life coding tasks, but
-
0:56
very often the processes we create are more complicated or have more steps.
-
1:00
For instance let's say instead we want to tally the entire bill,
-
1:04
create a PDF of the receipt, and then email that receipt to the customer.
-
1:09
In this case what we're really saying is that we don't just want to cut wood or
-
1:13
drive in a nail.
-
1:14
But we want to build the whole bird house ideally simply by saying build me a bird
-
1:18
house that command, build me a birdhouse, may trigger more complicated
-
1:23
functionality behind the scenes but from the outside, it's a simple command.
-
1:28
For this we'll call upon the fearless function and
-
1:30
the mighty method, now functions, and methods are very similar.
-
1:35
They're both reusable chunks of code that perform actions.
-
1:38
The difference is, methods belong to classes.
-
1:42
Say with me, the difference between methods and
-
1:44
functions is that methods belong to classes.
-
1:48
Now that you know that, which do you think will use more often in Objective-C,
-
1:52
methods or functions?
-
1:54
Well since we've been programming in an object oriented language and
-
1:58
objects are just instances of classes
-
2:01
It seems that methods will be the overwhelming choice.
-
2:04
With that in mind, we'll spend the vast majority of time working with methods, but
-
2:08
almost everything you learn will be applicable to functions as well.
-
2:12
So, I mentioned that methods are the doers of programming.
-
2:15
They allow us to manipulate our objects and
-
2:17
variables, like verbs manipulate nouns.
-
2:20
They provide the functionality we need to give our code real power.
-
2:24
If we create a car object the methods allow us to start our car turn left brake
-
2:29
and depending on where you live honk your horn incessantly.
-
2:33
It's important to note that when we tell our car to stop or
-
2:36
go we don't need to know what's happening under our foot or under the hood.
-
2:40
We just press the pedal and the method handles the rest.
-
2:43
This is a critical thing to grasp because often you'll need to use classes that were
-
2:47
created by another developer, or perhaps included within an apple framework, and
-
2:52
you should feel comfortable calling methods on those classes
-
2:55
without understanding the inner workings of the methods themselves.
-
2:59
But let's not get ahead of ourselves.
-
3:01
Let's look at some examples that might help us better understand what
-
3:03
methods can do and different shapes methods can take
-
3:07
speaking of which let's start with something simpler than a car drive.
-
3:13
Yes I see the cliff.
-
3:14
All right let's pretend our object is something simpler than a car,
-
3:19
namely a coffee maker.
-
3:21
Now a coffee maker can perform a number of actions which are analogous to methods.
-
3:27
In the simplest case, we can turn our coffee maker on.
-
3:30
In code we'd write this something like (void)turnOn coffeeMakerIsOn
-
3:36
= Yes; If we translate that, we'll see we have a method called turnOn.
-
3:41
When we call that method the bool coffeeMakerIsOn will be set to yes, and
-
3:46
yes this assumes our coffee maker is already plugged in.
-
3:49
Now let's talk about two other parts of the method syntax.
-
3:52
First the void, what you see inside those parentheses is the return type, the reason
-
3:58
that it is void, in our case, is that this method doesn't return anything.
-
4:03
Now it still does something really important, it turns our coffee maker on.
-
4:07
But it doesn't actually spit anything out, it simply changes the state of the coffee
-
4:12
maker object from coffeeMakerIsOn = NO to coffeeMakerIsOn = YES.
-
4:18
Now, thankfully, a coffee maker can do a lot more than just turn on.
-
4:22
Specifically, it can make precious, precious coffee, to do this,
-
4:26
a coffee maker takes three inputs, coffee grounds, water and electricity.
-
4:31
With those it produces one output, piping hot, delicious, blessed coffee.
-
4:36
Which, by the way, is about as essential to software development as methods,
-
4:40
variables, functions and
-
4:41
classes all put together but that's a lesson for another time.
-
4:44
Anyway, when we make a pot of coffee, we explicitly feed it the coffee grounds and
-
4:49
the water, but the electricity, the machine knows how to get on its own,
-
4:53
assuming it's already plugged in, of course.
-
4:57
In pseudocode, we could say that
-
5:00
the method makeCoffee takes the input grinds of type CoffeeGrinds and
-
5:05
freshWater of type water the body that's everything inside
-
5:10
the curly braces says that we can create delicious coffee of type coffee and
-
5:16
set that equal to the sum of grinds plus water, plus electricity.
-
5:21
Lastly we return delicious coffee which is of course of type coffee,
-
5:26
just as specified in our return type.
-
5:29
By the way, all I have shown you is how we define a method.
-
5:33
We haven't actually shown how to call the method but
-
5:35
don't worry, that's actually significantly simpler.
-
5:38
Now in programming we use the words parameter
-
5:41
to describe the name of the variable we'll be passing in.
-
5:45
So in our case the grinds and freshwater are our parameters.
-
5:50
Later when we call the method the values that we
-
5:53
actually pass into the method will be called arguments.
-
5:57
A trick to help remember that difference is that arguments are actual.
-
6:02
So here we see a method which has an explicit return type and
-
6:05
several explicit parameters.
-
6:08
The syntax may look a bit confusing, but
-
6:10
as we work through some examples that will become more familiar.
-
6:14
We just took a deep theoretical look at what methods are, what they can do and
-
6:18
what shapes they might take in terms of inputs and
-
6:20
return types, this is a big idea.
-
6:23
So I don't expect you've internalized it all just yet, with that in mind,
-
6:27
please join me for some hands on experience in creating methods.
You need to sign up for Treehouse in order to download course files.
Sign up