function-declaration

JavaScript function declaration

你离开我真会死。 提交于 2019-12-02 16:23:42
Are the JavaScript code snippets given below some sort of function declaration? If not can someone please give an overview of what they are? some_func = function(value) { // some code here } and show:function(value){ // some code here } The first one is simply creating an anonymous function and assigning it to a variable some_func . So using some_func() will call the function. The second one should be part of an object notation var obj = { show:function(value){ // some code here } }; So, obj.show() will call the function In both cases, you are creating an anonymous function. But in the first

Why is Taking the Address of a Function That is Declared Only Working?

半世苍凉 提交于 2019-12-02 09:15:39
I've asked a question here about whether taking the address of a function forces the compilation of said function specifically with regard to Substitution-Failure-Is-Not-An-Error. The most direct answer to this can be found here : Informally, an object is odr-used if its address is taken, or a reference is bound to it, and a function is odr-used if a function call to it is made or its address is taken. If an object or a function is odr-used, its definition must exist somewhere in the program; a violation of that is a link-time error. But all the compilers I've tested show this as perfectly

position of virtual keyword in function declaration

白昼怎懂夜的黑 提交于 2019-12-01 23:43:00
问题 Does it make any difference whether I place the virtual keyword in a function declaration before or after the return value type? virtual void DoSomething() = 0; void virtual DoSomething() = 0; Found the void virtual syntax while refactoring some legacy code, and was wondering that it is compiling at all... 回答1: Both the statements are equivalent. But the 1st one is more conventional. Because, generally mandatory fields are kept closest to any syntax (i.e. the function prototype in your

position of virtual keyword in function declaration

◇◆丶佛笑我妖孽 提交于 2019-12-01 22:40:34
Does it make any difference whether I place the virtual keyword in a function declaration before or after the return value type? virtual void DoSomething() = 0; void virtual DoSomething() = 0; Found the void virtual syntax while refactoring some legacy code, and was wondering that it is compiling at all... Both the statements are equivalent. But the 1st one is more conventional. Because, generally mandatory fields are kept closest to any syntax (i.e. the function prototype in your example). virtual is an optional keyword (it's needed for pure virtual though). However return type (here void )

Why is a 'conflicting type' error being thrown when I execute this program?

你离开我真会死。 提交于 2019-12-01 21:33:41
问题 In K&R Chapter 1.9, I've been experimenting with the program provided below. Particularly, what would happen if I removed certain decelerations of functions. So, I removed line #4. int getline(char line[], int maxline And the program complies perfectly and functions properly as far as I'm aware. When I remove line #5. void copy(char to[], char from[]); The program throws the following error: yay.c:37:6: warning: conflicting types for ‘copy’ void copy(char to[], char from[]) yay.c:15:9: note:

Why can't a typedef of a function be used to define a function?

你离开我真会死。 提交于 2019-11-30 18:42:45
From § 8.3.5.11 of ISO/IEC 14882:2011(E): A typedef of function type may be used to declare a function but shall not be used to define a function The standard goes on to give this example: typedef void F(); F fv; // OK: equivalent to void fv(); F fv { } // ill-formed void fv() { } // OK: definition of fv What motivates this rule? It seems to limit the potential expressive usefulness of function typedefs. Though this question is about C++, but since C++ inherits typedef and function pointer from C, so an explanation of the same question in C can be used in here. There's a formal explanation for C

Function declarations precedence/overwriting variable declarations? Hoisting? Why?

孤街浪徒 提交于 2019-11-30 09:44:37
问题 Snippet 1: var a; // undefined variable named 'a' function a(foo) { // a function named 'a' var foo = "Hello World"; console.log(foo); } console.log(a); // output is: [Function: a], but why not undefined? Snippet 2: function a(foo) { // a function named 'a' var foo = "Hello World"; console.log(foo); } var a; // undefined variable named 'a' console.log(a); // output is: [Function: a], but why not undefined? I could have just shown Snippet 1 to ask this question - however I showed both just for

Whyever **not** declare a function to be `constexpr`?

不羁的心 提交于 2019-11-29 22:47:08
Any function that consists of a return statement only could be declared constexpr and thus will allow to be evaluated at compile time if all arguments are constexpr and only constexpr functions are called in its body. Is there any reason not to declare any such function constexpr ? Example: constexpr int sum(int x, int y) { return x + y; } constexpr i = 10; static_assert(sum(i, 13) == 23, "sum correct"); Could anyone provide an example where declaring a function constexpr would do any harm? Some initial thoughts: Even if there should be no good reason for ever declaring a function not

How to provide explicit type declarations for functions when using GHCi?

霸气de小男生 提交于 2019-11-29 22:35:09
How to I define the equivalent of this function (taken from learnyouahaskell ) inside GHCi? import Data.List numUniques :: (Eq a) => [a] -> Int numUniques = length . nub Without the type declaration, GHCi accepts the function definition, but it ends up with an unhelpful type: Prelude Data.List> import Data.List Prelude Data.List> let numUniques' = length . nub Prelude Data.List> :t numUniques' numUniques' :: [()] -> Int The resulting function only accepts a list of units as a parameter. Is there a way provide type declarations in GHCi? Or is there another way to define functions like these

Function declarations precedence/overwriting variable declarations? Hoisting? Why?

爷,独闯天下 提交于 2019-11-29 16:45:21
Snippet 1: var a; // undefined variable named 'a' function a(foo) { // a function named 'a' var foo = "Hello World"; console.log(foo); } console.log(a); // output is: [Function: a], but why not undefined? Snippet 2: function a(foo) { // a function named 'a' var foo = "Hello World"; console.log(foo); } var a; // undefined variable named 'a' console.log(a); // output is: [Function: a], but why not undefined? I could have just shown Snippet 1 to ask this question - however I showed both just for completeness purposes. I have wrote some brief comments in them as well. My question is, in both cases