Author profile picture

Parameters and Arguments: An Easy Way to Remember the Difference

If you’ve ever had trouble remembering the difference between parameters and arguments, today’s article is for you! Understanding this distinction can help when we’re talking about functions, and it can help even more when we’re talking about generics.

Parameters and Arguments - the TL;DR

  • It’s a parameter when you’re inside the definition.
  • It’s an argument when you’re outside the definition.

The easiest way to recall the difference between the two is to associate the word argument with the word outside, by remembering this phrase:

“Take your argument outside!”

Here’s a cartoon that I drew to help you remember it:

Bouncer: 'Take your argument outside!'

For more clarification, let’s take a look at examples for both functions and generics classes.

Parameters and Arguments in Functions

Here’s a simple function that squares an integer. What data will be passed in? Just the number that we want squared.

fun square(number: Int) = number * number

Here, inside the definition of this function, we say that number is a parameter.

Now that we’ve defined our function, let’s use it. We’ll pass some data into our function when we call it.

val area = Math.PI * square(5)

Here, outside the definition of the function, we say that 5 is an argument to the square() function.

Parameters and Arguments in Generics

A generic class is one that has fill-in-the-blank spots for one or more types. For example, here’s a very simple Box class that simply wraps some other object.

class Box<T>(var item: T)

Here, inside the definition of the class, we say that T is a type parameter.

Using this class is easy enough - we just invoke its constructor, passing in the data that we want to wrap.

val box = Box<String>("Hello")

Here, outside of the definition of the Box class, we construct it with a type argument of String.

In fact, Kotlin does some fantastic type inference, so we don’t even have to specify it explicitly:

val box = Box("Hello")

In this case, there’s still a type argument, and it’s still String. It’s simply implied by the type of the "Hello" argument that we’re sending to the constructor.

Summary

So again, in both cases - functions and generic classes/interfaces - the theme is this:

  • Parameter - inside the definition.
  • Argument - outside the definition

It’s easy - just remember to “Take your argument outside!”

Share this article:

  • Share on Twitter
  • Share on Facebook
  • Share on Reddit