Returncode functions

My understanding is: If I leave a function with return $ret the calling script will not only recevive $ret but additionally all output generated by the function.

What is the “gold standard” for always returning only one value (like success/error):

  • Dismiss all output generated by the function before return $ret (how?)
  • A global variable
  • exit $ret instead of return $ret
  • ???
    Many thanks - Michael

I’d say it depends pretty much on what your function is made for. If you want your function to be able to feed the pipeline it should ouptut every single processed element.

For me the gold standard would be to be in control what the function outputs. So make sure there is no output you actually don’t want there to be. :man_shrugging:t4:

Do you have a particular issue you want to solve with this question?

From my experience it is “normal” for a function to do its work and then either return “true” or “false”. If for any reasons this result is completed by (unwanted / uncontrolled) output this does not reflect the behaviour of funktions in any other programming language (as far as I know).

But it is as it is and I have to define my own “gold standard” to handle this behaviour.

Thanks - Michael

Well … again … it depends. If you have a function and you want it to work just like any other PowerShell cmdlet it is not allowed to return true or false. Because that would break the pipeline. Instead it has to return every single element it receives over the pipeline and has processed it in the desired way. :man_shrugging:t4:

My thinking about “functions” is old-fashioned (FORTRAN, C, PASCAL, VBS, Batch) and pipelining ist a different concept .

Michael