Learn JavaScript 04 (functions)

Hey, How are you? 🙂 I think u r getting well. Ok first look at the below code.

Capture 10

Just type and run. You can’t copy and paste 😀 Type your own code and run. It is the best way to practice 🙂
Ok did you get any idea what we are gonna talk about. Yes we are going to talk about JavaScript FUNCTIONS.

In JavaScript we use function keyword to define a function, followed by a name, followed by parentheses ().

Syntax
function function_name(parameter1, parameter2, parameter3) {
code to be executed
}

Capture 11

Let’s consider about this small program. In here function name is “myFunction” and “a” and “b” are the parameters. This function returns the multiplication of a and b. When JavaScript reaches a return statement, the function will stop executing.
In “myFunction(4,3)”  we call the function and pass value 4 to letter a and pass value 3 to letter b.

Nested Functions

Capture 12
Did you get that ?  The “square” function is in the “myFunction” function. Oh! Function function everywhere 🙂  don’t get confused just think simple. Nested Functions mean function within a function. This is a simple definition for that. We can learn more about future parts.

Function() CONSTRUCTOR

The above way is not the only way to define a new function. We can define function dynamically using Function() constructor along with the new operator.
Now you may think what is constructor.
“A constructor is a special method of a class or structure in object-oriented programming that initializes an object of that type. A constructor is an instance method that usually has the same name as the class, and can be used to set the values of the members of an object, either to default or to user-defined values”

For example, if you have a Car class, you can create new objects of this type by saying new Car() . We can understand about constructors by looking some examples.

Syntax


var variablename = new Function(Arg1, Arg2, “Function Body”);

The Function() constructor can hold any number of string arguments. The last argument is the body of the function. It can contain arbitrary JavaScript statements, separated from each other by semicolons.
Youknow? the Function() constructor is not passed any argument that specifies a name for the function it creates. The unnamed functions created with the Function() constructor are called anonymous functions.

Capture 13
This is a small program which explains what constructor function is. You can see the underlined statement. It is the line which create a “funct” function. “x” and “y” are the parameters and “return x*y;” is the function body. Ok  I think that you got the idea.

Ok boss 🙂 This is the end of Function part. We can get more idea about functions in future parts.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s