Programming jargon crusher: functions,methods and complex objects.

You’re with me so far?

I hope so!

Let’s talk about functions.

So, what’s a function in programming?

It’s just a piece of code that does a specific task
and return a specific value.

Easy, right?

Let me give you an example.

Let’s use a standard Javascript function called round
that will round it’s argument to the nearest integer number.

An argument is just a value that we pass into a function.

A function can have many arguments, in our case we have only one.

So, to use or run this function you need to write something like that in your code:

Math.round(1.2);

Let’s ignore for a moment the fact that there’s Math. before our function’s name. I will
explain that in the moment.

So, this function will return 1, right? This is the nearest integer for our first argument which is
1.2.

Here’s the thing:
when you actually put this function into your code like that, you won’t see a thing.

Why is that?

Well, we didn’t actually save a return value anywhere. Right?

And, what do we use to store values? Do you remember?

I’m sure you do:) A variable, right?

So, if you want to actually use a value or a result of this function you would have to write
something like that:
var myroundnumber = Math.round(1.2);

Then our newly created variable called myroundnumber will store a number 1 which is the result
of our round function.

When you look at our first web mapping application will see plenty of functions like that
and variables that store their results, in our code example map_app7.html we have:
In line 14:
var mymap = L.map(‘mapid’);

It looks quite similar, right?

We have a different function, in this case it’s called map (again we’re ignoring L. for now)
and instead of having a number as a first argument we now have a text.

And if you have a look at the rest of the code, you will see that we’re using function all the time
in that way.

Of course, we have different kinds and numbers of arguments,but basically it’s all the same.

Ok, let’s clarify this Math. and L. confusion. What’s that about?

So, those are objects. Our function belong,so to speak, to two different objects.

As you remember an object is a way to group values using keys. Like an index at the back of a book.
Right?

Math and L objects are more complex in a way that instead of storing values they store functions.

The only thing that you need to know is that a function that belongs to an object is called its method.

So, Math.round is really a function that belongs to a Math object.

What about L object?

As you probably remember from our second class in L object we have most of the Leaflet’s web mapping
library functionality.

So, what you’re mostly do when you’re building a web mapping application is you use L’s functions (methods)
to create a web map, center it etc.

Ok, let’s recap:
A function is just a container for a piece code.

When you run a function,it’s like you would run a code that is inside it.

A function can have arguments which are basically values that we can pass inside a function.

After function runs it will return a value. To save it you need to store it in a variable.

Functions that belongs to an object are called methods.

Objects can store simple keys and values (numbers or text) or functions.

Ok, that’s it for now:)

Let me know if you have any questions!