Understand functions, parameters and arguments once for all

Arnaud Ambroselli
4 min readJan 3, 2018

There are some concepts to understand as soon as possible when people start to learn code in general. One of the most annoying I’ve been through is this :

In lines 1, 3 and 8, are food, food and food all the same ? Well, no. And there appears the biggest misunderstandings of all times.

  • line 1: food is a variable name.
  • line 3: food is a parameter of digest().
  • line 8: food is passed as an argument to the function digest(). It’s the food of line 1, but it has nothing to do with the food of line 3.

Whaaaaat ?
So let’s understand it once for all, and let’s go back to the basics.

First, the function.

A function is some kind of a tool that transform something into something else. For example, the body is a function that transform food into 💩. Or oxygen into energy for muscles.

Before being used, a function must be defined or declared. Like before we eat for real, we create our digestive system which will handle our food.
When we define a function, we must define what will be the potential inputs (like any kind of food) : let's call them parameters.
Then based on those parameters, we define the function itself, what it will achieve (digest the food), and what it would return (💩). It's commonly called the function's statement.
Once our digestive system is created, we can pass it some food, for real : we actually call the function. Parameters passed to the function when we call are called arguments. Got the vocabulary ?

Let’s do maths now.

Remember high school, when you saw this :

f: x → f(x) = 2x+3

x is a parameter, used as a placeholder when the function f is defined.

Then we can use this function for any argument we are allowed to.

  • f(2) = 2 * 2 + 3;
    f(2) = 7;
  • f(3+4) = 2 * (3+4) + 3;
    f(3+4) = 17;
  • f(X) = 2 * X + 3;
  • f(💩) = 2 * 💩 + 3;
  • f(x) = 2 * x + 3;

Question: is the x from the last line the same as the x from the first line ?

Nope. The first x is a parameter defining the function, a placeholder only, whereas the last one is an argument passed to the function.

Question 2: why the first x has always been x, why it always has to be x ? Why not y ? Because I am still lost with this two x’s.

Naming a parameter x is purely conventional. You don’t have to call your parameter x, you can (and you should) do as you wish. I could have done this :

f: 💩 → f(💩) = 2💩+3;

No difference. Because a parameter is nothing, just a placeholder, doesn’t exist for real ; as a placeholder is defined in a text input, it’s some text representing something which will be passed as an argument to the function, when we’ll use/call the function. Therefore you shouldn’t conform yourself to how have been named any parameters of any function, because their naming has no influence whatsoever on the behaviour of a function.

Free yourself by the 💩.

When you read your code, or code from other devs, and you are not sure wether you face a parameter or an argument, because the name of what you look at is not explicit, replace all the names by a beautiful poop or any word of your choice. An expression that you only would use in that code and makes you comfortable to separate parameters from arguments. Even naming a function f is conventional, you could call it ⭐ if you’d like !
: poop → (poop) = 2 * poop + 3;

As using poop or ⭐ isn’t so professional, instead we could do this :
f: parameter → f(parameter) = 2 * parameter + 3;

In JavaScript it would be :

Names of a variable, a function, a parameter or an argument is purely conventional. Conventions are great, but can be confusing sometimes, so go on and rename everything you want by everything you like, as long as it makes your code clearer for you.

Choose your naming.

Still, there is a balance to find between practicalness and confusedness. If a function have several parameters, calling every parameter parameter1, parameter2, etc., is not very explicit and makes the code hard to read. But calling the parameters with the same names as the arguments to come is also confusing for newbies, and may also be for advanced programmers too, no shame.

So if you want to be sure not to be confused at all, give an explicit parameter name to your parameter, such as foodParam, oxygenParam, or something I like better is anyArray, anyNumber, anyFoodYouLike
Or find your own easy, light and practical convention, one thanks to which you’ll clearly understand and not confuse who is a parameter and who is an argument in your code, and thanks to which you’ll help yourself and other devs to debug.

--

--