Revision [1470]
This is an old revision of SyntaxFunction made by DavidLee on 2010-06-25 08:52:07.
Functions
Functions can be declared in two forms, with or without the keyword "function". These forms behave identically.
In both forms the arguments to the functions are passed as positional parameters ($1 .. $N)
Note: prior to version 1.0.5 "function name ()" and "name ()" behaved differently. The former syntax caused all variable references to be local.
From version 1.0.5 onwards this has been fixed to conform to the behaviour of bash/ksh where variables are all global unless the "local" modifier is used.
name () { list; }
function name () { list; }
function name { list; }
This declares a function "name". The different variations in syntax are equivalent. The body of the function when executed runs in the same context as the parent shell, with the exception of Positional Parameters ($1 ... $N).
This means that variables assigned within the function have a "side effect" of writing into the global environment for the current shell.
Note: starting with version 1.0.5 local variables can be defined.
Example
setit () { A=$1 ; } A="no value" echo $A setit value echo $A
Result
no value value
Local Variables
Variables can be marked local using the "local" keyword. Local variable modifications are scoped to the function (and any function or script it calls) and do not affect the global (or parents) variables of the same name.
setit () { local A=$1 echo in setit A is $A } A=Value1 setit Value2 echo Outside of setit A is $A
Results
in setit A is Value2 Outside of setit A is Value1
Return Value
The Return value of a function is the return value of the last command, or the argument of the return function. Return values can be numeric only (signed integer).
Example
foo () { true ; } foo && echo Is True
Result
Is True
Example
foo () { return 2 ; } foo echo $?
Result
2
Expermental - Function Call Expressions
Starting with version 1.0.7 functions may be called in 'function call syntax' as a value expression.
The previous calling convention (as described above) is "procedural" where function calls behave like commands.
Function Call Expressions look like and behave more like traditional language functions.
The return value can be any value type (including XDM values such as sequences, elements etc). Evaluating a function call evaluates to the return value.
Arguments are passed in Function Call expressions with ( arglist ).
For example, A function that concatenates 2 arguments separated by ":"
function concat() { return "$1:$2" ; } echo concat(hello world)
Result
hello:world
Function call expressions can be assigned to variables
a=concat(hello world) echo $a
Since they are value expressions they can nest and recurse.
a=concat( concat(this that) another)
Result
this:that:anoother
Arguments are expanded exactly like command execution. The output of the function is shared with the output of the calling shell the same as in procedural calling.
Example
function list() { echo $* return $# } a=list(*.c) echo There were $a .c files
Result
foo.c bar.c spam.c There were 3 .c files
CoreSyntax
Commands