How to return a vector from a function

Posted By: jumpman

How to return a vector from a function - 08/14/18 23:24

Is it possible? I want to do some calculations in a function and return a vector. I tried doing renaming the function to VECTOR* super_function() but it gives me a struct error on return.
Posted By: Superku

Re: How to return a vector from a function - 08/15/18 03:39

You could add a vector pointer as an argument/ parameter, (and/) or use a static vector:

Code:
void foo(VECTOR* v, var x, var y, var z)
{
	v->x = x;
	v->y = y*2;
	v->z = z*3;
}

VECTOR* foo(VECTOR* v, var x, var y, var z)
{
	v->x = x;
	v->y = y*2;
	v->z = z*3;

	return v; // now you can use the function as an argument where VECTOR* is expected
}

// same function but accepts NULL as "v" as well
VECTOR* foo(VECTOR* v, var x, var y, var z)
{
	static VECTOR _v;
	if(!v) v = &_v;
	v->x = x;
	v->y = y*2;
	v->z = z*3;

	return v;
}

VECTOR* foo(var x, var y, var z)
{
	static VECTOR _v;
	_v.x = x;
	_v.y = y*2;
	_v.z = z*3;

	return &_v;
}

© 2024 lite-C Forums