File reference number and function Passes

I am trying to write some general functions to open, write and close files for Fprintf.and am having difficulties with how to use FileRef between functions.  The question is, is FileRef local to a function or can it be passed?  For example I have previously opened a file for output to fileRefNum = 21, it exists in the PC directory and cannot be deleted because "file is  open by Igor Pro".  When I type at the command prompt  CloseOutputFile(21)

Function CloseOutputFile(FileRefNum)
    variable FileRefNum
    close FileRefNum
end

and error is produced that "there is no open file with this reference number"

Using close /a from the command promt allows the file deletion.  Is there an Igor function that reports open file reference numbers?

PS incidentally why is my code snippet above shown as mostly bold red text?

 

I believe that the refnum value is uniquely associated with the open file until it is closed. You do not assign a value to refnum, the open operation does that and sets the value of the local refnum variable to match that associated with the open file.

You can pass the local variable containing the value of refnum between functions, then each function can operate on the open file.

perhaps it is clearer as code:

function OpenAndCloseFile()
    variable refnum
    open refnum
    FStatus refnum
    print selectstring(V_flag, "closed", "open")
    if (v_flag)
        CloseFile(refnum)
        FStatus refnum
        print selectstring(V_flag, "closed", "open")
    endif
end

function CloseFile(variable refnum)
    close refnum
    return 0
end