What is a Function? and Function Arguments?

In this article, we will take a look at the function arguments and function. We will first understand what is function and then move on to the next topic function arguments. So you will learn what is a function? what are arguments? and what is the default argument?

What is a Function?

Now in simple word function is just the collection of statements that run as a single unit. You can take a function as a subprogram. Function start with the function keyword followed by the function name. The function is very useful when you wanted to execute the same code again and again. Without writing the same code in the separate file you can just call the function and execute the same code.

function arguments

The function will have been saved you a lot of time.

let’s take a Syntax of function

So as I said function start with the function keyword followed by a function name. After that we specify parenthesis and in that parenthesis, you can pass comma separated optional arguments. The parameter arguments are optional.

Now the body of the function is composed of any number of statements.  so this statements will not execute until you call the function. keep in mind the curly braces is not the optional part of function it is necessary to specify curly braces to the function even if the body of the function consists of the only single statement.

Right. Now let’s take an example

Now when you execute this program again you will get hello function message on the console.

Now let me tell you how execution work. so when you execute this statement it will first go to the magic statement and call the function. After that, it will print a message on the console and exit from the function. In the end, execution will end the Magic statement and exit from the program.

Now let’s take one more example with return keyword. In the previous lecture, we know that what is the use of Return keyword now we will use this keyword to return value to its caller.

For example. I will print the same message again with return keyword.

So we will just first get rid of these two statements. and say return and in the double quote, I will specify hello function. When you execute this statement you will get hello function on the console.

There are two ways to call the function.

so when you use parentheses just after the function you are calling the function and you tell JavaScript to run the body of that function.  now the one more way to specify your function without any parenthesis. when you did not specify parentheses you are referring to the function object.

just like this. I will just get rid of this statement.

Now when you execute this statement you will get function on the console.  because this statement referring to the function. Now when you have reference to that function you can assign it to any other user define the variable.

Just like this.

now when you execute this statement you will get hello function on the console. Or you can do the same thing with object

Like this,

You get hello function on the console. Right.

Function Arguments

In the previous topic, we learned how to declare the function. Now in this section, you will understand what is an argument and how to pass the argument to the function. Now take attention on this line. The primary mechanism to pass information to a function call is function arguments. You can also call them parameters. Arguments are just like a variable. The argument just used to pass values to the function. Arguments don’t exist until the function is called.

So let’s take an example and understand how arguments work. So we will create a simple function with two arguments.

So when you execute this statement you will get 22.5 average.

Now in this function, a and b is formal arguments you can also call them formal parameter. So when the function is called the formal arguments receive values and become actual arguments.

now let’s understand this process step by step.

So in this example, formal arguments A and B receive values 5 and 40. And become actual arguments. Now you have 5 and 40 value in the function argument a and b. Function arguments exist only in the function. You cannot access the function and arguments outside of the function. so when the function has been called A and B become 5 and 40. and then the function will return average of 5 and 42 it coller.

Now, what if I specify variable when we call the function.

Keep in mind when you create a function the function name must be a valid identifier. Now here we pass two variables a and b when we call the function. Now here A and B distinct variable from the arguments a and b in the function. Even though they are having the same name. When JavaScript call this function javascript won’t pass the variable as a function argument, except javascript will pass a variable value to the function.

It means in the function arguments you have 5 and 40 In the function argument variable.

Default Argument

Now in ECMAScript 6, there is a new concept introduced, Default values for arguments. Normally when you did not specify a default value to the variable it returns undefined.

For example,

Now when you execute this statement you will get undefined variable value.  because we did not specify any argument to the function when we call them. So here we have a new concept default values for arguments.

I will take the same example, and just make some changes.

So as you can see we just specify the default value to the function parameter.  so when you execute this statement you will get default on the console.

And if you just change this default value and specify 3 then you will get 3 on the console. This is a very amazing way to specify the default value to the variable.

That’s it. I hope you understand the function and function arguments. In the next article, we will move forward and learn some more amazing features of function.

Well, read other articles also to understand javascript completely.