Function - MrKaszeba19/PapajLang GitHub Wiki
Function is an entity type used in PapajScript. It stores instructions that may be executed when calling it.
History
Pre-built functions existed since the birth of RPN Calculator (the first functions, then-called operands, were +, -, *, /). The first blocks of instructions were introduced on April 26, 2018 along with release of the 0.4.0 version. The breakthrough happened on November 8, 2018, when the Function entity type was introduced along with release of the 0.4.1 version.
The fun{ } syntax remained unchanged until August 2, 2020. Starting from version 0.5.0, the functions will have 4 ways to define.
Syntaxes
fun{ <instructions> }fun { <instructions> }function{ <instructions> }function { <instructions> }
Features
- Function can be called using
call. If it stored as a variable, then it may be called usingfvar vcall,@@fvaror explicitlyfvar. - As of now, there is no limit how many values functions can take from the stack.
- Functions can return 1 or more values, also they can return no values or remove them from the stack.
- The variables declared inside a function are not usable outside its scope.
- The
( <instructions> )technically used to be an entity ofFunctiontype. Since August 2020 it wasExpression, later replaced byLogicalExpression. - Since April 18, 2021 functions may have input parameters. They are enclosed by parentheses and delimited by spaces, e.g.
function ( x y ) { x y * sqrt }
Examples
fun{ 2 2 + }executed puts2 2 +(i.e.4) on the stackfun { 2 ^ ++ } >testis a function stored in a variabletest. Executed gets a value from the stack, squares it and increments. E.g.5 testreturns26and puts it on the stack.function{ 3.785 / } >toGallonsconverts liters to US gallons (it gets a value from the stack, divides by 3.785 and puts the result on the stack)function { -1 * } >invertmakes a positive number negative and vice versa.- Example of a function with parameters
function ( x y ) {
x y * sqrt
}
-> geomAvg
16 8 geomAvg