Alright, are you ready for the “hard” stuff? We’ve talked about where you can stick your data, but what about storing actions, stuff that manipulates that data? We need functions.

We’ve already seen some functions in action, like prompt and alert. These functions are baked into the language. You can just use them. When you pass a value into them, that value is called an argument.

When you are using alert you are calling the alert function. You’re saying hey, function do some shit.

But you can also write your own functions which is where things start to get interesting. If you weren’t interested already. (Push up)

Let’s create a simple function named cockBlock. Write the keyword “function”, then the name of the function, cockBlock, then parentheses, which remain empty for a simple function like this, then a set of braces to define the block that the function will execute when called. Inside there we’ll put the alert, “keep it in your pants.” It’s good to have a function name relate to what it’s actually doing.

Now let’s go back to our html page and refresh. You’ll see that nothing happens. This is because you have to call the function before it will run.

To call a function, just write out the function name, the two parentheses and a semicolon. When you refresh the page you’ll see an alert pop up.

Let’s get a little more advanced. Functions can also contain variables. So let’s create a function called addNumbers. In the function block we’ll add the variables A equals 2 and B equals 10. Then sum is equal to A plus B. We use the return keyword to get the function to output a value when called. We’ll do return sum. Now if we log the function call, we’ll see 12.

But that’s pretty rigid, the only thing we can get out is 12. That’s where arguments come in. Instead of putting the variables in the function block, we’ll put them in the parentheses after the function name, like this: (gesture)

Now the A and B values are going to be passed in as arguments when the function is called. Call the function addNumbers and then in the parentheses of the function call we put the arguments, let’s do 7 and 11. The great thing about functions is they can be used over an over. They’re a great way to make your code more modular.

So that’s it for the intro to programming lesson thanks for trying to pay attention.

Next articleSEO 2017 – The Complete Guide (Part 1)
Hi there, my name is William, an affiliate marketer comes from United States. I’ve been an affiliate marketer since 2013 ,other from learning how to do it right in online marketing, I want to share my own knowledge about online marketing tools, courses too.


Please enter your comment!
Please enter your name here