Next  |  Prev  |  Up  |  Top  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search

#### Formal Parameter Substitution9

Formal parameters are block diagrams substituted where they occur in the function definition, which is then evaluated. Any unused parameters are replaced by signal inputs.

For example, given

```  f(x) = x + x;
```
``f(2)'' gives 4, and `f(_)` gives ```_,_ : +`,'' (not ```_ <: _,_:+`'' or ```_ : *(2)`''). Passing in a two-input block diagram, such as ``f(*),'' yields the four-input block diagram ```_,_,_,_ : *, * : +`.'' Writing simply ``f'' compiles to a two-input adder, following the rule for unused parameters.

As another example of function structure depending on argument type, given

```  f(x) = 1, 2 : _, _, x;
```
```process = f;`'' defines the block diagram ```1,2,_`,'' but ```process = f(_);`'' expands to ```1,2 : _,_,_`'' and fails to compile (two outputs into three inputs). Desired structure can often be enforced by adding parentheses, e.g.,
```f(x) = (1, 2 : _, _), x;
```

To ensure that a function expansion has exactly input signals, it is good to start the function definition with a bus of size :

```  f(x) = _ : (1, 2 : _, _), x; // f and f(_) compile but not f(3)
```
or `si.bus(N)` more generally. This way, when the function is used in an unintended manner, compilation is more likely to fail and alert the user.

Next  |  Prev  |  Up  |  Top  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search