function/wave and return

I wonder if there is a recommended procedure for the following case:

function main()
    wave w = sub()
   
    return sum(w)
end


function/wave sub()
    wave/Z WaveMayNotExist
   
    if(!WaveExists(WaveMayNotExist))
        // this may also be another condition that causes the function to not run through
        // what's best to return now? A non-existing wave? Make/N=1 w=NaN; return w?
        return NaN
    endif
   
    Make/N=(numpnts(WaveMayNotExist)) output
    // more code
    return output
end

 Obviously "return NaN" is inconsistent with the function type although it compiles but gives an unexpected result (at least for me):

print main()
  0

 

I would recommend to return

$""

this is an invalid wave reference. The code then bugs out at

Wave w = sub()

which is what you expect or?

In reply to by johnweeks

johnweeks wrote:

Wave w = sub()
if (WaveExists(w))
    ... do something ...
endif

John, yes, main() should contain something like this but I was just puzzled what to return from sub() in case it aborts. 

 

thomas_braun wrote:

I would recommend to return

$""

this is an invalid wave reference. ...

 Thanks Thomas! That works for me and is what I was looking for!

Cheers

Christian

If the wave may not exist, you should use /Z, like this:

Wave/Z w = sub()    // Use /Z here
if (WaveExists(w))
    ... do something ...
endif

Without the /Z, if you are running with debugging enabled and "NVAR SVAR WAVE Checking" turned on, which is recommended during development, Igor will break into the debugger to let you know you are using a NULL wave reference.

With /Z, Igor will not break into the debugger, because /Z tells Igor "I know that this wave reference may be NULL".

For details, execute:

DisplayHelpTopic "Accessing Global Variables And Waves"

and especially the section "Runtime Lookup Failure".