Developer Fundamentals: Parameters

July 11, 2022
Written by

chalkboard

A contextual aside for self taught coders.

What are Parameters?

All of the data that we use in a given program comes from somewhere.

Sometimes it’s really obvious that we’ve assigned a given value to a specific variable or constant within the scope of the code we’re looking at, but other times a program will make use of variables or constants where the values have been assigned somewhere else and passed along to the code we’re looking at.

Parameters refer to variables or constants that a program needs to have passed in from somewhere else rather than being defined or looked up within said code. The values passed into those parameters are generally called arguments.

How are they used?

Let's use the following example program in PHP, which we'll save in a file named "program.php":

$fruit = $argv[1];
$color = $argv[2];
$amount = $argv[3];

function madLibs($fruit, $color, $amount) {
  return "A whole bowl of fruit, and you choose $amount $color $fruit.";
}

echo madLibs($fruit, $color, $amount);

If we execute this program as a command, the values for the parameters it needs will look like a series of values following the file to be executed:

> php program.php apples red 3

Which, in this example, would output "A whole bowl of fruit, and you choose 3 red apples."  This is useful, but you might notice that the assignment of value to parameters by $argv is entirely based on the order the values are given. If instead you executed:

> php program.php red 3 apples

Your output would now read "A whole bowl of fruit, and you choose apples 3 red."

If you wanted to be able to provide more context for whatever human might use program.php in future, you might instead redesign program.php to use flagged parameters:

$fruit = getopt("fruit");
$color = getopt("color");
$amount = getopt("amount");

function madLibs($fruit, $color, $amount) {
  return "A whole bowl of fruit, and you choose $amount $color $fruit.";
}

echo madLibs($fruit, $color, $amount);

Which you would then need to call using the named options it's looking for:

> php program.php –fruit=apples –color=red –amount=3

As you can observe in both of the above program.php examples, when programmatically calling a function that has parameters, you follow the function name with parenthesis containing a comma-separated list containing the values (or variables containing those values) for the parameters in the order they’re declared:

echo madLibs("apples","red",3);

When creating a function or command, you specify any required or optional parameters to be provided when calling that function.

It’s perfectly possible to declare a function or command which doesn’t use any parameters or which only uses optional parameters, but if a function or command has any required parameters, it will not even attempt to execute if values for those parameters aren’t provided when the function or command is called.

Although these examples have been in PHP, most programming languages will provide similar ways to accept optional and required parameter values through the command line in addition to directly passing them to a function within code.

What can make parameters tricky?

Many large code projects, and even relatively simple coding frameworks, often fulfill parameter requirements through inheritance, where the value is generated or defined somewhere else in the codebase.

While this can make complex operations easy to utilize, it can also make troubleshooting particularly challenging if anything interferes with that inheritance such as a function being called from an unexpected location or an inherited value being overwritten in a way that makes it no longer match what the inheriting function or command expects.

If you’re troubleshooting a function or command that takes in parameters but is giving you unexpected behavior, a great step to figuring out what’s going on is to add print commands for the values of parameters so you can make sure they match your expectations.

Fun bonus fact:

In functional programming, the concept of recursion is where you run the same function repeatedly using the output value from the previous run for the parameter input in subsequent runs:

function countByThreesForever (input) {
  output = input + 3;
  print(output);
  countByThreesForever (output);
}