You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/SPECIFICATION.html
+2-3Lines changed: 2 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -277,10 +277,9 @@
277
277
278
278
279
279
## 5. Functions
280
+
Functions are defined using the `FUNC` keyword with explicit parameter and return types. The canonical positional-only form is `FUNCR: name(T1: arg1,T2: arg2, ...,TN: argN){block}`,whereeach `Tk` and `R` is `INT`, `FLT`, `STR`, `TNS`, or `FUNC`. Parameters MAY also declare a call-time default value using `Tk: arg=expr`. A parameter without a default is positional; a parameter with a default is keyword-capable. Positional parameters MUST appear before any parameters with defaults. Defining a function binds `name` to a callable body with the specified typed formal parameters. Function names MUST NOT conflict with the names of built-in operators or functions.
280
281
281
-
Functions are defined using the `FUNC` keyword with explicit parameter and return types. The canonical positional-only form is `FUNCname(T1: arg1,T2: arg2, ...,TN: argN):R{block}`,whereeach `Tk` and `R` is `INT`,`FLT`,`STR`,`TNS`,or`FUNC`.ParametersMAYalsodeclareacall-timedefaultvalueusing`Tk: arg = expr`.Aparameterwithoutadefaultispositional;aparameterwithadefaultiskeyword-capable.PositionalparametersMUSTappearbeforeanyparameterswithdefaults.Definingafunctionbinds`name`toacallablebodywiththespecifiedtypedformalparameters.FunctionnamesMUSTNOTconflictwiththenamesofbuilt-inoperatorsorfunctions.
In addition to named functions, the language provides an anonymous function literal form `LAMBDA` which constructs a `FUNC` value without binding it to a function name in the global function table. The canonical form for lambdas is `LAMBDAR: (T1: arg1,T2: arg2, ...,TN: argN){ block }`. Parameter typing, default-value rules, call semantics, and return-type rules are the same as for `FUNC`. Evaluating a `LAMBDA` expression captures (closes over) the current lexical environment, producing a first-class `FUNC` value that can be assigned to variables, stored in tensors, passed as an argument, or returned.
284
283
285
284
A user-defined function is called with the same syntax as a built-in: `callee(expr1,expr2, ...,exprN)`. The callee MAY be any expression that evaluates to `FUNC`, including identifiers, tensor elements, or intermediate expressions. Calls MAY supply zero or more positional arguments (left-to-right) followed by zero or more keyword arguments of the form `param=expr`. Keyword arguments can only appear after all positional arguments. At the call site, every positional argument is bound to the next positional parameter; keyword arguments MUST match the name of a parameter that declared a default value. Duplicate keyword names, supplying too many positional arguments, or providing a keyword for an unknown parameter are runtime errors. If a keyword-capable parameter is omitted from the call, its default expression is evaluated at call time in the function's lexical environment after earlier parameters have been bound. The evaluated default MUST match the parameter's declared type. Arguments are evaluated left-to-right. The function body executes in a new environment (activation record) that closes over the defining environment. If a `RETURN(v)` statement is executed, the function terminates immediately and yields `v`; the returned value MUST match the declared return type. If control reaches the end of the body without `RETURN`, the function returns a default value of the declared return type (0 for `INT`, 0.0 for `FLT`, "" for `STR`). Functions whose return type is `TNS` or `FUNC` MUST execute an explicit `RETURN` of the declared type; reaching the end of the body without returning is a runtime error for `TNS`- or `FUNC`-returning functions.
0 commit comments