Skip to content

param Statement

The param statement in Filtrera is used to define parameters that can be passed into a program. These parameters allow for dynamic input values, making your programs more flexible and adaptable. This reference guide provides a detailed specification of the param statement, its syntax, and usage.

Syntax

The basic syntax for the param statement is:

param <parameter_name> : <type>

Optionally, a default value can be specified using the equal sign (=):

param <parameter_name> : <type> = <default_value>

If a default value is provided, the type can be inferred from the default value. Explicit type declarations are only necessary if the default value is a subtype of the allowed input type or if type clarity is needed.

Parameters

  • <parameter_name>: The name of the parameter. This should be a valid identifier.
  • <type>: The data type of the parameter. Filtrera supports several types, including number, text, boolean, etc. This can be omitted if a default value is provided.
  • <default_value> (optional): The default value for the parameter. If no value is provided when the program is executed, this default value will be used.

Examples

Basic Parameter

param age: number

This defines a parameter named age of type number.

Parameter with Default Value

param age: number = 30

This defines a parameter named age of type number with a default value of 30.

Parameter with Inferred Type

param age = 30

In this case, age is inferred to be of type number from the default value.

Parameter with Explicit Type and Default Value

param input: text | number = 'default text'

Here, input can be either text or number, with a default value of 'default text'.

Usage

Defining Multiple Parameters

You can define multiple parameters in a program. Each parameter is defined with its own param statement:

param width: number
param height: number
param unit: text = 'cm'

In this example, three parameters are defined: width and height of type number, and unit of type text with a default value of 'cm'.

Accessing Parameters

Once defined, parameters can be accessed and used within the program:

param width: number
param height: number
from width * height

This program calculates the area of a rectangle using the width and height parameters.

Using Default Values

When executing a program, if no value is provided for a parameter with a default value, the default value is used:

param age: number = 30
from age * 2

If no value is provided for age, the program will use 30 as the default value, resulting in an output of 60.

Overriding Default Values

Default values can be overridden by providing a value when the program is executed:

param age: number = 30
from age * 2

If the program is executed with age set to 25, the output will be 50.

Best Practices

  • Descriptive Names: Use descriptive names for parameters to enhance code readability and maintainability.
  • Default Values: Provide default values for parameters whenever possible to ensure the program can run with minimal input.
  • Type Consistency: Ensure the values passed to parameters match the specified type to avoid runtime errors.
  • Type Inference: Utilize type inference by providing default values to simplify parameter definitions unless explicit type is necessary.

Summary

The param statement is a fundamental feature in Filtrera that allows you to define and manage input parameters for your programs. By understanding its syntax and usage, you can create dynamic and flexible programs that are easy to adapt and maintain.