Introducing Functions

Modern JavaScript Incorporating Functions
6 minutes
Share the link to this page
Copied
  Completed

Transcript

You've made it to the section on functions. This is perhaps the most important section and the most exciting section for learning JavaScript. functions are a beautiful concept in JavaScript got functions, right functions can be seen as one of the strengths of JavaScript. They're really the fundamental modular unit in developing a program in JavaScript. In fact, the process of programming is really the process of creating different functions to manipulate data in order to meet a set of requirements. So functions are very important.

Now first, let me define what a function is. It's simply a container that contains a piece of code that can then be executed whenever you need it. That is why is modular. It sets code aside and you can access it at any time by invoking the function functions provide a lot of advantages to your JavaScript program. So I want to take a look at those advantages first. And then in the next movie, we'll look at actually beginning to create functions and we'll build on those concepts as we move forward.

So first advantages of functions. One is it provides structure to larger programs. Now large programs can be costly and time, they take a long time to build. They take time for any programmer that's coming into it to interpret the language and figure out what is going on. It's a lot easier to introduce mistakes in a large program. And therefore it takes time to correct those mistakes.

Functions provide that structure So that you can divide larger programs into smaller pieces. Those smaller pieces are then much more manageable. It's easier to tell what a function does. Because it's a smaller piece of code, it's much easier to manage that smaller piece of code as well. So a very important advantage of functions is the structure it provides to larger programs. Second, functions reduce repetition.

Remember, we had a movie on dry programming do not repeat yourself. functions are another way to accomplish dry programming. If something is repeatable, if a certain bit of code is repeatable, but not in the form of a loop, it can be placed into a function and then whenever you need to execute that code, you invoke the function or you call the function so very important To reduce in reducing repetition functions also isolate code, pieces of code or sub programs from each other. That is an important concept when you're dealing with large programs. If you're not able to isolate code from one another, it's possible that you could have collisions, you could run into problems where, for example, a variable you're using at some point was actually used somewhere else in the program. And perhaps the way it was defined was global.

And so it's causing problems because now you've redefined it in this part of the program. isolating your pieces of code or your sub programs is another advantage of functions and can help reduce problems. functions in JavaScript allow the assigning of a reference to this Piece of code or this sub program. That's an important point in JavaScript, because when you can assign a reference, you can then pass that around or you can then refer to it you can then call it. So an important advantage of function is being able to give it a name. So that you then can pass it around, call it do different things with it.

All right, fifth, functions can be used to specify the behavior of objects. So when we talk more about objects in the next section, the way you add behaviors to objects is by assigning functions to that objects. They're then called methods. Throughout this course, we have used numerous methods. In the section we just finished on arrays. Arrays have certain methods.

Those are simply functions that are assigned to The array object. Finally, functions are an important step to make towards making your code reusable. Because a function is a piece of code, it's much easier to reuse that code in other programs. In fact, the JavaScript frameworks that become so popular, one of the reasons they become so popular is because they contain a collection of functions, which once you include that framework as a part of your own code, you then have access to those functions to do different things. You can do the same thing with your code, you can make your functions reusable as well. So those are what I perceive as the main advantages of functions.

Now let's take a look at how we define our own functions.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.