What Are The Differences Between Parameters and Arguments?

I have an embarrassing confession.

Until just recently, I was using parameters and arguments interchangeably when talking and pairing with other software developers on my team.

Parameters and arguments are not the same things, but they are similar.

Let's dig into the differences so you don't have to make the same embarrassing mistake as me.


Parameters vs Arguments

A parameter is a variable defined in a function definition. It is the variable that the function expects you to pass when it is called. These parameters define the variables that we can use throughout the rest of the function as well.

An argument is the value that we pass to the function when we call it. Arguments determine the value of the parameter variables in our function.

They might still sound similar, so here's an example with code.

def adder(x, y)
  x + y
end

The adder function takes 2 parameters x and y.

We can then call our adder function and pass it 2 arguments.

>>> adder(2, 3)
5

Since we passed the arguments 2 and 3 the body of the function will run, substituting its parameters with the arguments we supplied.


How Keyword Arguments Work

Many programming languages, including Ruby, allow us to name parameters with specific keywords.

This is useful for a number of reasons.

When we call a function and pass arguments, our function depends on the order of our arguments.

For example, if our function instead performed an operation like subtraction or division, the order of our arguments matters significantly.

For example, if we called the below function with the arguments divider(0,1) the result would be zero, but if we swap the order our function would raise an exception. (Can't divide by zero, unfortunately...)

def divider(x, y)
  x / y.to_f
end

With keyword arguments, we can specify which parameter is which more clearly and when we call the function and pass arguments, the order no longer matters for our keyword arguments.

If we changed our method to something like this:

def divider(numerator:, denominator:)
  numerator / denominator.to_f
end

Then to call this method we have to include our keywords:

>>> divider(numerator: 0, denominator: 1)
0

We could also reverse the order without raising an exception since our parameters are named.

>>> divider(denominator: 1, numerator: 0)
0

Takeaways

I bet most of your team doesn't know the difference between parameters and arguments.

There's a subtle difference, but it's easy to understand once you see a few examples.

Parameters tell us what the function will take, while arguments are the values we pass to a function when we call it.

Parameters are like variable names, arguments tell us what their values are.

The order of your parameters and arguments matter. But you can use keyword arguments to make order unnecessary and to more clearly name your arguments when you call your function.

Featured
Level up faster
Recommended Books
Check out my list.
One on Ones: 101
Leveraging Other People's Experience
Hey, I'm Nicholas Dill.

I help people become better software developers with daily tips, tricks, and advice.

Level up