# Error using function in AWK code

**URL:** <https://community.unix.com/t/error-using-function-in-awk-code/297928>\
**Category:** Shell Programming and Scripting\
**Created:** [October 25, 2011, 12:30am UTC](https://community.unix.com/t/error-using-function-in-awk-code/297928 "2011-10-25T00:30:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ophiuchus](https://community.unix.com/letter_avatar/ophiuchus/32/6_449c533f586939b6e5f246632a46421f.png) [@Ophiuchus](https://community.unix.com/u/Ophiuchus)\
**Post date:** [October 25, 2011, 12:30am UTC](https://community.unix.com/t/error-using-function-in-awk-code/297928/1 "2011-10-25T00:30:45Z")

</div>

Hi to all,

I have an AWK code that looks like this:

```nohighlight
awk 'NR==FNR
        {
        #code to process file1 
         next
        }
        {
        #code to process file2 
        a=myfunction(x)
        }
        END {
            #Some code
             b=myfunction(x)
             
            }'file1 file2

```

As you can see, I need to call at least one time myfunction() when NR !=FNR and inside END{} statement.

I don't know where is the correct place to insert the function within the AWK code. If I put the function code in the part  
when NR==FNR I receive errors and when I put the function in the part when NR !=FNR I receive the same error.

The function is a recursive function and I receive this error.

```nohighlight
awk: cmd. line:4: function recurse(number, ret)
awk: cmd. line:4: ^ syntax error
awk: cmd. line:16: return ret
awk: cmd. line:16: ^ `return' used outside function context

```

Where is the correct place put the function code?

Many thanks in advance

---

<div class="post-metadata">

**Author:** ![rangarasan](https://community.unix.com/user_avatar/community.unix.com/rangarasan/32/2356_2.png) [@rangarasan](https://community.unix.com/u/rangarasan)\
**Post date:** [October 25, 2011, 12:33am UTC](https://community.unix.com/t/error-using-function-in-awk-code/297928/2 "2011-10-25T00:33:32Z")

</div>

Hi,

Try to use function definition in BEGIN block cos your code calls the function before function definition i think.  
END block execute at the end right.

Cheers,  
Ranga:)

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [October 25, 2011, 12:41am UTC](https://community.unix.com/t/error-using-function-in-awk-code/297928/3 "2011-10-25T00:41:04Z")

</div>

Probably best to define function up front:

```nohighlight
awk '
  function recurse(number, ret) {
        # Some code
        return ret
  }
  NR==FNR {
        #code to process file1 
         next
  }
  {
        #code to process file2 
        a=myfunction(x)
  }
  END {
        #Some code
        b=myfunction(x)
 
  }'file1 file2

```

---

<div class="post-metadata">

**Author:** ![Ophiuchus](https://community.unix.com/letter_avatar/ophiuchus/32/6_449c533f586939b6e5f246632a46421f.png) [@Ophiuchus](https://community.unix.com/u/Ophiuchus)\
**Post date:** [October 25, 2011, 1:29am UTC](https://community.unix.com/t/error-using-function-in-awk-code/297928/4 "2011-10-25T01:29:46Z")

</div>

Hi rangarasan and Chubler,

Thanks for your reply.

I've tried both and Chubler's suggestion worked for me.

Many thanks for your help guys.

Regards
