Skip to main content

Javascript 103 : Functions

A function in javascript (or any high level language) is a  block of code segment which carry out a certain task.

Once created, the function is not going to do anything by its own. You have to call it in your code use its functionality.

You have to master functions and all other advanced forms of it which is coming up in the later posts in order to be a good developer because you can never avoid them and it is fundamental to any development work. Unless what you develop is a "Hello world!" program!

Here's how you can declare a simple function.
What I have here is a simple function called "doSomething", and it will just print "Hello world" on the console.

But when you run this script, no error will show up, but nothing will print in the developer console either.

That is because you have to call the function in order for it to come to life and start functioning.

You can call the function like this,

And it will give you the out put as this.

But, printing "Hello World" is pretty basic and we need to definitely go beyond that. Let's do something complex.

A function,
01. May or may not accept some input. 
02. Then do something in the function body.
03. And may or may not output a result.

According to above 3 points, it is understandable that a function always carry out a certain task.

The function "doSomething" we developed above, did not accept any input, neither it returned anything. But it did something. That was, printing out "Hello World".

Lets actually change the function to return the string "Hello World" rather than printing it.  

If you run this code, no error will be given but, nothing will happen either.

That is because, now you return the string but you don't do anything with the returned value.

Lets capture the returned value to a variable and print it out.  It goes like this.

And it will give you this output.

Now in this case, all we did inside the method body is returning a hard coded fixed value. Now lets do something dynamic.

Lets, pass a value in, and then do a calculation, and return the result.

Study this function carefully.

The function "adjustForTax"
01. Accepts and input (it is called a method argument. A function can accept any number of arguments, but in this case it is only one).
02. In the function body,
      02.01. There is a constant which hold the tax rate.
      02.02. The "afterTax" variable holds the adjusted value. (Carefully study the calculation)
03. Finally, the function returns the result as a string statement.

And it will give you this output, once you call it and print out the result.

Now lets take this a step further,

At the moment, this function only works for a tax rate of 12.5. because it is hard coded, and it returns a String statement. Lets change it like this.

It is a simple modification, but very powerful.

Now you can use this function to calculate the final amount for any tax rate.

Watch how I called the function. I stored the values, amount and rate to variable and I passed them in as the function arguments.

Moreover now the function does not return a String, but it returns the adjusted amount. Which means you can use the returned value for another calculation.

Lets say, Your government is really rude and they do chained taxation. Means, hypothetically, when you buy something on Ebay, your government will,
01. Charge 12.5% VAT for the purchased amount.
02. Add 8.65% "Trade Tax" for the value adjusted after VAT. (Screw that government!)

You can implement this easily using this function.

See how the function being called twice on line 12 and 13.

You will get the output as follows.

This was a very short introduction to functions, but we will get into more details in the upcoming posts, when we discuss closures, callbacks and promises.

Happy learning!

Comments

Popular posts from this blog

Javascript 105 : Callbacks

Javascript is asynchronous. That means you cant guarantee that the code you write will always execute sequentially. Take the following code for example, Output When looking at the output, it is understandable that the statements has been executing sequentially.  But if I do a modification to the code like the following, the output will be different.  Output:  Don't worry about the line number 6 yet. the amendment there will just delay the the execution of the statement "console.log('statement 01')" by 100 milliseconds. Now the "statement 01" prints out at the end of the line. The change of the output can be explained as follows. the JS interpreter sees the line 6 (which is the first line of the script), and realize that it has to wait for a 100 milliseconds before executing the statement "console.log('statement 01')", but sees that other four lines has no such delay and can be executed right away. So it goes a...

Javascript 102 : Understanding 'this' keyword

The keyword "this" is frequently misunderstood when coding with Javascript. It is because its behavior depends on the execution context. Now what is "execution context"? It is mainly of three types. Global Execution Cotnext (GEC), Functional Execution Cotnext (FEC) and Eval. In GEC, it is the global environment which Javascript  (JS) is running. If you are running the script in a browser and you use the "this" keyword, it will refer to the "Window" object. But if you are using NodeJS and running the script outside a browser the "this" keyword would refer to the "Process". In FEC the "this" keyword would refer to the execution context created by the code inside the function. EVAL is hen you use the "this" keyword inside an "eval" function. If this does not make sense, don't worry because we are going to understand with an example which is always the best way. Make the following HT...