09 February 2014

Parameters vs. Arguments

“The beginning of wisdom is to call things by their proper names.”

-Confucius

Why do so many software engineers conflate the terms “argument” and “parameter”? The short answer is that 90% of the time, keeping them straight is not particularly important. Since they’re so closely related, it’s often simpler to just lump them together into a single concept. The long answer follows. But first, let’s explain the difference.

Parameters

Parameters exist in method declarations. They are placeholders for what later gets passed into the method. Syntactically, they say “something goes here.”

def plate_food(plate_goes_here, food_goes_here)
  # code to place food on plate
end

In this example, plate_goes_here and food_goes_here are parameters.

Arguments

Arguments, on the other hand, exist in method calls. They are the actual things that get passed to a method.

my_plate = plate.new "porcelain"
some_food = food.new "kimchi"

plate_food(my_plate, some_food)

In this example, my_plate and some_food are arguments.

In programming, methods allow us to store little bits of behavior. We “parameterize” methods in order to allow them to interact with data without us having to specify that data up front. That way, we can use the method over and over again, passing in different pieces of data as we please. Parameters act as placeholders in the method definition for the data that is passed as arguments every time the method is actually invoked.

parameter:hypothetical::argument:concrete

The Confusing Part…

I think this distinction becomes murky because programmers tend to name placeholders (logically) for the things that will eventually replace them.

To return to the previous example, instead of naming our parameters plate_goes_here and food_goes_here, we go with plate and food instead.

def plate_food(plate, food)
  # code to place food on plate
end

A particularly glaring example of this placetaker-for-placeholder conflation happens in Ruby when we name our splat parameters ‘args’ (short for ‘arguments’).

def clean_plates(*args)
  args.each { |plate| plate.wash }
end

In effect, they’ve become the same thing. Or another way of saying that: as programmers, we get so good at flipping back and forth between general and specific that we don’t even notice we’re doing it.