Very quick multiple return xC question

Technical questions regarding the XTC tools and programming with XMOS.
teachop
Active Member
Posts: 47
Joined: Wed Feb 05, 2014 1:25 am

Very quick multiple return xC question

Post by teachop »

Pretty sure the answer is "no" but, is there a way to use a multiple return function in a calling parameter list (Lua-like)? In other words is it possible to eliminate the intermediate r,g,b here with some syntax I didn't hit on:

Code: Select all

                {r,g,b} = wheel(( (index*256/LEDS) + outer) & 255);
                dvr.setPixelColorRGB(index, r, g, b);


User avatar
TSC
Experienced Member
Posts: 111
Joined: Sun Mar 06, 2011 11:39 pm

Post by TSC »

I think multiple return values are messy.

Most of the functions I write in XC update arguments by reference.

Code: Select all

void myFunc(unsigned &var1, unsigned &var2){
  var2 ++;
  var1 += var2;
}

// In header file:
#ifdef __XC__
void myFunc(unsigned &var1, unsigned &var2);
#endif
When passing around lots of variables gets too tedious, wrap them up in a struct:

Code: Select all

void otherFunc(){
  s_vars miscVariables;
  miscVariables.var1 = 0;
  miscVariables.var2 = 0;
  myFunc2(miscVariables);
  // etc...
}

void myFunc2(s_vars &miscVariables){
  miscVariables.var2 ++;
  miscVariables.var1 += miscVariables.var2;
}

// In header file:
typedef struct s_vars {
    unsigned var1;
    unsigned var2;
}s_vars;

#ifdef __XC__
void myFunc2(s_vars &miscVariables);
#endif

teachop
Active Member
Posts: 47
Joined: Wed Feb 05, 2014 1:25 am

Post by teachop »

TSC wrote:I think multiple return values are messy.
Yes perhaps it is in xC, I am just learning, coming from decades of C. Have you ever programmed in Lua? Multiple results is a useful feature there.
User avatar
TSC
Experienced Member
Posts: 111
Joined: Sun Mar 06, 2011 11:39 pm

Post by TSC »

No, I've never programmed in Lua. MATLAB also allows multiple return functions and I remember that feature being quite useful.

While I vaguely remember reading that XC supports multiple return functions, I don't think I've ever seen it being used in code written by XMOS staff. Give return by reference a try. It seems to be the predominant way of passing and updating multiple variables between functions.
teachop
Active Member
Posts: 47
Joined: Wed Feb 05, 2014 1:25 am

Post by teachop »

TSC wrote:Give return by reference a try.
Yes thanks, that is good advice. I have made use of references for return results in C++ at times.

I was poking around to make this:

Code: Select all

                {r,g,b} = wheel(( (index*256/LEDS) + outer) & 255);
                dvr.setPixelColorRGB(index, r, g, b);
Into this:

Code: Select all

                dvr.setPixelColorRGB(index, wheel(( (index*256/LEDS) + outer) & 255));
Probably this not possible in xC multiple return. But thought it worth asking. So that is my question - is there a way syntactically to use multiple return in the calling parameters of a function in xC?
User avatar
infiniteimprobability
XCore Legend
Posts: 1126
Joined: Thu May 27, 2010 10:08 am

Post by infiniteimprobability »

teachop
Active Member
Posts: 47
Joined: Wed Feb 05, 2014 1:25 am

Post by teachop »

infiniteimprobability wrote:you're in luck!!
Thanks, I don't see how that answers the question though but I might be dense!

I am using the multiple return feature OK, my question is can I use the function within calling parameters (as in Lua)?

I get an error from attempting to use it in parameters, and was wondering if there is some syntax that I am just not hitting on? Thanks!
User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am

Post by segher »

I don't think you can.

Either way, "function composition" is very bad style
in C (and XC). Good style code is as linear (and boring ;-) )
as possible.
teachop
Active Member
Posts: 47
Joined: Wed Feb 05, 2014 1:25 am

Post by teachop »

segher wrote:function composition" is very bad style
in C (and XC).
Oh. If that term means calling from within the parameter list, it is common in other languages. Lua I am thinking. Functional languages. And Javascript lives there.

Why do you say "very bad"? I am a xC noob!

I would still like to know if it syntactically possible and I am just missing it?
User avatar
ers35
Active Member
Posts: 62
Joined: Mon Jun 10, 2013 2:14 pm

Post by ers35 »

Try the following:

Code: Select all

// xcc multiple-return.xc -target=XS1-L16A-128-QF124-C10 -o multiple-return.xe; xsim multiple-return.xe;

#include <print.h>

struct color
{
  unsigned char r, g, b;
};

struct color
wheel(void)
{
  struct color color = {255, 15, 22};
  return color;
}

void
setPixelColorRGB(int index, struct color color)
{
  printuintln(color.r);
  printuintln(color.g);
  printuintln(color.b);
}

int
main(void)
{
  setPixelColorRGB(1, wheel());
  return 0;
}