Functions with variable-length argument lists
BlitzMax Forums/BlitzMax Programming/Functions with variable-length argument lists
| ||
Is it possible in BMax to create functions/methods with variable-lnegth argument lists. Somthing like this in c++ #include <cstdarg> #include <iostream> using namespace std; double average ( int num, ... ) { va_list arguments; // A place to store the list of arguments double sum = 0; va_start ( arguments, num ); // Initializing arguments to store all values after num for ( int x = 0; x < num; x++ ) // Loop until all numbers are added sum += va_arg ( arguments, double ); // Adds the next value in argument list to sum. va_end ( arguments ); // Cleans up the list return sum / num; // Returns some number (typecast prevents truncation) } int main() { cout<< average ( 3, 12.2, 22.3, 4.5 ) <<endl; cout<< average ( 5, 3.3, 2.2, 1.1, 5.5, 3.3 ) <<endl; } |
| ||
No. Pass in a TList or Array, perhaps. |
| ||
or create a function with multiple default arguments:function test(x:Int = null , y:Int = null , z:Int = null , t:Int = null) if X <> null print X if Y <> null print Y if Z <> null print Z if t <> null print t end function test(1) test(1 , 2) test(1 , 2 , 3) test(1,2,3,4) Not ideal of course |
| ||
Yuck :-) |
| ||
I'd use an array, as Brucey suggested...Strict Print Mean([3!, 12.2!, 22.3!, 4.5!]) Print Mean([5!, 3.3!, 2.2!, 1.1!, 5.5!, 3.3!]) End Function Mean!(num![]) Local sum! For Local x = 0 Until num.Length sum! :+ num[x] Next Return sum! / num.Length End Function |
| ||
thanks guys. Using an Array is perfect for my needs ;) |
| ||
Yuck :-) I never said it was a good way to do it :) |