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 101 : var, let and const

Let's understand variable types "var", "let" and "const" in Javascript.  Basically there are 3 ways to create variables in java script using 3 keywords as follows.  There differences are as follows,  01. The "variable01" which was created with "var" keyword, does not have a blocked scope. which means, you have to be careful when using it. A variable declared inside an "if" block can be changed outside the block.  02. The "variable02" which was created with "let" keyword, has blocked scope. A variable with "let" keyword declared inside an "if" block is not available outside the block. 03. The "variable03" which was created with "const" keyword, is a constant. Which means you have to initialize it once you declare it, and that value cannot be modified later in the programs. There will be an error if you try to do so.  Now lets explore