- Overview
- Transcript
5.2 Writing Your Own Functions
Every page or application we write will more than likely require our own functionality. So you'll need to write your own functions, and you'll learn how in this lesson.
1.Introduction2 lessons, 06:25
1.1Introduction01:51
1.2What You Need04:34
2.How the Web Works2 lessons, 16:03
2.1Networks and DNS09:20
2.2HTTP and the Web Server06:43
3.Creating Documents3 lessons, 27:13
3.1Marking Up a Document09:38
3.2Making Our Document Valid06:22
3.3Semantic and Generic Elements11:13
4.Styling Documents3 lessons, 34:07
4.1Adding Style08:17
4.2CSS Selectors16:15
4.3Modifying Layout09:35
5.Scripting Documents3 lessons, 22:36
5.1Variables and Functions09:06
5.2Writing Your Own Functions05:34
5.3Objects07:56
6.The Document Object Model6 lessons, 42:45
6.1DOM Basics08:58
6.2Finding Elements07:40
6.3Working With `NodeLists`04:30
6.4Manipulating an Element's CSS Classes04:29
6.5Listening for Events08:42
6.6Practicing the Concepts08:26
7.HTTP Requests With JavaScript1 lesson, 09:16
7.1The fetch() API09:16
8.Introduction to Server-Side Development5 lessons, 43:24
8.1Introduction to Server-Side Development10:31
8.2Your First Line of PHP Code06:57
8.3Functions and Variables08:00
8.4Parameters and Decisions08:13
8.5Getting Data From the User09:43
9.Getting Started With Databases3 lessons, 31:01
9.1Create a Users Table12:00
9.2Create a Posts Table10:20
9.3Inserting Data08:41
10.Using PHP to Interact With MySQL4 lessons, 51:59
10.1Reading and Displaying Data12:34
10.2Passing and Validating Data11:58
10.3Updating Data14:52
10.4Joining Data (And Deleting Posts)12:35
11.Conclusion1 lesson, 01:47
11.1Conclusion01:47
5.2 Writing Your Own Functions
In the previous lesson, we very briefly looked at functions and variables. And that they are two very important parts of not just JavaScript, but any programming language. But when it comes to running our own software, well, we're going to be writing our own functions. I mean the built-in functions that are given to us by JavaScript and through the browser are useful. I mean, alert does serve some purpose, however, we want our own functionality. For example, let's say that we have been assigned a project to where we have to write a calculator. Well, the alert function isn't going to really help us there and really nothing that comes from JavaScript or comes from the browser is going to help us. So we need to write our own functionality. Because if you think about it, we have at least four functions that we would need to write. We need to add, subtract, multiply, and divide. Now, we're not going to implement all those but lets write a function for adding two numbers together. So to create a function, kind of like a variable where we started with let. Well, we have a special word called function, and this basically creates a function anytime that we use it. So we have function followed by the name of the function and then a pair of parentheses. And then we want the code that is going to execute whenever we call the function and that is inside of these curly braces. Now, if you're brand new to programming, yeah, this is rather cryptic, this is a C style programming syntax. But of course as with everything, the more that you practice and use it, the more it becomes second nature to you. So right now, we have just defined our own function. And if we wanted to, we could call it just like we called the alert function in the previous lesson. We just need the name of our function and then we followed that up with a pair of parentheses. Now, it's not going to do anything because the function doesn't do anything, there's no code inside of it. And there is no data that is being input into that function. Kinda like the alert function, we had to give it a message in order for it to do its job. Well, we have to do the same thing for the sum function, we need to pass in the two numbers that we want to add together. So whenever we define a function, we can also define the input that is going to be used by that function. So we have two numbers, 1 and 2. And we could create what we call parameters called a and b to represent those numbers. These are essentially variables that represent the numbers that are passed to our sum function. And this gives us the ability to work with those values. So that's inside of our sum function, we can take a + b, and there we go, we've added them together. So if we wanted to, we could call alert here and we could pass in a + b, and whenever we save this on the right hand side, we will see the value of of 3. However, most of the time we want to call a function and we want it to return a value so that we can then work with it. For example, let's say that we want to create a variable called three and we want to assign that variable the result of calling our sum function. Well, that's very easy to do because we can return a value from inside of a function. So in this case, we can return a + b that is going to take a plus b, add them together, and then return that value to whatever we are assigning it to. So if we want it to alert the value of the three variable, we will see three. And of course, that's going to change based upon the values that we pass to our sum function. So if we pass 5 and 2, well, we're going to see 7 there. But let's do this, let's create another variable called seven. And we will call the sum function, we will pass in 5 and 2, that will give us the value of seven so that we can then display that. So the beauty about a function is that we can call it as many times as we want. In fact, that's the purpose of why we would want to write a function so that we could reuse that functionality over and over again. And so if we wanted to write a subtract function, we would do the same thing function subtract, then we would have our parentheses. But let's go ahead and define what we call parameters, the input that our function is going to use. And we will return a- b. And then, if we wanted to use that, let's just call that one, and we will subtract 3 and 2. Let's display the result of that, and that will, of course, be 1. Well, in the next lesson, we are going to talk about more complex things. We've talked about variables, which is just an individual's storage for a value. We've talked about functions which are just a single thing that performs work. Well, an object combines all of that together, and you will see how in the next lesson