Revision [1401]

This is an old revision of SyntaxFunction made by DavidLee on 2010-05-13 09:22:35.

 

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.



name () { list; }

function name () { list; }

function name { list; }


This declares a function "name". 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



Return Value


The Return value of a function is the return value of the last command, or the argument of the return function.

Example
foo ()
{  
	true ; 
}

foo && echo Is True


Result
Is True


Example

foo ()
{
   return 2 ;
}

foo
echo $?


Result
2









CoreSyntax
Commands





There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki