v0.4.0
Functions · Lesson 4.5

Default argument

Source
Give a parameter a default value so calls can leave that argument out, and name an argument to skip the defaults before it.
You'll need: Function, Return, Character

Some arguments are nearly always the same. Squaring is the usual power; 0 to 100 is the usual range. A default value lets the common call leave that argument out, while the unusual call can still say exactly what it wants. You write it after the parameter's type: name: type = value.

One optional parameter

exponent has a default, so a call may stop after base:

func Power(base: int, exponent: int = 2) -> int {
    var result = 1;
    for i in 0..exponent {
        result *= base;
    }
    return result;
}

Power(5) means exactly Power(5, 2) — the compiler fills in the missing argument at the call. Power(2, 10) passes its own exponent, and the default is not used.

Positional arguments fill from the left

Several parameters may have defaults:

func Clamp(value: int, low: int = 0, high: int = 100) -> int {

Plain, positional arguments fill the parameters strictly from the left. A call can stop early, but positional arguments alone cannot skip a parameter in the middle:

CallvaluelowhighResult
Clamp(150)1500100100
Clamp(-5)−501000
Clamp(42, 50)425010050
Clamp(150, 0, 255)1500255150

The last row is the price of that rule: to change high positionally, the call must also give low, even when it wants the default there.

Naming an argument to skip ahead

An argument can be labeled with the name of its parameter. A labeled argument may skip the defaulted parameters before it, and they keep their defaults:

PrintLine("Clamp(150, high: 255) {}", Clamp(150, high: 255));

Here value is 150, low keeps its default 0, and high is 255, so the result is 150 — the same as Clamp(150, 0, 255), without restating the default. Labels are checked, never reordered: they come after the positional arguments, in the order the parameters are declared. A label also makes a call easier to read, since high: 255 says what the number is for.

Every parameter optional

A default may be any value of the parameter's type — a character as easily as a number. When every parameter has one, a call may pass nothing at all:

func Rule(width: int = 12, mark: char = '-') {
    for i in 0..width {
        Print(mark);
    }
    PrintLine();
}

Rule() prints twelve dashes, Rule(5) five, and Rule(5, '=') five equals signs.

Defaults come last

A parameter without a default may not follow one with a default. In Mix(first: int = 1, second: int) there would be no way to supply second while leaving first out — the first argument always lands in first — so the declaration itself is refused. Put the parameters callers usually change first, and the ones they rarely touch at the end.

The program

The whole lesson is one package in the Examples repository. Its comments explain every step.

Src/Main.rux
// A parameter can carry a default value, written `name: type = value`. A call
// may then leave that argument out, and the default is used in its place.
//
// Defaults suit the argument that is nearly always the same: squaring is the
// usual power, 0 to 100 the usual range. The common call stays short, and the
// unusual one can still say exactly what it wants.
import Io::{ Print, PrintLine };

// `exponent` is optional. `Power(5)` means `Power(5, 2)`.
func Power(base: int, exponent: int = 2) -> int {
    var result = 1;
    for i in 0..exponent {
        result *= base;
    }
    return result;
}

// Several parameters may have defaults. Positional arguments fill the
// parameters from the left, so a call can stop early; skipping one in the
// middle takes a named argument, shown in `Main`.
func Clamp(value: int, low: int = 0, high: int = 100) -> int {
    if value < low {
        return low;
    }
    if value > high {
        return high;
    }
    return value;
}

// A default may be any value of the parameter's type, a character as easily as
// a number. Every parameter here has one, so a call may pass nothing at all.
func Rule(width: int = 12, mark: char = '-') {
    for i in 0..width {
        Print(mark);
    }
    PrintLine();
}

func Main() -> int {
    PrintLine("Power(5)            {}", Power(5));
    PrintLine("Power(2, 10)        {}", Power(2, 10));

    PrintLine("Clamp(150)          {}", Clamp(150));
    PrintLine("Clamp(-5)           {}", Clamp(-5));
    PrintLine("Clamp(42, 50)       {}", Clamp(42, 50));

    // Positional arguments fill the parameters from the left, so to change
    // `high` this call must also give `low`, even though it wants the default
    // there.
    PrintLine("Clamp(150, 0, 255)  {}", Clamp(150, 0, 255));

    // Labeling an argument with its parameter's name lets the call skip the
    // defaulted parameters before it, which keep their defaults. Labels must
    // still come after the positional arguments, in parameter order.
    PrintLine("Clamp(150, high: 255) {}", Clamp(150, high: 255));

    Rule();
    Rule(5);
    Rule(5, '=');

    // A parameter without a default may not come after one with a default,
    // because there would be no way to reach it while leaving the earlier one
    // out:
    //
    //     func Mix(first: int = 1, second: int) -> int { ... }
    //         error: parameter 'second' without a default value cannot follow
    //                a parameter with a default value
    return 0;
}

Run it

cd Examples/Functions/DefaultArgument
rux run
Power(5)            25
Power(2, 10)        1024
Clamp(150)          100
Clamp(-5)           0
Clamp(42, 50)       50
Clamp(150, 0, 255)  150
Clamp(150, high: 255) 150
------------
-----
=====

Common mistakes

Try it yourself

  1. Write Greet(name: char8[..] = "world") that prints Hello, …!, and call it with and without a name.
  2. Predict what Clamp(150, 255) returns, then run it. Which parameter did 255 land in?
  3. Swap the parameters of Rule so that mark comes first and only width has a default. Which calls in Main still compile?
  4. Call Rule(mark: '*') and predict how wide the line is.
  5. Write the Mix function from this lesson and read the error.

Learn more