Showing posts with label Functions. Show all posts
Showing posts with label Functions. Show all posts

Monday, January 15, 2018

JavaScript: Functions

In JavaScript, functions are one of the main components of writing code. A function can be anything that "encloses a set of statements" (Crockford 26). For new programmers like me, functions can be very confusing to learn, but are essential in order to create complex code. Over time, however, the learning process pays off, allowing you to employ functions and use them to your advantage.

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.
I am still very new to JavaScript, and so far the most confusing thing so far are functions, probably because of my inexperience with style and syntax. In my learning to understand functions, I found that instead of skipping to the big ideas, I should start small. In the case of literal functions, learning what returns are can be very helpful in understanding the larger concept of functions.

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.
Run Button
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.

Line 12 with the return function

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.