Skip to content

Parameters and inout

A parameter is written int $first: a type, then the variable. It can have a default value, int $second = 2. The last parameter can collect all other arguments: int ...$rest.

Parameters

A method lists the values it needs as parameters, and the call says which value goes to which.

Each parameter has a type and a name. A parameter with a default can be left out by the caller. The default can be a value, a class constant, or an enum case. The last parameter can be variadic: it collects the remaining arguments into a list. At the call, you can write an argument with its parameter's name in front of it, as in book(day: "Friday"). Then you can skip the defaults you do not need. Arguments without a name come first.

A parameter written inout changes the caller's own variable when the method returns. The call writes inout in front of that argument too, so you can see the change at the call.

Good to know: parameter names are part of what a method promises, so a caller may rely on them.

The examples below show defaults and a variadic parameter, then named arguments with an enum case as a default, then an inout stock count. The last one leaves out inout at the call, so it does not compile.

Defaults and what is left over

<?nvs
// A parameter can have a default value, so a caller can leave it out.
// The last parameter can collect all the remaining arguments into a list.
class Basket
{
public const int USUAL_PRICE = 5;
// A default can be a value or a class constant. `$each` uses the constant `USUAL_PRICE`.
public static function total(int $items, int $each = Basket::USUAL_PRICE, int ...$surcharges): int
{
var $sum = $items * $each;
foreach ($surcharges as var $surcharge) {
$sum = $sum + $surcharge;
}
return $sum;
}
}
echo 'three at the usual price: ', Basket::total(3), "\n";
echo 'three at four each: ', Basket::total(3, 4), "\n";
echo 'three at four each, plus two surcharges: ', Basket::total(3, 4, 2, 7), "\n";
// `...` passes the values of an array you already have as separate arguments.
var $surcharges = [2, 7];
echo 'the same two, out of a list: ', Basket::total(3, 4, ...$surcharges), "\n";
Output
three at the usual price: 15
three at four each: 12
three at four each, plus two surcharges: 21
the same two, out of a list: 21

Arguments written by name

<?nvs
// You can write an argument with its parameter's name in front of it.
// You can then leave out any parameter that has a default value.
// The call is also easier to read, because each value has its name beside it.
class Slot
{
enum Length { Short = 30, Long = 60 }
// A default can be a case of an enum. `$length` is `Length::Short`, 30 minutes, by default.
public static function book(string $who, string $day = 'Monday', string $time = '09:00', Length $length = Length::Short): string
{
return $who . ' on ' . $day . ' at ' . $time . ' for ' . (($length as int) as string) . ' minutes';
}
}
echo Slot::book('Ada'), "\n";
// Only `length` is given. The other parameters use their default values.
echo Slot::book('Ada', length: Slot::Length::Long), "\n";
// You can write named arguments in any order. Here `time` comes before `day`,
// but the method declares `day` first.
echo Slot::book('Ada', time: '14:00', day: 'Friday'), "\n";
// You can name every argument. An argument without a name must come before the named ones.
echo Slot::book(who: 'Ada', day: 'Friday'), "\n";
// Methods from `Core` accept named arguments too. This prints `007`.
echo Core\Str::padStart('7', length: 3, padding: '0'), "\n";
Output
Ada on Monday at 09:00 for 30 minutes
Ada on Monday at 09:00 for 60 minutes
Ada on Friday at 14:00 for 30 minutes
Ada on Friday at 09:00 for 30 minutes
007

A running total the caller keeps

<?nvs
// Fills orders from one stock count. `take` returns how much it shipped, and it also
// changes the caller's variable `$available`. The method and the call both write `inout`,
// so a reader of the call can see that the variable will change.
class Stock
{
public static function take(inout int $available, int $wanted): int
{
var $given = $wanted < $available ? $wanted : $available;
$available = $available - $given;
return $given;
}
}
var $available = 15;
var $orders = [4, 4, 5];
foreach ($orders as var $order) {
var $shipped = Stock::take(inout $available, $order);
echo 'order for ', $order, ': shipped ', $shipped, ', left ', $available, "\n";
}
// You can also write the parameter's name at the call, as with any other parameter.
echo 'one more: ', Stock::take(inout available: $available, wanted: 2), "\n";
echo 'left: ', $available, "\n";
Output
order for 4: shipped 4, left 11
order for 4: shipped 4, left 7
order for 5: shipped 5, left 2
one more: 2
left: 0

An inout argument without inout

<?nvs
// Shows that the call must write `inout` before an argument to an `inout` parameter.
// This call leaves it out, so this program does not compile.
// proof: compile-error
class Counter
{
public static function bump(inout int $count): void
{
$count = $count + 1;
}
}
int $visits = 0;
Counter::bump($visits);
echo $visits, "\n";
Output
error[E0713]: this argument binds an `inout` parameter and must say so
--> 04-an-inout-argument-without-inout.nvs:15:15
|
15 | Counter::bump($visits);
| ^^^^^^^ write `inout` before it
= help: the function writes a new value back into this argument. `inout` at the call shows that to the reader
error: aborting due to 1 error
To read what a code means and how to fix it, run `nvs agent show <code>`.

Details

A default must be a constant of the parameter's type: a value written in the code, a class constant, an enum case or null. A call such as Core\Math::abs(-1) is not allowed (E0451). Only the last parameter can be variadic, with .... Inside the method, a variadic int ...$rest is an array<int>, and it is empty when the call gives no more arguments.

A call with too few or too many arguments does not compile (E0402). ...$list at the call passes the values of an array as separate arguments.

A named argument must come after every argument without a name. A positional argument after a name: or ... argument does not compile (E0488). A name that no parameter has is E0486, and a parameter given two arguments is E0487.

An inout argument must be a variable or a property of an object, such as $box->count. An array element and a static property do not compile (E0439). A property with hooks, a property named by a key and $obj?->count do not compile either. Neither does the result of a call or a value written in the code. The argument's type must be exactly the parameter's type. An int variable at an inout float parameter does not compile (E0440). A call that leaves out inout before the argument does not compile (E0713).

& before a parameter does not compile (E0237). An anonymous function written with fn cannot have an inout parameter (E0493).

A foreach variable can be inout too. The page foreach explains it.

  • Do give a parameter a default value when most calls use the same value.
  • Do use inout when a method must change the caller’s variable, such as a counter.
  • Don’t use inout to return a result. Use the return value. It is easier to read.
  • Don’t write &$x. It does not compile.