- Home /
Question by
robinking · Nov 16, 2010 at 03:16 PM ·
javascriptfunctionparameters
How do I create a function that accepts EITHER vector3 OR three floats?
I would like to create a function (in JS) that will EITHER accept a vector3 OR three floats (and then make them into a vector3)... so that either of the following will work:
v = Vector3(1,2,3); result = MyFunction( v );
x = 1; y = 2; z = 3; result = MyFunction( x, y, z );
Comment
Best Answer
Answer by Anton Petrov · Nov 16, 2010 at 03:19 PM
use overloading - define two versions of a function:
Vector3 MyFunction( float x, float y, float z ) { // ... }
Vector3 MyFunction( Vector3 vec ) { return MyFunction( vec.x, vec.y, vec.z ); }
nice code reuse calling the first function from the second. +1