using waveexists and creating the non-existant wave

Igor doesn't like me.

wave testwave //have to declare it or waveexists wont work
if (waveexists(testwave) == 0 )
...make/o/d/n=1 testwave
endif


I cant seem to do this, as the two wave references are overlapping. Is there a way?

Also just in general, some way to check for the existence of data before running a section of a user defined function. Purpose being that said function will create certain data on the first running, but subsequent passes would then skip this part. The check has to be permanent after the function has finished running, which is why I tried with a wave here..
Does adding /Z to the declaration sort out the problem?
wave/Z testwave //have to declare it or waveexists wont work
if (waveexists(testwave) == 0 )
...make/o/d/n=1 testwave
endif
What about /Z/D in the wave declaration?
wave/Z/D testwave //have to declare it or waveexists wont work
if (waveexists(testwave) == 0 )
    make/o/d/n=1 testwave
endif

If it's not simply a trascription error, the problem may lie in the requirement that parentheses must enclose the argument to the /n flag:

 make /o/d/n=(1) testwave

As Howard points out below, this is not correct. Parentheses are needed if the argument is a variable.

Egregious display of ignorance expurgated.
The wave reference must also have the /d flag. If you are repeating this for text waves, both the reference and the make command must have the /T flag.

Or you could do something like this

wave tempwave = $("testwave")
if (waveexists(tempwave) ==0)
  make/o/d/n=1 testwave
endif


If the wave reference is made with respect to a string name, they don't have to have the same flags. This seems odd to me but it gets around the problem.
Adding /D to the Wave statement makes this work. This is necessary because the Make/D statement automatically creates a wave reference like this:
    Wave/D testwave // Implicitly created by Make statement

which gives you two wave references in the function - one with /D and one without - thus the compile error "Inconsistent type for wave reference".

The automatic wave reference is created only if the argument to Make is a simple name, not if it uses a $ or a path (:testwave).

Here is how I would do it:
Function Test()
    Wave/Z w=testwave       // Reference to testwave in current data folder
    if (waveexists(w) == 0)
        Print "Making testwave"
        Make/O/D/N=1 testwave
    else
        Print "testwave already existed"
    endif
    Wave w = testwave
    Print NameOfWave(w)
End


Quote:
parentheses must enclose the argument to the /n flag

This is true only if the parameter to /N is a variable, not if it is a literal number.