TIL the difference between Parameters and Arguments

An earlier lesson taught me Parameters but, today, when I learned about Arguments I thought, “What’s the diff?” I asked the Google and read many articles & posts before finding this clear & concise explanation:

“The parameters are the aliases for the values that will be passed to the function. The arguments are the actual values.”

Thanks to 0x499602D2 at StackOverflow!

My own explanation is this:

Arguments are pieces of information a function needs to perform a task. Suppose there’s a function called drawMyRectangle and it needs to know the width and height. It would be written like this:

drawMyRectangle (100, 50);

100 and 50 are arguments.

Suppose you’re writing a program that will draw a rectangle based on input from a user. A script asks a user for two numbers–width and height–so it can draw the rectangle. You still need a function to draw the rectangle but you can’t put in numbers–those values aren’t assigned yet–so you put variables in place of those arguments.

A parameter is an empty variable–a variable with no value–you’ll assign it a value later. For example,  We’d create a function looking something like this:

var drawMyRectangle = function(myWidth, myHeight)

myWidth and myHeight are parameters.

Later, when the function is called (after the user provides numbers for width and height), the function is called:

drawMyRectangle(myWidth, myHeight);

Those variables that used to be parameters are now arguments because they’re not empty anymore–they have values assigned to them.

Like a bill wants to become a law, a parameter wants to become an argument. Bills and parameters have no power–they’re just symbols–until they become laws or arguments.

Advertisements

About jotascript

Aiming to please. Seeking to impress.
This entry was posted in JavaScript and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s