github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/docs/commands/function.md (about) 1 # `function` 2 3 > Define a function block 4 5 ## Description 6 7 `function` defines a block of code as a function 8 9 ## Usage 10 11 Define a function: 12 13 ``` 14 function name { code-block } 15 ``` 16 17 Define a function with variable names defined (**default value** and 18 **description** are optional parameters): 19 20 ``` 21 function name ( 22 variable1: data-type [default-value] "description", 23 variable2: data-type [default-value] "description" 24 ) { 25 code-block 26 } 27 ``` 28 29 Undefine a function: 30 31 ``` 32 !function command 33 ``` 34 35 ## Examples 36 37 ``` 38 » function hw { out "Hello, World!" } 39 » hw 40 Hello, World! 41 42 » !function hw 43 » hw 44 exec "hw": executable file not found in $PATH 45 ``` 46 47 ## Detail 48 49 ### Allowed characters 50 51 Function names can only include any characters apart from dollar (`$`). 52 This is to prevent functions from overwriting variables (see the order of 53 preference below). 54 55 ### Undefining a function 56 57 Like all other definable states in Murex, you can delete a function with 58 the bang prefix `!function` (see the example above). 59 60 ### Using parameterized variable names 61 62 By default, if you wanted to query the parameters passed to a Murex function 63 you would have to use either: 64 65 * the Bash syntax where of `$2` style numbered reserved variables, 66 67 * and/or the Murex convention of `$PARAM` / `$ARGS` arrays (see **reserved-vars** 68 document below), 69 70 * and/or the older Murex convention of the `args` builtin for any flags. 71 72 Starting from Murex `2.7.x` it's been possible to declare parameters from 73 within the function declaration: 74 75 ``` 76 function name ( 77 variable1: data-type [default-value] "description", 78 variable2: data-type [default-value] "description" 79 ) { 80 code-block 81 } 82 ``` 83 84 #### Syntax 85 86 First off, the syntax doesn't have to follow exactly as above: 87 88 * **Variables** shouldn't be prefixed with a dollar (`$`). This is a little like 89 declaring variables via `set`, etc. However it should be followed by a colon 90 (`:`) or comma (`,`). Normal rules apply with regards to allowed characters 91 in variable names: limited to ASCII letters (upper and lower case), numbers, 92 underscore (`_`), and hyphen (`-`). Unicode characters as variable names are 93 not currently supported. 94 95 * **data-type** is the Murex data type. This is an optional field in version 96 `2.8.x` (defaults to `str`) but is required in `2.7.x`. 97 98 * The **default value** must be inside square brackets (`[...]`). Any value is 99 allowed (including Unicode) _except_ for carriage returns / new lines (`\r`, 100 `\n`) and a closing square bracket (`]`) as the latter would indicate the end 101 of this field. You cannot escape these characters either. 102 103 This field is optional. 104 105 * The **description** must sit inside double quotes (`"..."`). Any value is allowed 106 (including Unicode) _except_ for carriage returns / new lines (`\r`, `\n`) 107 and double quotes (`"`) as the latter would indicate the end of this field. 108 You cannot escape these characters either. 109 110 This field is optional. 111 112 * You do not need a new line between each parameter, however you do need to 113 separate them with a comma (like with JSON, there should not be a trailing 114 comma at the end of the parameters). Thus the following is valid: 115 `variable1: data-type, variable2: data-type`. 116 117 #### Variables 118 119 Any variable name you declare in your function declaration will be exposed in 120 your function body as a local variable. For example: 121 122 ``` 123 function hello (name: str) { 124 out "Hello $name, pleased to meet you." 125 } 126 ``` 127 128 If the function isn't called with the complete list of parameters and it is 129 running in the foreground (ie not part of `autocomplete`, `event`, `bg`, etc) 130 then you will be prompted for it's value. That could look something like this: 131 132 ``` 133 » function hello (name: str) { 134 » out "Hello $name, pleased to meet you." 135 » } 136 137 » hello 138 Please enter a value for 'name': Bob 139 Hello Bob, pleased to meet you. 140 ``` 141 142 (in this example you typed `Bob` when prompted) 143 144 #### Data-Types 145 146 This is the Murex data type of the variable. From version `2.8.x` this field 147 is optional and will default to `str` when omitted. 148 149 The advantage of setting this field is that values are type checked and the 150 function will fail early if an incorrect value is presented. For example: 151 152 ``` 153 » function age (age: int) { out "$age is a great age." } 154 155 » age 156 Please enter a value for 'age': ten 157 Error in `age` ( 2,1): cannot convert parameter 1 'ten' to data type 'int' 158 159 » age ten 160 Error in `age` ( 2,1): cannot convert parameter 1 'ten' to data type 'int' 161 ``` 162 163 However it will try to automatically convert values if it can: 164 165 ``` 166 » age 1.2 167 1 is a great age. 168 ``` 169 170 #### Default values 171 172 Default values are only relevant when functions are run interactively. It 173 allows the user to press enter without inputting a value: 174 175 ``` 176 » function hello (name: str [John]) { out "Hello $name, pleased to meet you." } 177 178 » hello 179 Please enter a value for 'name' [John]: 180 Hello John, pleased to meet you. 181 ``` 182 183 Here no value was entered so `$name` defaulted to `John`. 184 185 Default values will not auto-populate when the function is run in the 186 background. For example: 187 188 ``` 189 » bg {hello} 190 Error in `hello` ( 2,2): cannot prompt for parameters when a function is run in the background: too few parameters 191 ``` 192 193 #### Description 194 195 Descriptions are only relevant when functions are run interactively. It allows 196 you to define a more useful prompt should that function be called without 197 sufficient parameters. For example: 198 199 ``` 200 » function hello (name: str "What is your name?") { out "Hello $name" } 201 202 » hello 203 What is your name?: Sally 204 Hello Sally 205 ``` 206 207 ### Order of precedence 208 209 There is an order of precedence for which commands are looked up: 210 211 1. `runmode`: this is executed before the rest of the script. It is invoked by 212 the pre-compiler forking process and is required to sit at the top of any 213 scripts. 214 215 1. `test` and `pipe` functions also alter the behavior of the compiler and thus 216 are executed ahead of any scripts. 217 218 4. private functions - defined via `private`. Private's cannot be global and 219 are scoped only to the module or source that defined them. For example, You 220 cannot call a private function directly from the interactive command line 221 (however you can force an indirect call via `fexec`). 222 223 2. Aliases - defined via `alias`. All aliases are global. 224 225 3. Murex functions - defined via `function`. All functions are global. 226 227 5. Variables (dollar prefixed) which are declared via `global`, `set` or `let`. 228 Also environmental variables too, declared via `export`. 229 230 6. globbing: however this only applies for commands executed in the interactive 231 shell. 232 233 7. Murex builtins. 234 235 8. External executable files 236 237 You can override this order of precedence via the `fexec` and `exec` builtins. 238 239 ## Synonyms 240 241 * `function` 242 * `!function` 243 244 245 ## See Also 246 247 * [Reserved Variables](../user-guide/reserved-vars.md): 248 Special variables reserved by Murex 249 * [`alias`](../commands/alias.md): 250 Create an alias for a command 251 * [`args` ](../commands/args.md): 252 Command line flag parser for Murex shell scripting 253 * [`break`](../commands/break.md): 254 Terminate execution of a block within your processes scope 255 * [`exec`](../commands/exec.md): 256 Runs an executable 257 * [`export`](../commands/export.md): 258 Define an environmental variable and set it's value 259 * [`fexec` ](../commands/fexec.md): 260 Execute a command or function, bypassing the usual order of precedence. 261 * [`g`](../commands/g.md): 262 Glob pattern matching for file system objects (eg `*.txt`) 263 * [`global`](../commands/global.md): 264 Define a global variable and set it's value 265 * [`let`](../commands/let.md): 266 Evaluate a mathematical function and assign to variable (deprecated) 267 * [`method`](../commands/method.md): 268 Define a methods supported data-types 269 * [`private`](../commands/private.md): 270 Define a private function block 271 * [`set`](../commands/set.md): 272 Define a local variable and set it's value 273 * [`source`](../commands/source.md): 274 Import Murex code from another file of code block 275 * [`version`](../commands/version.md): 276 Get Murex version 277 278 <hr/> 279 280 This document was generated from [builtins/core/structs/function_doc.yaml](https://github.com/lmorg/murex/blob/master/builtins/core/structs/function_doc.yaml).