Reputation: 455
Stupid question, is it a good way to change value of function parameter inside the function or create another variable to hold it, which one is better? eg :
s16 functionA(s16 sum) {
u8 addy = 5;
sum += addy;
return sum;
}
OR
s16 functionA(s16 origin) {
u8 addy = 5;
s16 sum = origin + addy;
return sum;
}
Thanks.
Upvotes: 1
Views: 121
Reputation: 400069
It's fine to write to the parameter, and might even be better performance-wise depending on how clever the compiler is.
On the other hand, your example could also be written as:
s16 functionA(s16 sum)
{
const u8 addy = 5; /* Assuming this is more complex in reality. */
return sum + addy;
}
There's no point in assigning to sum
and then returning it, the two statements can be combined.
Upvotes: 2
Reputation: 14571
Since the second one doesn't improve readability, I would choose the first one.
Moreover, it saves 2 bytes on the stack, which can be good in a embedded environment (but it's very likely that the compiler will optimize it).
Upvotes: 0
Reputation: 27577
OR
s16 functionA(s16 origin) {
u8 addy = 5;
return origin + addy;
}
Upvotes: 0