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

I'm getting unknown characters in output in C

I'm doing this exercise: Write a program that reverses the words of a sentence like this: My name is John --> John is name My and I wrote this: #include #include #include int main(){ int word=0,character=0; …
Bonsai99
  • 23
  • 2
2
votes
1 answer

"name followed by '::' must be a class or namespace name" error when making a template class

Node.cpp code : #include "Node.h" using namespace std; template Node :: Node(t nodeDate){ data = nodeData; } Node.h code : #ifndef NODE_H #define NODE_H using namespace std; template class Node{ public: Node(t…
2
votes
4 answers

My strstr function works, but a program designed to test it says it doesn't

for school, I have to recreate the strstr function. I did, and I tested it as much as I could, and everything seems to work perfectly. I am still looking into it but after several hours of troubleshooting, I don't get it. Can someone tell me what's…
Toyama70
  • 23
  • 3
2
votes
1 answer

Getting incompatible integer to pointer conversion error in program. Unsure how/why exactly this is occurring but looking for an explanation

I'm trying to count how many dashes "-" there are in char p[]. I loop over the string, and use the strcmp function to compare what is at the p[i] location to "-". The strcmp function returns 0 if the they are the same. int howmanyDash( char p[] ){ …
zackychan97
  • 112
  • 1
  • 9
2
votes
1 answer

error in c++ matrix*vector multiplication

I need a function that multiply a matrix and a vector (Matrix*vector) It takes in a matrix A and a vector B, with int describing the dimensions. Somehow it isn't running correctly. Any help?? void Multiply(double *res, double **A, double *B, int…
DHJ
  • 611
  • 2
  • 13
2
votes
5 answers

How can I access the certain indexes of string using the function?

I was practicing and found some exercise on the internet and it says that I should copy str2 to str1, perhaps the main problem is that I should copy only a certain index of that string (so from str2[a] to str2[b]). Therefore A and B should be…
2
votes
3 answers

skipping spaces in C using pointers

I've been asked to do this: Make a skip_spaces() function accepting a string s, that returns a reference to the first element in the array that is not a space character (if the string is only composed of spaces, the pointer will address the null…
froggyalex
  • 33
  • 7
2
votes
1 answer

Why this C-program doesn't gives the reverse of given string?

Why this program doesnot gives reverse of the given string computer, though the length() function works fine(when I comment other codes and only run that part) and gives output correct but the second reverse() function is not giving any…
2
votes
2 answers

Merge 2 sorted arrays into vector c++

Hi I'm trying to merge 2 sorted array into a vector. The following is my code. I believe that I am using the correct algorithm however, when I print the vector out its only prints 1,2. Can someone please take a look at my code? #include…
Vincent
  • 71
  • 5
2
votes
4 answers

strncat causes buffer overflow

how can I use strncat with heap objects? Im trying to write a simple function to concatenate 2 strings together returning the result, however, I can not run it without making the return buffer very large (adding approximately an additional 5000 to…
2
votes
2 answers

Find String Function in [ C ]

Needing a function that would check me for the existence of a substring and give me the location I created this function, I wanted to know if something similar already exists in the C headers and how it works. This own function give the position…
ExagonX
  • 141
  • 10
2
votes
1 answer

C - checking if strings contain any non-null characters

I have a function with two strings parameters. I have to check if both strings contains any non-null characters. Here is an example code: void fun(const char* str1, const char* str2) { if (!str1 || !str1[0] || !str2 || !str2[0]) { …
Irbis
  • 1,432
  • 1
  • 13
  • 39
2
votes
2 answers

Understanding function which implements foldr and foldl

There is some case where I don't understand how foldr and foldl are used in function. Here is a couple of example, I then explain why I don't understand them: -- Two implementation of filter and map map' f = foldr (\x acc -> (f x):acc) [] map'' f…
JavaDumbell
  • 215
  • 2
  • 11
2
votes
2 answers

Iterate Over Array in Another Function in C

I have an array of unknown size populated while reading the file in main function. I would like to write another function that iterates over this array, compare strings and return index of requested string. However, I seem cannot iterate over all…
J Szum
  • 548
  • 1
  • 4
  • 12
2
votes
3 answers

Why am I always getting 0 always after running my c program? Kindly help me out

#include #include float distance(float a,float b,float c,float d); int main() { float x1,y1,x2,y2,dist; printf("Input x1: "); scanf("%f", &x1); printf("Input y1: "); scanf("%f",…