On Wed, 12 Mar 2008, James Newton wrote: > Is there a value in having a subroutine called, for example, > "set_var_val_ret" that takes a var, sets it to a value, and returns another > value? > > E.g. rather than writing > > If /* Stuff */ && (got_num_colon = TRUE) && /* more stuff */ { > > you write > > If /* Stuff */ && set_var_val_ret(got_cum_colon, TRUE, TRUE) && /* more > stuff */ { > > and so avoid both the warning/error from the compiler, and more importantly > the subtle point about the conditional continuing to evaluate due to the > value of the assignment being returned. I don't think it would be much of a > performance hit over the simply assignment. A good compiler would probably > optimize it back to that anyway. > > Or set_var_val_stop and set_var_val_go_on. > Hi James, if by that you meen: int set_var_val_ret(int *arg, int val, int res) { *arg = val; return res; } and your example would become: If /* Stuff */ && set_var_val_ret(&got_cum_colon, TRUE, TRUE) && /* more stuff */ { I'm not sure this would be such a good idea because the arg 'arg' needs to be passed by reference which ends up bing a pointer in C. You'd need an exceptional C compiler to deal with the pointer as though it were a dirrect access to the variable. BTW did I mention XCSB actually does the direct access optimisation mentioned above :-) Another thing to consider, when you assign a value to a variable, a C compiler actually does a lot of work making sure that the value is in a form that is compatible with the variable. You would lose that using the above function since you would need to explicitly define a function that can take any variable / value combination to get the full benefit of not going through an intermediate value. e.g. int x; float y; x = y / 2 + 0.5; y = x + 3.5; int set_var_val_ret_int(int *arg, int val, int res) { *arg = val; return res; } int set_var_val_ret_float(float *arg, float val, int res) { *arg = val; return res; } So you end up with lots of different versions of the same function differentiated by a slight mod to the name to indicate what type the var should be. Of course you could also do this more transparently by overloading the same function name. C++ allows such function overloading but unfortunately C does not. Have I mentioned that XCSB also provides function overloading? ;-) Regards Sergio -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist