# awk: unable to define user's function



## Seeker (Sep 21, 2013)

I can't define user function in awk.

```
#!/bin/sh

awk '{
print

function foo() {
    print "foo"
}

foo
}' /dev/null
```

I get error:

```
awk: syntax error at source line 4
 context is
         >>> function <<<  foo() {
awk: illegal statement at source line 5
awk: syntax error at source line 9
```

Man pages state:


> Functions may be defined (at the position of  a	pattern-action	state-
> ment) thus:
> 
> function foo(a, b, c) { ...; return x }
> ...



So where is the problem?

```
awk version 20110810 (FreeBSD)
```


----------



## jrm@ (Sep 21, 2013)

Try this.


```
#!/bin/sh

awk '

function foo() { 
   print "foo"
} 

{ 
   foo() 
}

' /dev/random
```


----------



## Seeker (Sep 21, 2013)

Thanks. This works.
The point is, that function* mustn't* be declared inside of a curly brackets.
Duh! I've been pulling my hair out!


----------

