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

Error when calling function to sort array of n numbers

I am trying to write a function by the name of selection_sort. This function should, when presented with an array of n integers, search the array to find the largest element then move it to the last position of the array. Once it has done this it…
Guthrie
  • 75
  • 6
3
votes
3 answers

Function to reverse print

Yesterday I had to solve an exam exercise, which, unfortunately, I failed.. The exercise was to create a function in C with the following rules: Write a function that takes a string and displays the string in reverse order followed by the…
πrre
  • 33
  • 5
3
votes
2 answers

Getting address boundary error when working with pointers in C

The following code gives me a terminated by signal SIGSEGV (Address boundary error): void rec(int x, int *arr, int *size) { if (x < 0) { rec(-x, arr, size); return; } arr = realloc(arr, sizeof(int) * ++(*size)); *(arr + (*size) -…
3
votes
2 answers

finding out if all the digits of a number are equal

So I want to start by saying that I already solved the problem, but there is something that is bugging me, Here is the code first: #include int flag = 1; int controlNumber(int); int main() { int array[10] = { 233, 45, 777, 81,…
blake
  • 107
  • 1
  • 7
3
votes
1 answer

How to compare a string with a substring to transform the equal parts between the first and second into '*' without using

I have to request a first word to compare it to a second word, and replace all occurrences with '*' working character by character without using the library. Exercise: Write a C program that receives two words entered from the keyboard as…
3
votes
3 answers

How to reverse a string with pointers only?

I'm trying to reverse a string, but it just stays the same. I don't use any modules except and . void rev(s){ char i, temp; char *sf = s; char ri = strlen((s) - 1); char *sl = &s[ri]; for (i = 0; i < ri;…
Deno
  • 57
  • 5
3
votes
5 answers

Why is this version of strrev faster than mine?

I can't read assembly code, so my assumptions may be completely wrong! Here's my code : void reverse(char* str) { size_t size = strlen(str) / 2; char tmp; for (int i = 0; i < size; ++i) { tmp = str[size - i - 1]; …
S.Sot
  • 321
  • 1
  • 7
3
votes
3 answers

Should I define functions in .h file or just declare them?

I'm new to C and I came across this statement: "Functions need to be declared in .h files and not defined with exception of inline functions". My question is then, where are standard functions defined?
3
votes
2 answers

Virtual overloaded operators >> and <<

I need an interface that would require its subclasses to overload << and >>, but I'm not quite sure how since these operators aren't overloaded as member functions: std::istream& operator>> (std::istream& in, Student& student) { in >>…
3
votes
3 answers

comparing the ending of the strings

I am writing a program to compare different strings. Specifically chemical elements that end with an OH. I have to return -1 if the string ends with OH. However, my program doesn't work. Where am I wrong? #include #include int…
user13442873
3
votes
5 answers

Recursively removing duplicate characters in a string

I'm trying to create a recursive function which removes the consecutive duplicate characters from a string. It works fine except the first few characters. For example if my input is MMMMMuuuuuOOOOOKKKKLLLEE OOOOLLL or something like this, output is…
gokbeykeskin
  • 149
  • 9
3
votes
4 answers

How to reverse order of a set array

This is an exercise for my class, and I'm not sure how to go about the function that needs to be made to reverse order. #include #include void reverseorder(int[], int); void printout(int[], int); const int SIZE = 10; int…
SalvGi
  • 59
  • 9
3
votes
4 answers

How come that in C declaring a variable between a function name and the function block compiles as valid?

At C lang FAQ I found the following code: void f(ipp) int **ipp; { static int dummy = 5; *ipp = &dummy; } I tested compiling it with -Wall -std=c11 -pedantic flags, and it compiled with no warnings or errors. How is that possible - a…
Theo d'Or
  • 783
  • 1
  • 4
  • 17
3
votes
1 answer

How to locate where a built-in function is defined?

In MATLAB, there are roughly 3 ways to define functions: non-comment-only .m files, .p files, and compiled code (e.g. DLL, MEX). Knowing where a function is defined could be helpful in several cases, such as when a breaking change was introduced to…
3
votes
4 answers

Loop through array of unknown size C++

I am trying to traverse a double array of an unknown size. Why does the following not work? for (unsigned int = 1; i < sizeof(anArray)/sizeof(double); i++) { ... } Everything compiles fine (g++ -Wall -Werror -std=c++11 app.cpp -o app), but…
Mike B
  • 125
  • 1
  • 1
  • 10
1 2
3
64 65