1

I have this function:

void myFunct(const int A, ?out? int B){B = B + A;} 

How to declare B in such a way that the function can update its value and return it to the caller?

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
zeus
  • 12,173
  • 9
  • 63
  • 184
  • Assuming standard C++ sematics in Metal, `void myFunct(const int A, int &B) { B = B + A; }` – bg2b Nov 24 '19 at 15:14

1 Answers1

0

Metal is a C++ based programming language that allows you to pass a pointer to a function. To do so, simply declare the function parameter as a pointer type.

void myFunct(const int A, device int *B)
{
   *B = *B + A;
} 

device int *C = 0;
myFunct(1, C);

You can also pass variable by reference:

void myFunct(const int A, device int &B)
{
   B = B + A;
} 

device int *C = 0;
myFunct(1, *C)

or pointer by reference:

void myFunct(const int A, device int *&B)
{
   *B = *B + A;
} 

device int *C = 0;
myFunct(1, C);
Hamid Yusifli
  • 9,688
  • 2
  • 24
  • 48
  • thanks 0xBFE1A8, however what the difference between using &B like @bg2b suggest and using *B ? – zeus Nov 24 '19 at 15:43
  • & indicates a reference. It's basically a pointer under the hood, but you don't have to write *B. Also, you're guaranteed that the reference points to something (it can't be a null pointer). Either method will work though. – bg2b Nov 24 '19 at 16:30
  • @loki in c++, we can pass parameters to a function either by pointers or by reference. In both the cases, we get the same result. [What are the reasons we use one over the other?](https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in) – Hamid Yusifli Nov 24 '19 at 18:02