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.

4 comments:

  1. Hi William!
    As I've been programming for many years, I have had lots of experience with functions and organizing code to make it all flow together. I have found functions to be extremely useful when doing things repetitively, as you can run the function over and over as many times as you want, keeping the code less cluttered. They're also great for other organization, which relates to how you organize your life in order to complete tasks efficiently! Small portions are always important in order to complete the whole.

    ReplyDelete
    Replies
    1. Hi Josh, thank you for commenting on my post! I am still new to programming, and I am still learning and experimenting with code. Throughout the learning process, I've also found that functions can be really useful for organization, which I believe is one of the most important parts for programming. I also believe that the smaller parts of programming are usually the most important, because they are the main factor in determining whether the program works or not.

      Delete
  2. Hey William!
    Just like you, I am new to programming. Although, I did notice a significant difference between JavaScript and the programming language I've been using - Java. From your blog, I have seen a few parallels between Java and JavaScript, namely that functions in JavaScript are very similar to methods in Java. I also empathize your struggle with adapting to the syntax of programming; Sometimes I would take hours to create code that is structurally correct in Java. What I found useful as I struggled, is to look at other example code that works, and experiment with it to see what you can and can't do.

    ReplyDelete
    Replies
    1. Thank you for taking the time to read my post Yafqa! There are many computer languages, and this sparks diversity within the programming community. This can be a great thing, because each programming language has its designated task, but can also be a negative because of how different they can be. Thank you for the great tip on looking at the correct methods! I have started to do so, and the process of figuring out what is correct way to do something has greatly improved my coding knowledge.

      Delete