As Ofer says, it's better to pass the values to the function than look
them up again - more efficient. "if you already have the values, pass
them on" - can't argue with that!
But it might be useful to think beyond this particular call to the
function, where you already have the values - if the function is likely
to be used again and again in different contexts. If
the input values are only being looked up by the calling code for the
purposes of calling the function, then it _might_ be better to redesign
things so that you can just pass the unique value and let the function
handle all the rest.
It's one of those hard-to-answer questions, where the answer depends on
the precise circumstances. If I write a function that uses seven
arguments it looks wrong/messy, I feel sure that there must be some
better way of doing it - even though I've never come across any
performance/stability problems using functions with lots of arguments;
but then again, I've often ended up deciding that a function with lots
of arguments _is_ the best way of doing it.
Things to consider:
a) Is the function so wide-rangingly useful (to take a simple example,
a function which looks up an organisation ID and returns the
organisation's name) that it's likely to be called many times? If yes,
having to code the calling procedures to run about and collect all the
values each time will turn out to be a right pain.
b) Does the function encapsulate some important business logic? For
example, an IsEmployeeEligibleForFamilyTaxRelief() function - which
might depend on 27 different factors (age, gender, marital status,
income, number of dependents, employment status). This type of
function, IMHO, is best designed to take just one key value as input,
and look up the rest.
c) Are the values particularly difficult to look up or calculate? Do
you get a noticeable performance hit looking them up twice?
I've just realised that what I say in (b) contradicts the principle of
three-tier architecture, where the business logic and data access are
to be kept separate - this is a murky area, and I'm sure that lots of
people (if they care to) could point out other circumstances where what
I say doesn't apply.
One thing I've found useful to avoid having a whole lot of separate
variables floating about is to declare a user-defined type, and pass
that around code from function to function. Or use a class, for more
complex work.
cheers
SEb