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.
function-declaration
487
questions
529
votes
31
answers
396k
views
Does using const on function parameters have any effect? Why does it not affect the function signature?
For example, imagine a simple mutator that takes a single boolean parameter:
void SetValue(const bool b) { my_val_ = b; }
Does that const actually have any impact? Personally I opt to use it ...
286
votes
5
answers
80k
views
Is there a difference between foo(void) and foo() in C++ or C?
Consider these two function definitions:
void foo() { }
void foo(void) { }
Is there any difference between these two? If not, why is the void argument there? Aesthetic reasons?
227
votes
9
answers
118k
views
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 ...
89
votes
5
answers
8k
views
What's the significance of a C function declaration in parentheses apparently forever calling itself?
In gatomic.c of glib there are several function declarations that look like this:
gboolean
(g_atomic_int_compare_and_exchange_full) (gint *atomic,
gint ...
87
votes
6
answers
19k
views
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
// ...
87
votes
3
answers
19k
views
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 ...
83
votes
11
answers
34k
views
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 <...
66
votes
7
answers
11k
views
Does int main() need a declaration on C++?
I was taught that functions need declarations to be called. To illustrate, the following example would give me an error as there is no declaration for the function sum:
#include <iostream>
int ...
65
votes
3
answers
17k
views
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 ...
46
votes
2
answers
2k
views
Function default argument value depending on argument name in C++ [duplicate]
If one defines a new variable in C++, then the name of the variable can be used in the initialization expression, for example:
int x = sizeof(x);
And what about default value of a function argument? ...
40
votes
4
answers
42k
views
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 ...
37
votes
2
answers
32k
views
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. ...
29
votes
3
answers
25k
views
How does ampersand in the return type of a function declaration work? [duplicate]
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 = ...
27
votes
3
answers
3k
views
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 ...
26
votes
11
answers
15k
views
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 ...
25
votes
2
answers
18k
views
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., ...
23
votes
1
answer
3k
views
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 ...
19
votes
7
answers
12k
views
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<T>(input: T?) {
if (input != nil) {
print(input!);
}...
17
votes
1
answer
2k
views
If arrays are passed by reference, why should I use int(&)[]? [duplicate]
Consider:
#include <iostream>
using namespace std;
void Change(int arr[3]) {
for (int i = 0; i < 3; i++) {
arr[i] = 1;
}
}
int Test() {
int arr[3] = { 0, 0, 0 };
...
17
votes
5
answers
3k
views
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,...
17
votes
3
answers
2k
views
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, ...
15
votes
2
answers
3k
views
What does this declaration typedef void foo(); mean? [closed]
I don't understand the meaning of typedef void interrupt_handler();. Could someone explain it with some examples?
typedef void interrupt_handler();
14
votes
3
answers
17k
views
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();
...
14
votes
1
answer
4k
views
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);...
14
votes
3
answers
4k
views
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 ...
13
votes
5
answers
5k
views
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 ...
12
votes
6
answers
1k
views
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 (...
12
votes
2
answers
15k
views
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)?
12
votes
2
answers
6k
views
OCaml: Declaring a function before defining it
Is there a way to declare a function before defining it in OCaml? I'm using an OCaml interpreter.
I have two functions:
let myFunctionA =
(* some stuff here..... *) myFunctionB (*some stuff *)
...
12
votes
5
answers
11k
views
extern declaration and function definition both in the same file
I was just browsing through gcc source files. In gcc.c, I found something like
extern int main (int, char **);
int
main (int argc, char **argv)
{
Now my doubt is extern is to tell the compiler that ...
12
votes
4
answers
3k
views
Why can't a typedef of a function be used to define a function?
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 ...
12
votes
2
answers
3k
views
How to prevent error : this old-style function
I am following a tutorial and my code seems normal but I got a message which says
This old-style function definition is not preceded by a prototype
code.c :
void viderBuffer()
{
int c = 0;
...
11
votes
7
answers
1k
views
Is it good programming practice in C to use first array element as array length?
Because in C the array length has to be stated when the array is defined, would it be acceptable practice to use the first element as the length, e.g.
int arr[9]={9,0,1,2,3,4,5,6,7};
Then use a ...
11
votes
3
answers
2k
views
Why is it allowed to omit the first dimension, but not the other dimensions when declaring a multi-dimensional array?
Why it is not necessary to mention first dimension of multidimensional array and necessary to mention other dimensions:
int A[][][2]={{{1,2},{3,4}},{{4,5},{5,6}}}; // error
int A[][2][2]={{{1,2},{3,...
11
votes
1
answer
4k
views
Linker can't find function definition in a namespace
I get this /tmp/ccnL7Yz1.o: In function 'main':
main.cpp:(.text+0x70): undefined reference to 'dng::genDungeon()'
main.cpp:(.text+0xf0): undefined reference to 'dng::clrDungeon(char**)'
collect2: ...
11
votes
1
answer
5k
views
How to define a "callable" parameter in a Python docstring?
Consider an implementation of filterNot (basically the opposite of filter):
def filterNot(f, sequence):
return filter(lambda x: not f(x), sequence)
The parameter f can be a "function" or a "...
11
votes
2
answers
836
views
What if I declare a function with empty parameter table, then pass arguments to it?
For example,
#include <stdio.h>
void foo();
int main(void)
{
foo();
foo(42);
foo("a string", 'C', 1.0);
return 0;
}
void foo()
{
puts("foo() is called"...
10
votes
3
answers
3k
views
Why parentheses are important in function pointer declaration?
I don't understand why the declaration below is accepted:
typedef void (*_tStandardDeclaration)(LPVOID);
while the following doesn't:
typedef void *_tDeclarationWithoutParenthesis(LPVOID);
...
10
votes
3
answers
942
views
an error about C struct array in formal parameter
I have got the following code:
struct student_info;
void paiming1(struct student_info student[]);
struct student_info
{
int num;
char name[6];
};
The IDE gives an error
error: array ...
9
votes
3
answers
4k
views
Finding out which functions are called within a given function [duplicate]
Possible Duplicate:
Generating a Call Graph in R
I'd like to systematically analyze a given function to find out which other functions are called within that very function. If possible, ...
9
votes
1
answer
688
views
Forward-declaration of a `constexpr` function inside another function -- Compiler bug?
While producing a MCVE for this problem I stumbled upon, I've found the following discrepancy between compilers:
Consider the following code :
// constexpr int f(); // 1
constexpr int g() {
...
8
votes
5
answers
24k
views
Does C support optional null parameters?
In Python, I'm used to things like
def send_command(command, modifier = None):
and then the modifier argument is optional, and the absence of the argument can be differentiated from an argument of ...
8
votes
2
answers
1k
views
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 ...
8
votes
2
answers
310
views
How do I interpret this declaration that appears to be a function declaration, but doesn't fit the usual mould?
I'm trying to decipher this declaration from sqlite3.c
SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
It seems like it is declaring a function because subsequently ...
7
votes
3
answers
20k
views
Function without return type specified in C
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 ...
7
votes
3
answers
2k
views
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);
6
votes
4
answers
929
views
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)
{
...
6
votes
4
answers
878
views
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 * ...
6
votes
2
answers
161
views
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 ...
6
votes
2
answers
930
views
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: ...