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
1 answer

Swap variables that pointers point in

This function replaces values ​​of two integer variables using two pointers. I need to make function which will make p1 point to b and p2 point to a. #include void swap(int *p1, int *p2) { int temp = *p1; *p1 = *p2; *p2 =…
user17936437
2
votes
3 answers

segmentation fault, c programm

I had to write a program in c as a homework assignment. The program checks if the inputed string is a palindrome. Unfortunately I am getting a segmentation fault error when executing my program. According to my knowledge segmentation fault means I…
2
votes
3 answers

Copying elements of a linked list to another linked list in reverse order in C

I'm new to programming in C and taking a course. I'm having trouble with one of the tasks I'm practicing. I'm supposed to Write a program that creates a linked list of 10 characters, then creates a copy of the list in reverse order. I have written…
2
votes
2 answers

runtime error: member access within null pointer of type 'struct ListNode' [solution.c]

I am trying to solve the merge k sorted lists question on leetcode using the non-recursive solution. When I run the code it gives the error as mentioned below. Can you help me to solve the problem? Error is at lists[min_idx] =…
2
votes
3 answers

Attempt to reverse a string results in a blank string

I'm newbie in C and I'm trying to do some function to reverse a word, here my code: #include #include // ƒ to revers the word void lettreCount(char *saisi, int length){ int i, j = 0; char revers[length]; for (i =…
2
votes
2 answers

How to fix "error:expected primary-expression before ‘,’ token"

I am trying to make a first fit memory management code but everytime I try to run it I keep getting incomplete results in the output and these errors error:expected primary-expression before ‘,’ token I don't know what to put in the code to resolve…
2
votes
3 answers

Running until a certain value linked list

I want to sum the nodes until 0 is reached and update the original linked list with the new values. Note: it skips 0 until it reaches a number to calc sum or the end of the linked list. Definition of node: struct Node { int data; Node*…
2
votes
3 answers

Recursive positive digits sum program

I'm facing an issue, the program almost works correctly except it gives +1 in output result. For example num input is 123 = 1+2+3=6, instead it gives 7 as output. I can simply fix this problem by adding -1 in the printf statement, but I want to know…
MaxX
  • 33
  • 6
2
votes
1 answer

Get the wrong answer——about uncertain number of variables

When I ran the program below in the Linux system, I can't get the expected answer "9". But I can get it in the windows system. Why does this happen? #include int sum(int num, ...){ int* p = &num + 1; int res = 0; …
Breeze
  • 23
  • 5
2
votes
2 answers

Get segmentation fault when manipulating with malloc in c

I am implementing a program to divide all value in a array by 100 then store them in b array using malloc. The problem is I got segmentation fault when printing value of b in main. This is my code #include #include void…
2
votes
2 answers

C: print the longest common prefix

Beginner programmer here, trying to figure out how to find and print the longest common prefix in C. I have a base here for the program. #include void findprefix(char *str1, char *str2, char *found); int main(void) { char str1[100]; …
2
votes
3 answers

Adding two corresponding array elements and return the resultant array

Facing segmentation fault in the following code . Can anyone help me fix it? #include int* multiply(int *arr1, int *arr2, int m); int main(void){ int m = 0; printf("Enter size of array1 and array2 >"); …
2
votes
1 answer

trim function halve the memory size to remove the whitespaces?

I have to create a function (trim function) who can perform this task: taking a null terminated string and if at the 0-th position of the string there's a whitespace, remove that whitespace. Same thing if the whitespace is at the end of the string…
2
votes
1 answer

Binary Search Tree root node stays NULL

for some reason my root node consistently stays NULL and nothing gets added to it. I am unsure as to why, and was wondering if someone could guide me as to what I am doing wrong. I am trying to read a file and perform different actions for every…
2
votes
3 answers

Why do I have to define virtual function in a base class?

I'm trying to create a simple base abstract class with a virtual function and a child class that defines that virtual function. Running the following produces an error during compilation: #include using namespace std; class Animal…
kandi
  • 1,098
  • 2
  • 12
  • 24