function
)Define a function block
function
defines a block of code as a function
Define a function:
function name { code-block }
Define a function with variable names defined (default value and description are optional parameters):
function name (
variable1: data-type [default-value] "description",
variable2: data-type [default-value] "description"
) {
code-block
}
Undefine a function:
!function command
» function hw { out "Hello, World!" }
» hw
Hello, World!
» !function hw
» hw
exec "hw": executable file not found in $PATH
Function names can only include any characters apart from dollar ($
). This is to prevent functions from overwriting variables (see the order of preference below).
Like all other definable states in Murex, you can delete a function with the bang prefix !function
(see the example above).
By default, if you wanted to query the parameters passed to a Murex function you would have to use either:
$2
style numbered reserved variables,$PARAM
/ $ARGS
arrays (see reserved-vars document below),args
builtin for any flags.Starting from Murex 2.7.x
it’s been possible to declare parameters from within the function declaration:
function name (
variable1: data-type [default-value] "description",
variable2: data-type [default-value] "description"
) {
code-block
}
First off, the syntax doesn’t have to follow exactly as above:
$
). This is a little like declaring variables via set
, etc. However it should be followed by a colon (:
) or comma (,
). Normal rules apply with regards to allowed characters in variable names: limited to ASCII letters (upper and lower case), numbers, underscore (_
), and hyphen (-
). Unicode characters as variable names are not currently supported.2.8.x
(defaults to str
) but is required in 2.7.x
.[...]
). Any value is allowed (including Unicode) except for carriage returns / new lines (\r
, \n
) and a closing square bracket (]
) as the latter would indicate the end of this field. You cannot escape these characters either.
This field is optional.
"..."
). Any value is allowed (including Unicode) except for carriage returns / new lines (\r
, \n
) and double quotes ("
) as the latter would indicate the end of this field. You cannot escape these characters either.
This field is optional.
variable1: data-type, variable2: data-type
.Any variable name you declare in your function declaration will be exposed in your function body as a local variable. For example:
function hello (name: str) {
out "Hello $name, pleased to meet you."
}
If the function isn’t called with the complete list of parameters and it is running in the foreground (ie not part of autocomplete
, event
, bg
, etc) then you will be prompted for it’s value. That could look something like this:
» function hello (name: str) {
» out "Hello $name, pleased to meet you."
» }
» hello
Please enter a value for 'name': Bob
Hello Bob, pleased to meet you.
(in this example you typed Bob
when prompted)
This is the Murex data type of the variable. From version 2.8.x
this field is optional and will default to str
when omitted.
The advantage of setting this field is that values are type checked and the function will fail early if an incorrect value is presented. For example:
» function age (age: int) { out "$age is a great age." }
» age
Please enter a value for 'age': ten
Error in `age` ( 2,1): cannot convert parameter 1 'ten' to data type 'int'
» age ten
Error in `age` ( 2,1): cannot convert parameter 1 'ten' to data type 'int'
However it will try to automatically convert values if it can:
» age 1.2
1 is a great age.
Default values are only relevant when functions are run interactively. It allows the user to press enter without inputting a value:
» function hello (name: str [John]) { out "Hello $name, pleased to meet you." }
» hello
Please enter a value for 'name' [John]:
Hello John, pleased to meet you.
Here no value was entered so $name
defaulted to John
.
Default values will not auto-populate when the function is run in the background. For example:
» bg {hello}
Error in `hello` ( 2,2): cannot prompt for parameters when a function is run in the background: too few parameters
Descriptions are only relevant when functions are run interactively. It allows you to define a more useful prompt should that function be called without sufficient parameters. For example:
» function hello (name: str "What is your name?") { out "Hello $name" }
» hello
What is your name?: Sally
Hello Sally
There is an order of precedence for which commands are looked up:
runmode
: this is executed before the rest of the script. It is invoked by the pre-compiler forking process and is required to sit at the top of any scripts.test
and pipe
functions also alter the behavior of the compiler and thus are executed ahead of any scripts.private
. Private’s cannot be global and are scoped only to the module or source that defined them. For example, You cannot call a private function directly from the interactive command line (however you can force an indirect call via fexec
).alias
. All aliases are global.function
. All functions are global.global
, set
or let
. Also environmental variables too, declared via export
.You can override this order of precedence via the fexec
and exec
builtins.
function
!function
alias
): Create an alias for a commandexport
): Define an environmental variable and set it’s valueargs
): Command line flag parser for Murex shell scriptingglobal
): Define a global variable and set it’s valuemethod
): Define a methods supported data-typesset
): Define a variable (typically local) and set it’s valueexec
): Runs an executablefexec
): Execute a command or function, bypassing the usual order of precedence.break
): Terminate execution of a block within your processes scopeg
): Glob pattern matching for file system objects (eg *.txt
)source
): Import Murex code from another file or code blockversion
): Get Murex versionprivate
): Define a private function blocklet
: Evaluate a mathematical function and assign to variable (deprecated)This document was generated from builtins/core/structs/function_doc.yaml.
This site's content is rebuilt automatically from murex's source code after each merge to the master
branch. Downloadable murex binaries are also built with the website.
Last built on Wed Sep 18 21:18:57 UTC 2024 against commit c037883c03788357164e9846c84d9f777251495d9452a8e.
Current version is 6.3.4225 (develop) which has been verified against tests cases.