Questions tagged [function-declaration]

the process of describing the type and identifier of a function.

403 questions
224
votes
9 answers

Is it possible to define more than one function per file in MATLAB, and access them from outside that file?

When I was studying for my undergraduate degree in EE, MATLAB required each function to be defined in its own file, even if it was a one-liner. I'm studying for a graduate degree now, and I have to write a project in MATLAB. Is this still a…
Nathan Fellman
  • 108,984
  • 95
  • 246
  • 308
83
votes
3 answers

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

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…
mattbh
  • 4,890
  • 2
  • 24
  • 26
82
votes
4 answers

JavaScript function declaration and evaluation order

Why does the first one of these examples not work, but all the other ones do? // 1 - does not work (function() { setTimeout(someFunction1, 10); var someFunction1 = function() { alert('here1'); }; })(); // 2 (function() { setTimeout(someFunction2,…
We Are All Monica
  • 11,597
  • 7
  • 41
  • 69
80
votes
7 answers

Function declaration in CoffeeScript

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) { …
Grace Shao
  • 4,917
  • 4
  • 26
  • 51
80
votes
11 answers

Why can't I define a function inside another function?

This is not a lambda function question, I know that I can assign a lambda to a variable. What's the point of allowing us to declare, but not define a function inside code? For example: #include int main() { // This is illegal //…
Jonathan Mee
  • 35,107
  • 16
  • 95
  • 241
77
votes
5 answers

Alternative (K&R) C syntax for function declaration versus prototypes

What is useful about this C syntax — using 'K&R' style function declarations? int func (p, p2) void* p; int p2; { return 0; } I was able to write this in Visual Studios 2010beta // yes, the arguments are flipped void f() { void* v…
66
votes
7 answers

Does int main() need a declaration on C++?

When reading about functions in C++, I was taught that functions need declarations to be called. For example: #include int main() { std::cout << "The result is " << sum(1, 2); return 0; } int sum(int x, int y) { return x +…
Vini Brasil
  • 4,828
  • 3
  • 22
  • 31
61
votes
3 answers

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

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…
Lars
  • 2,536
  • 3
  • 19
  • 18
37
votes
2 answers

Can a function prototype typedef be used in function definitions?

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…
bitmask
  • 25,740
  • 12
  • 80
  • 142
31
votes
4 answers

Maximum number of parameters in function declaration

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…
Nawaz
  • 327,095
  • 105
  • 629
  • 812
27
votes
8 answers

JavaScript function declaration

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 }
Anand Shah
  • 13,453
  • 15
  • 65
  • 105
24
votes
3 answers

How does ampersand in the return type of a function declaration work?

In this piece of code, why f() is declared as "double & f(..."? What does it mean and how does it work? I don't even know what to google to find the answer to my question. Please help. double a = 1, b = 2; double & f (double & d) { d = 4; …
Azad Salahli
  • 887
  • 3
  • 12
  • 25
23
votes
3 answers

Function pointer parameter without asterisk

I have seen this definition of a function that receives a function pointer as parameter: double fin_diff(double f(double), double x, double h = 0.01) { return (f(x+h)-f(x)) / h; } I am used to see this definition with an asterisk, i.e.: double…
Freeman
  • 5,246
  • 2
  • 41
  • 46
21
votes
11 answers

Why do functions need to be declared before they are used?

When reading through some answers to this question, I started wondering why the compiler actually does need to know about a function when it first encounters it. Wouldn't it be simple to just add an extra pass when parsing a compilation unit that…
Björn Pollex
  • 70,106
  • 28
  • 177
  • 265
19
votes
1 answer

C++11, `noexcept` specifier, definition versus declaration

If a declared function has a noexcept specificator (noexcept, noexcept(true), noexcept(false), or any other noexcept(expr) which evaluates to true or false), but it's defined in another place, do I need to specify the noexcept specifier in the…
Peregring-lk
  • 9,237
  • 6
  • 37
  • 86
1
2 3
26 27