How to use pass-by-reference in XC

  • version

    1.1.1

  • scope

    Example.

    This code is provided as example code for a user to base their code on.

  • description

    How to use pass-by-reference in XC

  • boards

    Unless otherwise specified, this example runs on the SliceKIT Core Board, but can easily be run on any XMOS device by using a different XN file.

In XC you can pass function parameters “by reference”. Any changes to pass-by-reference parameters will also change the original variable passed in by the calling function.

For example, the following function takes a reference parameter x and adds 10 to it.

void add10(int &x)
{
  x += 10;
}

This function can then be called and the argument will be updated

int y=5;

add10(y);

printintln(y);  // This will print out 15