function-parameter

Difference call function with asterisk parameter and without

核能气质少年 提交于 2019-12-03 16:49:50
问题 I know what the meaning of an asterisk is in a function definition in Python. I often, though, see asterisks for calls to functions with parameters like: def foo(*args, **kwargs): first_func(args, kwargs) second_func(*args, **kwargs) What is the difference between the first and the second function call? 回答1: Let args = [1,2,3] : func(*args) == func(1,2,3) - variables are unpacked out of list (or any other sequence type) as parameters func(args) == func([1,2,3]) - the list is passed Let kwargs

MATLAB: Is there a method to better organize functions for experiments?

╄→гoц情女王★ 提交于 2019-12-03 14:50:41
I will run a set of experiments. The main method evaluated has the following signature: [Model threshold] = detect(... TrainNeg, TrainPos, nf, nT, factors, ... removeEachStage, applyEstEachStage, removeFeatures); where removeEachStage , applyEstEachStage , and removeFeatures are booleans. You can see that if I reverse the order of any of these boolean parameters I may get wrong results. Is there a method in MATLAB that allows better organization in order to minimize this kind of error? Or is there any tool I can use to protect me against these errors? Organization with a struct You could input

Passing arguments to functions with const parameters: is it faster?

冷暖自知 提交于 2019-12-03 11:00:55
问题 Consider, for example: int sum(int a, int b) { return a + b; } vs. int sum(const int a, const int b) { return a + b; } Is the second approach in general faster? Function parameters in C are copied and sent to the function, so that changes inside the function do not affect the original values. My reasoning is that in the second sum above, the compiler knows for sure that a and b are not modified inside the function, so it can just pass the original values without copying them first. That's why

Why parameters stored in registers and not on the stack in x86-64 Assembly?

元气小坏坏 提交于 2019-12-02 07:06:21
问题 In x86-32 assembly, parameters are stored on the stack but in x86-64, parameters stored in registers. What is the reason for this? 回答1: It is (a lot) faster to access CPU registers than to access RAM. Since 64bit CPU have a lot more general purpose registers (has nothing to do with being 64bit, it's just because they are newer/bigger), it makes sense to make use of them. 回答2: Store/reload round trips take instructions and cost ~6 cycles of store-forwarding latency, so modern calling

Setting local variables to a function instead of using globals optimize the function

左心房为你撑大大i 提交于 2019-12-01 19:32:51
In the documentation for the itertools module I found this comment def dotproduct(vec1, vec2): return sum(imap(operator.mul, vec1, vec2)) Note, many of the above recipes can be optimized by replacing global lookups with local variables defined as default values. For example, the dotproduct recipe can be written as: def dotproduct(vec1, vec2, sum=sum, imap=imap, mul=operator.mul): return sum(imap(mul, vec1, vec2)) How is it?. Is there a practical noticeable speed-up (that could balance the inconvenience of the larger function signature)? In which specific conditions the use of local variables

Aren't a[][] and (*a)[] equivalent as function parameters?

ε祈祈猫儿з 提交于 2019-12-01 15:23:40
Function prototype void foo(int n, int a[][]); gives error about incomplete type while void foo(int n, int (*a)[]); compiles. As per the decay rule int a[][] is equivalent to int (*a)[] in this case and therefore int (*a)[] should also give an error about an incomplete type but GCC seems to accept it. Is there anything I am missing? This might be a GCC bug but I didn't find anything related to it. AnT No, they are not equivalent as function parameters. They are not equivalent in exactly the same way as parameter declarations in foo and bar struct S; void foo(struct S* s); // OK void bar(struct

Aren't a[][] and (*a)[] equivalent as function parameters?

我的未来我决定 提交于 2019-12-01 15:12:56
问题 Function prototype void foo(int n, int a[][]); gives error about incomplete type while void foo(int n, int (*a)[]); compiles. As per the decay rule int a[][] is equivalent to int (*a)[] in this case and therefore int (*a)[] should also give an error about an incomplete type but GCC seems to accept it. Is there anything I am missing? This might be a GCC bug but I didn't find anything related to it. 回答1: No, they are not equivalent as function parameters. They are not equivalent in exactly the

C# enums as function parameters?

泄露秘密 提交于 2019-11-30 17:11:37
Can you pass a standard c# enum as a parameter? For example: enum e1 { //... } enum e2 { //... } public void test() { myFunc( e1 ); myFunc( e2 ); } public void myFunc( Enum e ) { // Iterate through all the values in e } By doing this I hope to retrieve all the names within any given enum. What would the Iteration code look like? This! public void Foo(Enum e) { var names = Enum.GetNames(e.GetType()); foreach (var name in names) { // do something! } } EDIT: My bad, you did say iterate . Note: I know I could just do the GetNames() call in my foreach statement, but I prefer to assign that type of

assignment operator within function parameter C++

早过忘川 提交于 2019-11-30 13:58:10
问题 I'm studying data structures (List, Stack, Queue), and this part of code is confusing me. ListNode( const Object& theElement = Object(), ListNode * node = NULL); template<class Object> ListNode<Object>::ListNode( const Object& theElement, ListNode<Object> * node) { element = theElement; next = node; } Why there are assignment operators within function parameters? What does Object() call do? 回答1: Those are not assignment operators. Those are default arguments for the function. A function can

C++ : Meaning of const char*const*

让人想犯罪 __ 提交于 2019-11-30 11:46:10
In one of the C++ programs, I saw a function prototype : int Classifier::command(int argc, const char*const* argv) What does const char*const* argv mean? Is it the same as const char* argv[] ? Does const char** argv also mean the same? No, it's not the same as const char *argv[] . The const prohibits modifications of the dereferenced value at the particular level of dereferencing: **argv = x; // not allowed because of the first const *argv = y; // not allowed because of the second const argv = z; // allowed because no const appears right next to the argv identifier const char*const* argv means