JavaScript Parameters

Brandon Wilson
12/4/18

From code.org, I have learned a lot about parameters. First of all, what is a parameter? A parameter is an extra piece of information passed to a function to customize it for a specific need. For instance, in code.org, the turtle has a command called moveForward(); which will move the turtle 25 pixels by default. However within the parenthesis you are able to fill parameters to specify the exact amount of pixels you would want the turtle to move. Parameters are a perfect example of an abstraction for repeating the same code multiple times. For example, if I wanted to move the turtle 100 pixels forward, instead of repeating moveForward(); 4 times, I would just use parameters and specify moveForward(100);. Also, I learned you can use parameters in your own functions to edit what happens when the function is called. Say for example I wanted to draw a square. The moveForward command would be used to draw the sides. However, instead of defining a number to how long I want the side to be, I could type size in as a parameter for a function. For example...

function drawSquare(size) {
moveForward(size);
turnLeft();
moveForward(size);
turnLeft();
moveForward(size);
turnLeft();
moveForward(size);
turnLeft();
}

Now as the parameter for this function is size, I would call the function with this parameter. Say I wanted each side of this square to be 50 pixels long, I would call the function like this...

drawSquare(50);

This would then draw a square that has each side 50 pixels long. As you can see, parameters can change the way you call your functions by editing a specific element to the code when creating it. The main thing I learned from parameters would be that they create abstractions within your code which will allow for you to have more control over your program.

Comments

Popular posts from this blog

WBL Workshop #6 Interview Skills

CTE Expo Reflection Journal

Lab #12 Reflection