Best Practice: function return value or byref output parameters?

function, return, return-value

Solution

Personally I would not do either with that method name.

I would instead make two methods:

TryFindSpecificRow
FindSpecificRow

This would follow the pattern of Int32.Parse/TryParse, and in C# they could look like this:

public static Boolean TryFindSpecificRow(DataTable table, out Int32 rowNumber)
{
    if (row-can-be-found)
    {
        rowNumber = index-of-row-that-was-found;
        return true;
    }
    else
    {
        rowNumber = 0; // this value will not be used anyway
        return false;
    }
}

public static Int32 FindSpecificRow(DataTable table)
{
    Int32 rowNumber;


    if (TryFindSpecificRow(table, out rowNumber))
        return rowNumber;
    else
        throw new RowNotFoundException(String.Format("Row {0} was not found", rowNumber));
}

Edit: Changed to be more appropriate to the question.

Problem

I have a function called FindSpecificRowValue that takes in a datatable and returns the row number that contains a particular value. If that value isn't found, I want to indicate so to the calling function. Is the best approach to: - Write a function that returns false if not found, true if found, and the found row number as a byref/output parameter, or - Write a function that returns an int and pass back -999 if the row value isn't found, the row number if it is?

Original source