Function pointer as an argument
Definitely. void f(void (*a)()) { a(); } void test() { printf(“hello world\n”); } int main() { f(&test); return 0; }
Definitely. void f(void (*a)()) { a(); } void test() { printf(“hello world\n”); } int main() { f(&test); return 0; }
Semantically, they are identical. However, I’d recommend using the latter syntax (String[] args) when declaring arrays. The former syntax is there mainly for compatibility with C syntax. Since String[], as a whole, is the type of the object in Java, it’s more consistent and clear not to split it up. A similar question addresses the … Read more
Since this answer was somehow accepted and thus will appear at the top, although it’s not the best, I’ve improved it based on the other answers and the comments. The C way; simplest, but will treat any invalid number as 0: #include <cstdlib> int x = atoi(argv[1]); The C way with input checking: #include <cstdlib> … Read more
There is a bug report on this: http://bugs.python.org/issue9338 argparse optionals with nargs=”?”, ‘*’ or ‘+’ can’t be followed by positionals A simple (user) fix is to use — to separate postionals from optionals: ./test.py -o 0.21 0.11 0.33 0.13 — 100 I wrote a patch that reserves some of the arguments for use by the … Read more
Well, arguments are always passed. Default parameter values just ensure that the user doesn’t have to explicitly specify them when calling the function. When the compiler sees a call like this: ExampleMethod(1); It silently converts it to: ExampleMethod(1, “default string”, 10); So it’s not techically possible to determine if the argument was passed at run-time. … Read more
Variadic functions use a calling convention where the caller is responsible for popping the function parameters from the stack, so yes, it is possible to do this dynamically. It’s not standardized in C, and normally would require some assembly to manually push the desired parameters, and invoke the variadic function correctly. The cdecl calling convention … Read more
An automatic way: foo.plot <- function(x,y,…) { lnames <- names(formals(legend)) pnames <- c(names(formals(plot.default)), names(par())) dots <- list(…) do.call(‘plot’, c(list(x = x, y = x), dots[names(dots) %in% pnames])) do.call(‘legend’, c(“bottomleft”, “bar”, pch = 1, dots[names(dots) %in% lnames])) } pch must be filtered from the lnames to avoid duplication in the legend call in case the user … Read more
Sure, just use the arguments object. function foo() { for (var i = 0; i < arguments.length; i++) { console.log(arguments[i]); } }
To pass an array of values to any function that normally expects them to be separate parameters, use Function.prototype.apply, so in this case you need: $.when.apply($, my_array).then( ___ ); See http://jsfiddle.net/YNGcm/21/ In ES6, you can use the … spread operator instead: $.when(…my_array).then( ___ ); In either case, since it’s unlikely that you’ll known in advance … Read more