Parameters
When working with Filtrera, you often need to pass input values into your programs. This is where the param
statement comes in handy. Understanding how to use parameters effectively can make your scripts more flexible, reusable, and easier to understand. This article will explain the use of the param
statement, its use cases, and the benefits of passing input values into your programs.
What is the param
Statement?
The param
statement in Filtrera allows you to define parameters that can be passed into your program. This is similar to how you might pass arguments to a function in other programming languages. Parameters enable you to provide dynamic input values to your program, making it adaptable to different situations without changing the code.
Basic Syntax
The basic syntax for defining a parameter in Filtrera is as follows:
param <parameter_name> : <type>
Here, <parameter_name>
is the name of the parameter, and <type>
specifies the type of data the parameter will hold (such as Int
, String
, Float
, etc.). You can also set a default value for the parameter using the equal sign (=
):
param <parameter_name> : <type> = <default_value>
Example
Let’s look at a simple example where we define a parameter to accept an integer input:
param age: number
from age * 2
In this example, age
is a parameter of type number
. The program will take a number input and multiply it by 2.
If you want to set a default value for the age
parameter, you can do it like this:
param age: number = 30
from age * 2
In this case, if no value is provided for age
, the program will use the default value of 30.
Why Use Parameters?
Using parameters in your programs offers several advantages:
- Flexibility: Parameters allow you to change the input values without modifying the program code. This makes your programs more flexible and easier to adapt to different use cases.
- Reusability: Programs with parameters can be reused in different contexts with different inputs. This reduces code duplication and makes maintenance easier.
- Clarity: Parameters make it clear what inputs a program expects. This improves the readability and understandability of your code.
Use Cases for Parameters
Here are some common use cases where parameters are particularly useful:
1. Customizing Outputs
Suppose you have a program that generates a report based on user input. By using parameters, you can customize the report for different users without changing the code.
param userName: textparam salesAmount: number
from $'Report for {userName}: Total Sales = {salesAmount}'
2. Performing Calculations
Parameters are ideal for performing calculations with varying input values. For instance, calculating the area of a rectangle with given width and height:
param width: numberparam height: number
from width * height
3. Filtering Data
You can use parameters to filter data dynamically. For example, filtering a list of numbers based on a threshold value:
param threshold: numberlet numbers = [10, 20, 30, 40, 50]
from numbers where n => n > threshold
Conclusion
The param
statement is a powerful feature in Filtrera that allows you to define input parameters for your programs. By using parameters, you can create flexible, reusable, and clear scripts that are easy to adapt to different scenarios. Whether you’re customizing outputs, performing calculations, or filtering data, parameters can significantly enhance the functionality and usability of your Filtrera programs.
By understanding and utilizing the param
statement, you can write more dynamic and versatile programs that meet a wide range of business needs.