Saturday, 6 January 2018

An Argument About Arguments

What's the difference between an argument and a parameter?

The difference might seem academic, and to some, these terms may even be interchangeable. Lesson of the day: they're not. I've had to listen to people say "pass in the parameters to this function", and, just gotta say, the usage is incorrect. You do not pass in parameters to a function. You pass in arguments.

Hey, I'm not judging. I've made that same mistake in early days. What the hell, everybody's a noob once, right?

Parameters are the inputs to a function (and subroutine, in the case of QBasic). They form the basic, if optional, structure of the function like so. (example in JavaScript)
function shoutOut(param1, param2)
{
    return param1 + " " + param2;
}


So you could say: shoutOut() accepts two parameters, param1 and param2.

Arguments, however, are the actual values you pass into a function in place of those parameters. In the example below, arg1 has the value "Hello" and arg2 has the value "World". These values are arguments passed into shoutOut().
var arg1 = "Hello";
var arg2 = "World";

shoutOut(arg1, arg2);

function shoutOut(param1, param2)
{
    return param1 + " " + param2;
}

Long story short: The arguments are "Hello" and "World" (or arg1 and arg2). The parameters are param1 and param2.

Layman's example

I know most techs will get it at this point, but let me make it even simpler. Think of parameters as placeholders for arguments.

Think of this as a function
with four parameters.
Let's use an analogy. Let's imagine that a function is a parking lot, and parameters are parking spots within the parking lot. Now we all know that parking spots aren't the same as cars. Parking spots are just lines drawn in a parking lot for cars to occupy.

Now think of this as a function
with five  parameters and only
one argument passed in.
So now, think of parameters as parking spots and arguments as cars!

Conclusion

I know, I know, this is really pedantic, huh? But if you're going to code and you want to be able to communicate ideas clearly to other people who are going to read that code, you need to get your terminology right.

Besides, it's only professional.

function bye (seeya) { return seeya; }
T___T

No comments:

Post a Comment