Since functions have a broad definition, they can be found in nearly any set of code. For example, a simple function literal, which includes four parts. Respectively, the function literal's parts consists of the word function, the functions name, the parameters of the function, and the statements in curly braces (Crockford 27). Since I am just starting to learn how to create functions, I was only able to make a simple one, a function literal that can retrieve someone's information.
An example of a function literal. |
A return is a statement that can be used to cause a function to return early, meaning that it instantly executes the function and ignores the remaining statements. To practice return functions, I made a statement that gives you the sum of two numbers when you press a button, and one with the return function:
Sum statement without the return function |
Sum statement with the return function |
First, I decided to use HTML for JavaScript so I could see the user interface, or the button which says "get sum of c and b". It took me a while to find out how to format the program the way I wanted it to look, but I eventually was able to make it the way I wanted. As for the return function, I included it in one of the programs and left it out in the other to see how the output would differ.
I pressed "RUN" on each of the consoles, and they both gave me some different results, as I had predicted. Alluding to Crockford's description of the return statement, it made sense that one of the programs gave an output through an alert, or a pop-up notification on the top of the screen.
In line 12, the return function was present, and this caused the program to completely ignore the alert, and so when I pressed the button there was no pop-up giving me the sum of C and B, which was 30.
I pressed "RUN" on each of the consoles, and they both gave me some different results, as I had predicted. Alluding to Crockford's description of the return statement, it made sense that one of the programs gave an output through an alert, or a pop-up notification on the top of the screen.
Run Button |
This was definitely a great learning experience for me. I was able to realize that the individual parts are what make up the sum of the larger parts, and gave me a chance to open my eyes to new learning processes!
Question: How do you complete your tasks? Do you agree that the small portions are as significant as the bigger whole?
Works Cited:
Crockford, Douglas. JavaScript: The Good Parts. O'Reilly Media, Inc., 2008.