Coding for Kids

Functions: Arguments and Parameters

Arguments and Parameters

These two terms get mixed up all the time because they represent a similar thing. When you call a function, you can send information into the function – that's a parameter. The function can take that information – as an argument – to do something special.

A parameter is passed into a function when you call it, such as in sayHello("Steve"); In this case, "Steve" is a parameter.

An argument is part of the function's declaration. These arguments become local, pre-defined variables for use inside the function.

Let's look at this with a basic function that calculates area.

Example:

This isn't proper code (called pseudocode), but I'm illustrating what happens.

When this function is called, the length argument takes on the value of 2 and the width argument takes on the value of 5. Notice how we didn't need to use let length = 2; or let width = 5;? They're already declared as variables and assigned values because they're arguments.

The next part is just like substituting into an equation in science or math class. The computer replaced the variable names with their values and then does the multiplying we asked it to. Once it has the result (10) it then assigns it to the area variable.

When passing in parameters, they have to be in the correct order to match the parameters. If we called calculateArea(5, 2); instead of calculateArea(2, 5);, the values for length and width would be reversed. In this particular case, it doesn't matter, but most of the time, it's a big deal.

This next pseudo-example shows why.

Intellisense

Mistakes like this can really make a mess of your program. A good IDE will give you hints when you start typing in a function call, reminding you of what the parameter names are for that function, and the order in which they need to be entered.

Coding for Kids
JetBrains WebStorm IDE showing property names while typing in a function name

Two parameters got into an argument. They could no longer function!

—Dr. Wolf