View Single Post
Old Aug 16th, 2005, 11:49 AM   #1
Arla
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 206
Rep Power: 4 Arla is on a distinguished road
Recommended Practice for returning data from function

Okay, so I've been thinking this through quite a bit, and have a few questions

I have some functions, that essentially return two disparate sets of data, one set of data is the error messages, which are standard across every function I write, the other is the actual data (these are database functions, so mostly return table data).

What I'm trying to work out is the best way to return this data, now it seems I have about three options.

1. Return error data from function as in (assume ErrorData is some sort of data structure)

public ErrorData Function(int x, int y, int z)

I like this approach at least for the ErrorData because it allows me to assign the return to a variable and immediately reference it, the other option would be to have it as a :out from the function eg

public void Function (int x, int y, int z, out ErrorData errors)

or even Ref (although since in my code errors start at the lowest level of the program and bubble up, I'd never have any need to pass the errors in).

Now same goes for the "return" data, the only issue being that having two sets of returns from a function isn't possible, so then I either have to create another class that contains both the error data and the return data, or I have to return one of them with out or ref parameter.

Alternatively, I can make variables inside the functions class public, and just reference them, however I don't like that as it seems to me it allows for them to change without necessarily meaning it (global variables).

So any thoughts? Currently I'm thinking maybe that passing the errors as the return from the function, and using out in the function for the return data, however a lot of things say "NEVER USE OUT parameters" but never seem to say why they are bad.
Arla is online now   Reply With Quote