Pass command line arguments to awk

I am trying to pass max as a sommand line argument when I call awk.

Made the modification in the BEGIN but it is not working

I'm getting an error as below:

awk: txsrx.awk:82: (FILENAME=jcd.tx FNR=4161) fatal: cannot open file `40' for reading (No such file or directory)

Somehow it is reading 40 and interpreting it as a file.

# 1: Absolute value of a number
function abs(val) {
    return val > 0 ? val : -val
}

# 2: Read file twice, set maximum S-R distance
BEGIN {
#    ARGV[ARGC++] = ARGV[ARGC-1]
    print "0." ARGV[0]
    print "1." ARGV[1]
    print "2." ARGV[2]
    print ""
    ARGV[ARGC++] = ARGV[2]
    ARGV[ARGC-2] = ARGV[ARGC-3]
    max = 40
    print ARGC
    print "0." ARGV[0]
    print "1." ARGV[1]
    print "2." ARGV[2]
    print "3." ARGV[3]
}

# 3: First time round, store source location
FNR == NR {
    />/ && idx[FNR] = ++i
    $2 || val = $1
    next
}

# 4: Second time round, get source location
FNR in idx {
    v = val[idx[FNR]]
}

# 5: Calculate S-R distance
{ !/>/ && dist = abs( $1 - v ) }

# 6: Print only if S-R distance less than max, create third column
# For the print statement can also use { OFS = ""; print $0, $2; OFS = " " }
/>/ || dist < max && NF == 2 { sub(/[ \t]+$/, ""); print }

Hi
Can you show us how you are passing 40 to awk? because it is seems that you are not writing it correctly and awk is considering the argument as a filename

I'm doing like below

awk -f txsrx.awk jcd.tx 40 > temp

Did not delete the arg containing the 40 so it is tying to open it as a file.

Doing it like this seems to work

BEGIN {
#    ARGV[ARGC++] = ARGV[ARGC-1]
    print "0." ARGV[0]
    print "1." ARGV[1]
    print "2." ARGV[2]
    print ""
    max = ARGV[ARGC-1]
    # Delete args so awk does not open them as files
    ARGV[ARGC-1] = ARGV[1]
    print ARGC
    print "0." ARGV[0]
    print "1." ARGV[1]
    print "2." ARGV[2]
    print "3." ARGV[3]
}