Why default parameters must always be on the right side of any non-default parameters in all programming languages?

StackOverflow https://stackoverflow.com/questions/22589607

Question

"Note that when using default arguments, any defaults should be on the right side of any non-default arguments; otherwise, things will not work as expected."

The above sentence is quoted from http://www.php.net/manual/en/functions.arguments.php

I remember that all the programming languages I've passed by has the same exact rule for the defaulted parameters, I was wondering, why is that? Is there some sort of a compiler or interpreter rule or restriction which makes this a must? Or can this rule be fixed if let's say I wanna make my own compiler?

Was it helpful?

Solution

The reason for this restriction is that otherwise it would be impossible to determine which arguments you are trying to pass. Let's assume the following function:

function foo($first = 'default', $second = 'default', $third) { ... }

And now we're calling it with foo('custom value', 'another value');. another value is obviously the value for $third, but what parameter does custom value stand for? $first or $second? You can only guess and guessing is a really bad thing for a programming language to do, as it limits predictability for the programmer.

A way to fix that would be named parameter passes. In PHP this could look like foo($first='custom value', 'another value');. Now it's clear which default value you're trying to overwrite. Python supports syntax like that, but PHP does not (yet). There has been some discussion to add this feature, I think, but it hasn't been done so far.

OTHER TIPS

Because basically if you had: function F($a = 0, $b); What should you do if you want to call the function with an empty $a? How will the interpreter know you are filling $b not $a?

What about this: function F($a = 0, $b = 0, $c); And you want to pass $b and $c? F(0, 1)? A and C? B and C?

The reason this is happening is simpler than you think. I will base it on the example on the same page you have provides.

Example #5 Incorrect usage of default function arguments

<?php
function makeyogurt($type = "acidophilus", $flavour)
{
    return "Making a bowl of $type $flavour.\n";
}

echo makeyogurt("raspberry");   // won't work as expected
?>

First, why functions have default parameters? Usually this is to save you some time, so you can provide only non-default values. As arguments are parssed to function left-to-right, going with the example #5 you see that by executing makeyogurt("raspberry") you will get an error. The error is because the first argument (raspberry) you have provided to makeyogurt() function gets assigned to $type, thus making $flavour unassigned and throwing an error.

The only way to make the function work with same definition would be to use it as so:

echo makeyogurt("acidophilus", "raspberry");

While this would work as expected, it would really make our default value of $type in the defition of makeyogurt() function obsolete. There would be no point specifying it there if we have to also pass it every time we call the function.

This, the simple solution (and a rule of thumb) is to have all the parameters with default values on the right, like this:

<?php
function makeyogurt($flavour, $type = "acidophilus") // <-- NOTE THIS
{
    return "Making a bowl of $type $flavour.\n";
}

echo makeyogurt("raspberry");   // will work as expected
?>

Now with this kind of definition, when you call makeyogurt("raspberry") you will get the expected result:

Making a bowl of acidophilus raspberry.

And you will also be able to specify any non-default $type when necessary:

echo makeyogurt("raspberry", "plasma");

This will echo:

Making a bowl of plasma raspberry.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top