Having a sub-function edit a wave passed to it.

Hi,
I apologize for the simple question.

I have a wave in a function that I want to pass to another function to edit, then return. It doesn't seem like you can "return" waves. From the small amount I've read, it seems like you have to pass them by reference, which I've tried (and failed) to do.

Here's some short code that represents what I have.

Function Run()
    String backGroundd = "backGround"
    Wave w_backGround = $backGroundd
        Variable timee = 1427     //I'm paraphrasing here with a lot of stuff, so it may not be 100% correct.
//... Lots of stuff here...
        String workingWave = "WorkingWave" + num2str(timee)
        Make/D $workingWave
        Wave w_wiNm = $workingWave
        CalculateBackground(w_backGround,w_wiNm)
        Print w_backGround   //This print statement prints a Null Wave, which makes sense since w_backGround IS a null wave before it's passed to the CalculateBackground function, but I want it to have the value it picked up in the other function.

End


Function CalculateBackground(w_backGround,w_wiNm) //I know the waves here don't have to have the same name, but it's convenient.
    Wave &w_backGround, w_wiNm
    Display/N=TEMP/W=(350,50,1250,650) w_wiNm
    DoMyInputPanel()  //Pretend that this makes a wave called W_base in a certain data folder, I don't want to include all of the function that this call ends up invoking.
    Duplicate/O root:Packages:SplineFit:W_base,w_backGround
    print w_backGround //This print works just fine, however it does not return the wave to the calling function.
End



What am I doing wrong?
When you pass a numeric or string parameter, unless you explicitly request pass-by-reference using the ampersand, Igor passes a copy of the numeric or string variable to the subroutine. If the subroutine changes it, it is changing a copy and thus the original, in the calling function, is not affected.

However, when you pass a wave as a parameter, Igor actually passes a reference to the wave, not the wave itself or a copy of the wave. Consequently all wave parameters are "pass-by-reference". This means that you do not need to use the ampersand character.

Since the wave is passed by reference, there is no copying of the wave involved. Thus there is only one wave and, if you change it in the subroutine, you are changing the one and only wave.

If that is too abstract, here is an example:
Function Subroutine(w)
    Wave w

    w += 1
End

Function Test()
    Make/O testWave = {1,2,3}
    Subroutine(testWave)
    Print testWave      // Prints {2,3,4}
End


For further study, execute:
DisplayHelpTopic "How Parameters Work"

Thank you. It makes more sense now. That help topic is where I was reading and trying to understand.

Also, you forgot a "wave w = testWave" in the above example. (Alternatively, I believe you could just pass "testWave" to the function and change the final print statement to print testWave instead of w).

Thanks again.
Quote:
You forgot a "wave w = testWave" in the above example.


Thanks. I have fixed it.

(You may notice that I just used testWave with no Wave statement in the main routine. I can do this because Make and Duplicate create automatic wave references when the destination is a simple wave name as opposed to a path or a $ expression.

DisplayHelpTopic "Automatic Creation of WAVE References" for details.)
hrodstein][quote wrote:

(You may notice that I just used testWave with no Wave statement in the main routine. I can do this because Make and Duplicate create automatic wave references when the destination is a simple wave name as opposed to a path or a $ expression.

DisplayHelpTopic "Automatic Creation of WAVE References" for details.)



Thanks. I did know that much, in fact all of my temporary Waves are simply named "tempWave" with no (manual) wave references in the program. (Aka I didn't use a wave statement, like you mentioned above.)

Working with waves is probably one of the more confusing things to deal with in (basic) igor programming. At least, that's what everyone in my lab has trouble with. (Well, they mostly just come ask me!) I think I've finally gotten the basics, however I've only started trying to write more organized procedures (using multiple functions and whatnot), hence my confusion here.

Thank goodness Igor has great documentation (and support.)