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.
Questions tagged [function-declaration]
549 questions
26
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
- 75,346
- 28
- 201
- 283
25
votes
2 answers
Should a virtual c++ method implementation in .cpp file be marked virtual?
I have a virtual C++ method that I'm defining in a .h file and implementing in a .cc file. Should the implementation in the .cc file be marked virtual, or just the declaration in the .h file? E.g., my header has:
virtual std::string toString()…

David Lobron
- 1,039
- 2
- 12
- 21
21
votes
7 answers
What does the keyword "callable" do in PHP
To be more exact, the "callable" used in function declaration arguments. like the one below.
function post($pattern, callable $handler) {
$this->routes['post'][$pattern] = $handler;
return $this;
}
How does it benefit us?
why and how do we…

S. Goody
- 360
- 1
- 2
- 10
21
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…

ABu
- 10,423
- 6
- 52
- 103
19
votes
7 answers
Swift - Take Nil as Argument in Generic Function with Optional Argument
I am trying to create a generic function that can take an optional argument.
Here's what I have so far:
func somethingGeneric(input: T?) {
if (input != nil) {
print(input!);
}
}
somethingGeneric("Hello, World!") // Hello,…

Coder-256
- 5,212
- 2
- 23
- 51
18
votes
3 answers
Assigning function to function pointer, const argument correctness?
I am learning the basics of C++ and OOP in my university now. I am not 100% sure how a function pointer works when assigning functions to them. I encountered the following code:
void mystery7(int a, const double b) { cout << "mystery7" << endl;…

edwardlam0328
- 315
- 1
- 7
17
votes
5 answers
How is this possible to use in c++?
To my surprise, I found that the name of a c++ object can be the same as class name. Can someone explain to me the reason why?
When I declare an object of class a as a a1(), it does not raise an error, but doesn't call the constructor. Why is this…

nihar
- 153
- 9
15
votes
2 answers
What does this declaration typedef void foo(); mean?
I don't understand the meaning of typedef void interrupt_handler();. Could someone explain it with some examples?
typedef void interrupt_handler();

Lefteris Sarantaris
- 173
- 1
- 7
14
votes
3 answers
Why does an empty declaration work for definitions with int arguments but not for float arguments?
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…

compiler
- 4,143
- 9
- 36
- 40
14
votes
1 answer
K&R style function definition problem
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;
{
…

nitzs
- 259
- 4
- 9
14
votes
3 answers
letting a java function accept a collection or an array
I am trying to write a function that takes some strings and does something with them.
The only thing I'm going to do that the set of strings is loop over them. Right now I end up with an awkward construct along the lines of
public void…

BostonJohn
- 2,631
- 2
- 26
- 48
13
votes
4 answers
Javascript function declaration with same arguments
I am learning javascript myself. I found if I declare a function with same arguments it just working fine:
function func(a, b, a){
return b;
}
alert(func(1,2,3));
But if I do this :
function func(a, b, a = 5){
return b;
}
alert(func(1,2,3));…

Marymon
- 247
- 2
- 8
12
votes
5 answers
static inline vs inline static
I have noticed that both work, what is the correct way to use inline here?
static inline int getAreaIndex()
OR
inline static int getAreaIndex()
Plus, getAreaIndex contains a large loop. sometimes I call it only one and sometimes I call it through…
user13511156
12
votes
6 answers
How to understand this define
Nowadays , i was reading the APUE.and i found the function defined as below:
void (*signal(int signo, void (*func)(int)))(int);
i was confused, i know signal is pointer to a function and the last (int) is his parameter.
i did not know what is (int…

Sim Sun
- 587
- 1
- 4
- 28
12
votes
2 answers
what does "->" mean in swift when declaring functions?
example function
func example(titles: [String]) `->` [UIButton] {
}
and where could i find more docs on this topic (docs relevant to functions declaring in swift)?

Bob
- 2,263
- 5
- 21
- 30