What Exactly Are Functions

A function, as the word describes it, is a piece of functionality.
If a function was a closed magic box we would only see three parts to it:

(1) an input mechanism that we use to insert data into the function.
(2) the function box where magic happens.
(3) an output from the function with some result.

This is a function in JavaScript:

var xyz = function (input1, input2) {
return input1 + input2;

When we look at our xyz function we see two input mechanisms, input1 and input2. We call these inputs “parameters“.

We also see an output mechanism: return

A return outputs the result of the calculation in its raw form. Which means that we can actually grab the data from the return and reuse it on some other script.

Sometimes we use console.log() instead of return. console.log() is very limited because it only returns data as a string for display purposes. We can’t use this data for further processing.

So if the purpose is to display it, console.log is sufficient.
If the purpose is to use the data for something else, then we need to code a return in order to fetch the data from the function.

How do we use a function?

That’s when the variable xyz comes into play.

xyz points to the function so that a human can hardwire a function call.

We call it this way:

xyz( );

That’s it! That’s how we call a function.

However, this particular function wants us to input something in order for it to do its magic.

So we need to pass some values when we call the function.

This is how it is done if I want to add 3 + 7:

xyz(3 , 7);

By the way, 3 and 7, the data being presented, are called arguments. Arguments are passed into a function via the function parameters which act as an interface between the function and the outside world. (To be more precise, between the stack and the heap).*

Now the function is happy because it has some food for thought.

It grabs a copy of 3 at input1, and a copy from 7 at input2, and it processes its internal logic which is an addition operation.

Then it returns the result which is 10.

The return mechanism has also a secondary job which is to halt the function after it returns. In other words, once the return returns a result, the function process stops and the browser clears the data from inside the function to free up memory.

If you love JavaScript like I do, you will enjoy reading it.
Use the free amazon “Kindle for PC” app to read it on your computer.


Author: tarau

technical writer, web technologies

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s