Parameters and Arguments are often confused in the world of software development, it's understandable because the concepts are very closely tied which results in the terms often being used incorrectly. With that said, there is a clear distinction. In this post, we'll understand the difference between Parameters and Arguments by explaining the concept using analogies that we see in everyday life. Let's start off by defining these terminologies and building on these definitions.
Simple Definitions: Parameter vs Argument?
- Function: A piece of code that can be used repeatedly, it returns a result based on the data fed to it.
- Parameter: Defines the information that a function needs to receive in order to run a piece of code.
- Argument: The actual information that we pass into a function when calling it.
[code language="javascript"] function greeting (name) { return console.log('hello ' + name); } greeting('Eugene'); greeting('Michael'); [/code] Output (result of running the above code): [code] hello Eugene hello Michael [/code] In the above code we have a function by the name of greeting - line 1. This function has a 'name' parameter. The function: greeting prints out 'hello ' + value of name. So now the question is, what is the value of name? Well, the name value is set when we call the function. We call the function on line 4 (and 5): greeting('Eugene');
. Notice that when we call the function, we pass in a string ('Eugene'). This string is the argument that we are passing into the greeting function. The output section is the result of calling the function, twice in this case. The first call is on line 4 with ('Eugene') as an argument, the second call is on line 5 with ('Michael') as an argument. Notice that I wrote the function once, but can call it as many times as I want (line 4 and 5 as an example). Questions you may be asking:
- How did we know that we had to pass in a 'name' when calling the function? We know based on how the function is written, based on line 1, we see that this function has a parameter of 'name', this tells us that when we call this function we have to provide a value that will fill in for 'name', the value we provide is the 'argument'. We call the function with the arguments we want on line 4 (and 5).
Summarizing with an everyday analogy: Your Car and the gas you put into it! I don't fully understand what makes a car run, but I do know that when I get in my car and turn the key, it starts and I'm able to drive. We know that a car takes in gas in order to operate (or electricity). With that said, obviously you can't just pour anything into your car and expect it to run. If you pour in milk, or juice, you car will not work. Not only do you have to pour in gas, you have to use a certain type of gas that will work with your vehicle. How does this relate to functions, arguments, and parameters?
- The engineering and mechanical parts that makes a car work can be equated to a function.
- The specifications for the type of fuel your car needs to run properly are you parameters. This is what your car accepts and runs on.
- The actual gas that you pour into your car is the argument, this is what you give to your car that makes it drive.
That's all - Questions? Leave them in the comment section below.