Questions tagged [function-declaration]

A function declaration is the process of describing only the return type, argument types, and name of a function. This is required in some programming languages to use functions that were defined at a later point in the code or in another file. Use this tag for questions that pertain to or problems caused by forward declaring functions in languages that support or require doing so.

549 questions
8
votes
1 answer

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

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() { …
8
votes
2 answers

Local function declaration inside namespace

In such a situation namespace n { void f() { void another_function(); } } Should the function another_function be defined inside the namespace n or outside? VS 2012 (with the November CTP) says it should be outside, and GCC 4.7.2…
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
7
votes
3 answers

Function without return type specified in C

I came across this piece of code in C: #include 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…
Sanjana S
  • 141
  • 1
  • 1
  • 6
7
votes
3 answers

Alternative function syntax difference

What's the difference between these two functions? auto func(int a, int b) -> int; int func(int a, int b);
MBZ
  • 26,084
  • 47
  • 114
  • 191
6
votes
1 answer

Please help me understand this C++ parameter declaration with an argument

I use the ROOT C++ libraries (root.cern.ch) daily and was browsing the source when I came across this function declaration: TString TString::Format(const char *va_(fmt), ...) { //etc. It can be found here. I don't understand how a const char *…
Simon
  • 961
  • 2
  • 9
  • 14
6
votes
4 answers

C++ declares a function instead of calling a complex constructor

First of, I know there are similar questions already on stackoverflow (this, this and this one) and that is why I understand the why of my problem. Unfortunately, that doesn't help me to solve it. While the above questions are all concerning the…
penelope
  • 8,251
  • 8
  • 45
  • 87
6
votes
2 answers

Why does declaring the same function both with and without parameters not cause compilation errors?

For the code: int hi(int); int hi(); int main() { hi(3); } I don't get any compilation errors (calling hi(); without arguments does get a compilation error). I expected that the compiler would complain that the function has already been…
arye
  • 458
  • 3
  • 15
6
votes
4 answers

Why ++str and str+1 is working and str++ isn't?

I know that here are some explanations about the difference between p++, ++p and p+1 but I couldn't understand it clearly yet, especially when it's not working with that function: void replace(char * str, char c1, char c2){ if (*str == '\0') { …
Joe
  • 109
  • 6
6
votes
1 answer

C++ pointer to function declaration syntax

What is the difference between the two declarations in case of foo's arguments? The syntax in the second one is familiar to me and declares a pointer to function. Are both declarations fully equivalent? void foo(int(int)); void foo(int(*)(int));
lukeg
  • 4,189
  • 3
  • 19
  • 40
6
votes
3 answers

Does this abuse of function declarations invoke undefined behavior?

Consider the following program: int main() { int exit(); ((void(*)())exit)(0); } As you can see, exit is declared with the wrong return type, but is never called with the incorrect function type. Is this program's behavior well-defined?
6
votes
1 answer

Function declaration or function expression

I just ran into a problem when defining a function in a block scope. Consider the following program: try { greet(); function greet() { alert("Merry Christmas!"); } } catch (error) { alert(error); } I expected this program…
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
6
votes
2 answers

Function arguments: upper bound vs parent class as argument?

Consider we have: abstract class FlyingObject; case class Rocket(name: String) extends FlyingObject; what is difference between those two function declarations: def launch[T <: FlyingObject](fo: T) and def launch(fo: FlyingObject) Great would be…
PrimosK
  • 13,848
  • 10
  • 60
  • 78
5
votes
4 answers

Why does an invalid use of C function compile fine without any warnings?

I'm more like C++ than C, but this simple example of code is big surprise for me: int foo() { return 3; } int main() { int x; foo(&x); return x; } https://godbolt.org/z/4ov9nTzjM No warnings, no linking issues still this code is…
Marek R
  • 32,568
  • 6
  • 55
  • 140
5
votes
2 answers

Do you have to declare a function's type?

One thing I do not fully understand about Haskell is declaring functions and their types: is it something you have to do or is it just something you should do for clarity? Or are there certain scenarios where you need to do it, just not all?
Jake Jackson
  • 1,055
  • 1
  • 12
  • 34
5
votes
4 answers

Function with 0 arguments - void vs void*?

I know that you can declare a function without any arguments simply like this: void test() { cout << "Hello world!!" << endl; } But I have also seen void test(void) { cout << "Hello world!!" << endl; } and void test(void*) { cout <<…
user2039981