What’s the Difference Between a Parameter and an Argument?

I’ve an embarrassing confession.

Till only in the near past, I used to be utilizing parameters and arguments interchangeably when speaking and pairing with different software program builders on my group.

Parameters and arguments will not be the identical issues, however they’re related.

Let’s dig into the variations so you do not have to make the identical embarrassing mistake as me.

Parameters vs Arguments

A parameter is a variable outlined in a operate definition. It’s the variable that the operate expects you to go when it’s referred to as. These parameters outline the variables that we are able to use all through the remainder of the operate as properly.

An argument is the worth that we go to the operate once we name it. Arguments decide the worth of the parameter variables in our operate.

They could nonetheless sound related, so here is an instance with code.

def adder(x, y)
  x + y
Enter fullscreen mode

Exit fullscreen mode

The adder operate takes 2 parameters x and y.

We are able to then name our adder operate and go it 2 arguments.

>>> adder(2, 3)
Enter fullscreen mode

Exit fullscreen mode

Since we handed the arguments 2 and 3 the physique of the operate will run, substituting its parameters with the arguments we equipped.

How Key phrase Arguments Work

Many programming languages, together with Ruby, permit us to call parameters with particular key phrases.

That is helpful for numerous causes.

After we name a operate and go arguments, our operate depends upon the order of our arguments.

For instance, if our operate as an alternative carried out an operation like subtraction or division, the order of our arguments issues considerably.

For instance, if we referred to as the under operate with the arguments divider(0,1) the outcome could be zero, but when we swap the order our operate would increase an exception. (Cannot divide by zero, sadly…)

def divider(x, y)
  x / y.to_f
Enter fullscreen mode

Exit fullscreen mode

With key phrase arguments, we are able to specify which parameter is which extra clearly and once we name the operate and go arguments, the order now not issues for our key phrase arguments.

If we modified our technique to one thing like this:

def divider(numerator:, denominator:)
  numerator / denominator.to_f
Enter fullscreen mode

Exit fullscreen mode

Then to name this technique we now have to incorporate our key phrases:

>>> divider(numerator: 0, denominator: 1)
Enter fullscreen mode

Exit fullscreen mode

We might additionally reverse the order with out elevating an exception since our parameters are named.

>>> divider(denominator: 1, numerator: 0)
Enter fullscreen mode

Exit fullscreen mode


I wager most of your group would not know the distinction between parameters and arguments.

There is a delicate distinction, but it surely’s simple to know when you see a number of examples.

Parameters inform us what the operate will take, whereas arguments are the values we go to a operate once we name it.

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

The order of your parameters and arguments matter. However you should use key phrase arguments to make order pointless and to extra clearly title your arguments while you name your operate.

Add a Comment

Your email address will not be published. Required fields are marked *