Questions tagged [function-definition]

Use this tag for questions specifically about the rules, syntax, or behavior of function definitions but not for questions that happen to have code with function definitions in them but are not about function definitions.

Only use this tag if the issue at hand is about or related to the function definition.

A function definition is the definition of a user defined function along with the code the function contains. This is distinct from which only defines the name and arguments of a function in certain programming languages. Example in C:

void SayHello(void); // Function declaration

void SayHello(void) { // Function definition
    puts("Hello world!");
}

Some languages do not have function declarations and only have function definitions. Example in JavaScript:

function SayHello() { // Function definition; no forward declaration is required in JavaScript
    console.log("Hello world!")
}

Function definitions are very common in programming. Many questions will contain them, but only use this tag if they are part of the question, and not just part of code being used to demonstrate an unrelated problem. Consider using other tags in these cases:

  • Use if the question is about functions in general.
  • Use if the question is about forward declarations of functions and not about defining the code of the function.
963 questions
934
votes
11 answers

What does -> mean in Python function definitions?

I've recently noticed something interesting when looking at Python 3.3 grammar specification: funcdef: 'def' NAME parameters ['->' test] ':' suite The optional 'arrow' block was absent in Python 2 and I couldn't find any information regarding its…
Krotton
  • 9,561
  • 3
  • 15
  • 12
86
votes
6 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…
21
votes
3 answers

is there any way for multiple where statement in Haskell

i tried to write 3-4 where statement in a one function but i get error and couldnt do it , i tried to do something like that : foo x= | x == foo1 = 5 | x == foo2 =3 | x == foo3 =1 | otherwise =2 where foo1= samplefunct1 x foo2= samplefunct2…
15
votes
1 answer

Should I define my Cython function using def, cdef, or cpdef for optimal performance?

How can I know whether to use def, cdef or cpdef when defining a Cython function, assuming I want optimal performance?
PDiracDelta
  • 2,348
  • 5
  • 21
  • 43
8
votes
3 answers

How to pass pointer to function and dynamically allocate memory within function C++

I'm trying to declare a pointer and pass that pointer to a function where memory is allocated. Here is a minimal example: #include #include using namespace std; void alloc_mem(int &size, double *x); int main() { …
7
votes
2 answers

Is it possible to define a function dynamically in ZSH?

I would like to define a series of functions dynamically in ZSH. For example: #!/bin/zsh for action in status start stop restart; do $action() { systemctl $action $* } done However, this results in four identical functions which…
7
votes
2 answers

Function definitions of built-in functions in C

We include header files like stdio.h in our C programs to use the built-in library functions. I once used to think that these header files contained the function definitions of the built-in functions that we may use in our programs. But soon found…
J...S
  • 5,079
  • 1
  • 20
  • 35
7
votes
1 answer

Error lnk2005 already defined in .obj

There are many question about this error. But they are related to only a single variable. test.h namespace World { enum Objects { TERRAIN = 1, BOX = 2, SPHERE = 4, CAPSULE = 8 }; void…
zakjma
  • 2,030
  • 12
  • 40
  • 81
6
votes
1 answer

Finding the type of a composition function in Haskell

Let's say I have the following function in Haskell: compose2 = (.) . (.) How would I go about finding the type of this function? Because of the multiple compositions I get stuck trying to determine what the type of this function would be. I know I…
Miles Cox
  • 63
  • 3
6
votes
3 answers

Python3 function definition, arrow and colon

I have found the following python function definition: def reverseString(self, s: 'List[str]') -> 'None': I don't quite understand 'List[str]' and -> 'None'. I have found that the arrow is a function annotation but I couldn't find anything useful…
Amir
  • 317
  • 5
  • 18
6
votes
4 answers

Swapping 2 arrays in C

I need to swap the values of 2 arrays in a function. The problem is I can change anything in the main, just the function itself. It should recive 2 integer arrays, and swap those. The problem is, that I don't know the size of the arrays, and for my…
Dr.Java
  • 73
  • 1
  • 1
  • 5
6
votes
2 answers

Segmentation Fault chkstk_ms C++

I need help with this following counting sort implementation. Is it because value of x can be too big? I am getting segmentation fault. This is what gdb says: Program received signal SIGSEGV, Segmentation fault. ___chkstk_ms () at…
ARSN
  • 167
  • 1
  • 10
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
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
5
votes
1 answer

Struct declared immediately after function name/parameters but before brackets. Can someone explain this C syntax?

Here is the code: A_output(message) struct msg message; { } I've never seen syntax like this before. What is that struct definition doing? Is that just another way of specifying the "type" of "message" in the parameter field? So, is it the same…
Lv99Zubat
  • 853
  • 2
  • 10
  • 27
1
2 3
64 65