function-declaration

Inline function prototype vs regular declaration vs prototype

可紊 提交于 2019-11-29 14:55:13
What's the difference between inline function and then main like so: inline double cube(double side) { return side * side * side; } int main( ) { cube(5); } vs just declaring a function regularly like: double cube(double side) { return side * side * side; } int main( ) { cube(5); } vs function prototype? double cube(double); int main( ) { cube(5); } double cube(double side) { return side * side * side; } An inline function can be defined in multiple translation units (cpp file + includes), and is a hint to the compiler to inline the function. It is usually placed in a header which increases

K&R style function definition problem

不想你离开。 提交于 2019-11-29 03:38:43
The following code works: int main() { void foo(int); foo(3); return 0; } void foo(a) int a; { printf("In foo\n"); } but this one does not: int main() { void foo(float); foo(3.24); return 0; } void foo(a) float a; { printf("In foo\n"); } Why does this happen? Actually, kind of an interesting question. This has to do with the evolution of the C language and the way it arranges to be backwards-compatible to the older flavors. In both cases, you have a K&R-era definition for foo() , but a C99 declaration (with prototype) earlier. But in the first case, the default parameter of int actually is the

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

一个人想着一个人 提交于 2019-11-28 19:28:29
问题 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?

Function without return type specified in C

泪湿孤枕 提交于 2019-11-28 10:23:55
I came across this piece of code in C: #include <stdio.h> main( ) { int i = 5; workover(i); printf("%d",i); } workover(i) int i; { i = i*i; return(i); } I want to know how the declaration of the function "workover" is valid? What happens when we don't mention the return type of a function? (can we return anything?).The parameter is also just a variable name, how does this work? If you do not specify a return type or parameter type, C will implicitly declare it as int . This is a "feature" from the earlier versions of C (C89 and C90), but is generally considered bad practice nowadays. Since the

Can a function prototype typedef be used in function definitions?

不问归期 提交于 2019-11-28 05:17:33
I have a series of functions with the same prototype, say int func1(int a, int b) { // ... } int func2(int a, int b) { // ... } // ... Now, I want to simplify their definition and declaration. Of course I could use a macro like that: #define SP_FUNC(name) int name(int a, int b) But I'd like to keep it in C, so I tried to use the storage specifier typedef for this: typedef int SpFunc(int a, int b); This seems to work fine for the declaration: SpFunc func1; // compiles but not for the definition: SpFunc func1 { // ... } which gives me the following error: error: expected '=', ',', ';', 'asm' or

Why can’t I assign values to a variable inside a named function expression with the same name?

为君一笑 提交于 2019-11-27 22:27:27
This is a named function expression with the name test . Inside, I assign 123 to a variable, also named test . Then test is logged. The function prints its body in the console, but not 123 . What is the reason for such behavior? (function test() { test = 123; console.log( test ); }()); Where does my explanation of function execution fail? Start of function execution: test is a local variable that references the function itself Local variable test is reassigned to number 123 console.log(test) shows the number 123 . I believe this piece of the ecma spec explains this behavior. This relates

Function declaration in CoffeeScript

ε祈祈猫儿з 提交于 2019-11-27 18:26:31
I notice that in CoffeeScript, if I define a function using: a = (c) -> c=1 I can only get the function expression : var a; a = function(c) { return c = 1; }; But, personally I often use function declaration ,for example: function a(c) { return c = 1; } I do use the first form, but I'm wondering if there is a way in CoffeeScript generating a function declaration. If there is no such way, I would like to know why CoffeeScript avoid doing this. I don't think JSLint would holler an error for declaration, as long as the function is declared at the top of the scope. CoffeeScript uses function

K&R style function definition problem

让人想犯罪 __ 提交于 2019-11-27 17:41:40
问题 The following code works: int main() { void foo(int); foo(3); return 0; } void foo(a) int a; { printf("In foo\n"); } but this one does not: int main() { void foo(float); foo(3.24); return 0; } void foo(a) float a; { printf("In foo\n"); } Why does this happen? 回答1: Actually, kind of an interesting question. This has to do with the evolution of the C language and the way it arranges to be backwards-compatible to the older flavors. In both cases, you have a K&R-era definition for foo() , but a

Why does an empty declaration work for definitions with int arguments but not for float arguments?

二次信任 提交于 2019-11-27 12:13:44
I thought the difference is that declaration doesn't have parameter types... Why does this work: int fuc(); int fuc(int i) { printf("%d", i); return 0; } but this fails compiling: int fuc(); int fuc(float f) { printf("%f", f); return 0; } with the message: error: conflicting types for ‘fuc’. note: an argument type that has a default promotion can’t match an empty parameter name list declaration A declaration: int f(); ...tells the compiler that some identifier ( f , in this case) names a function, and tells it the return type of the function -- but does not specify the number or type(s) of

Maximum number of parameters in function declaration

谁都会走 提交于 2019-11-27 11:48:44
I know that minimum number of parameters in function definition is zero, but what is the maximum number of parameters in function definition? I am asking the question just for the sake of knowledge and out of curiosity, not that I am going to write a real function. Yes, there are limits imposed by the implementation. Your answer is given in the bold text in the following excerpt from the C++ Standard. 1. C++ Language Annex B - Implementation quantities Because computers are finite, C + + implementations are inevitably limited in the size of the programs they can successfully process. Every