Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Function definitions



On Jun 6,  5:23am, Meino Christian Cramer wrote:
>  
>  Is there any difference in defining functions like

In zsh there is no difference whether you use the "function" keyword or
not, but in ksh the variable scoping rules are different for definitions
that use it.  So be careful if you have any intention of porting.

I prefer to use "function" because it avoids problems with function and
alias names clashing.

However:

>  function fnord(){
>  fnord(){

Those two are OK and do what you expect, but you should get used to
putting spaces before the empty parens and before the open-brace, because
even though zsh does not always enforce it, the grammar does say that the
spaces should be there, and it's crucial to this next example:

>  function fnord{

That defines a function named "fnord{" whose body will consist of the next
single command seen (*not* up to a matching close-brace).  If you add a
space ...

  function fnord {

... then *that* does what you meant.

>  fnord{

This is a *call* to the function named "fnord{".  Even with a space ...

  fnord {

... is not a definition, it's a *call* to the "fnord" function with the
argument "{".  You must have either the keyword "function" or the empty
parens to make a definition.  I believe using both is a syntax error in
ksh, although zsh allows it.

> (In 'man zshall' I found nothing appropiate...)

In the COMPLEX COMMANDS section:

       function word ... [ () ] [ term ] { list }
       word ... () [ term ] { list }
       word ... () [ term ] command
              where term is one or more newline or ;.  Define a function
              which is referenced by any one of word.



Messages sorted by: Reverse Date, Date, Thread, Author