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
4
votes
6 answers

My factorial function is not returning factorial

I am not able to find out why my function returns the user input only rather then the factorial of the input. #include #include int factorial(int x) { //int x; int sum = 1; while (x!=0){ sum = sum * x; …
4
votes
3 answers

How to sort an array that going down and then going up, to array that going down all the way?

I have an assignment to make a function that sort array that going down and then going up (for example: 9, 8, 7, 6, 5, 7, 11, 13) to array that going down all the way (for example: 13, 11, 9, 8, 7, 7, 6, 5). I wrote this in online compiler…
Alfa Hores
  • 347
  • 2
  • 10
4
votes
1 answer

SIGSEGV on access to pointer to left node of binary tree, even though the pointer is initialized

I am trying to create a function which returns the mirrored copy of a binary tree. By "mirrored" I mean a tree with each left node as its right node and vice versa. The one on the left gets copied to resemble the one on the right. This is the code…
4
votes
3 answers

Why should we use double pointer for adding node in front/back, but not in the middle?

I have this code which creates new node at the very front: void push(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next =…
4
votes
3 answers

using binary search to find the first capital letter in a sorted string

I wrote the following code to find the first capital letter in a string using binary search: char first_capital(const char str[], int n) { int begin = 0; int end = n - 1; int mid; while (begin <= end) { mid = (begin +…
Jason
  • 85
  • 5
4
votes
3 answers

Binary search tree lookup output

I am attempting to implement a lookup function for a binary search tree. While it does return true if I lookup the root of the tree, when I lookup other entries that are in the tree it returns false. When I debugged it the function seemed to return…
4
votes
1 answer

R: Where are formals for a function stored in memory?

When a function has been defined but has not yet been called, do the formals that do not have default values exist? If they do, do they exist in the execution environment, or in the environment where the function definition is located, or somewhere…
andrewH
  • 2,281
  • 2
  • 22
  • 32
4
votes
1 answer

How to hash a class or function definition?

Background When experimenting with machine learning, I often reuse models trained previously, by means of pickling/unpickling. However, when working on the feature-extraction part, it's a challenge not to confuse different models. Therefore, I want…
lenz
  • 5,658
  • 5
  • 24
  • 44
4
votes
4 answers

Returns in a recursive function

I am trying to understand how to use recursion in C, and I can't get how return works in it. Please consider the following code: int recur(int i) { printf("recur: i = %d\n", i); if (i < 3) { recur(i + 1); return 10; …
nounoursnoir
  • 671
  • 2
  • 10
  • 25
4
votes
2 answers

Nested `constexpr` function calls before definition in a constant-expression context

From what I gather from this answer, a constexpr function's result is not a constant-expression if the function has not been declared yet. What surprises me is the following code snippet : constexpr int f(); constexpr int g() { return…
3
votes
3 answers

string splitter in C - how is it working?

I have inherited a large code base and there is a utility function to split strings on : char. I understand about 80% of how it works, I do not understand the *token = '\0'; line. Any pointers are highly appreciated. #include #include…
Yoshiro
  • 53
  • 1
  • 3
3
votes
2 answers

Counting the number of occurences of a specific word within a string

The problem with this function is that it looks after all the substrings, but not for words like for example if I'm looking for "hi" within "hifive to to evereyone" it returns 1 int HowManyString(char *satz,char *word) { int coun = 0; while…
Muhito
  • 37
  • 5
3
votes
1 answer

how to make recursive function that find the index of the biggest number in array?

I'm trying to find the index of the biggest number in array, by using a recursive function, but it doesn't work for me. I wrote this code in "Online C Complier": #include int max(int arr[], int n){ if (n==0) { return 0; } …
Alfa Hores
  • 347
  • 2
  • 10
3
votes
2 answers

Defining a recursive function inside another function in C

I'm trying to write a recursive function (printPath) inside another function (dijkstraSSSP), but it gives me a compiler error. When I run gcc dijkstra.c WGraph.c PQueue.c, it compiles just fine. However, when I run dcc -Wall -Werror -std=c11 -o…
dtrinh
  • 69
  • 5
3
votes
3 answers

C error expected ‘char **’ but argument is of type ‘char (*)[10]’

I am trying to implement my version of strcat. However, I get below warning and my code crashes while running. I am passing &p from main to make permanent changes to variable p in the main function. Warning: note: expected ‘char **’ but argument is…
Ravi
  • 173
  • 1
  • 10
1
2
3
64 65