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

Swapping arrays using pointers

I was trying to swap two arrays using pointers. What I wanted was to swap using a call by reference. This code I wrote is given below #include #include void swap(int **a, int **b) { int *temp = (int*)malloc(5*sizeof(int)); …
2
votes
2 answers

How can I convert chars(pointer) to strings in C - to be used in strcmp

I am trying to create a function that takes a string as an input and compares each character to each other to ensure they don't repeat. The string consists of 26 alpha characters The goal is to compare the 26 characters to each other to ensure that…
Sizwe
  • 51
  • 5
2
votes
1 answer

C Program bump into segmentation fault. (gdb) Cannot access memory at address

I am trying to write codes in C for dynamic array (vector), and it runs okay until I add another function pointer into the struct (line29: int (findEle)(vector, int) ), and initialize it (line 153: v->findEle = findEle) to point to the function…
k.cc
  • 23
  • 2
2
votes
2 answers

Swapping 2 void pointers addresses

I need to create a swap function that takes 2 addresses as input and swaps them regardless what type they point to. Here's my swap function: void swap(void* x,void* y){ void* temp=x; x=y; y=temp; } When I use it with integers it works fine…
2
votes
3 answers

Replicating the strcmp() function from string.h library

I am trying to replicate the strcmp() function from the string.h library and here is my code /** * string_compare - this function compares two strings pointed * by s1 and s2. Is a replica of the strcmp from the string.h library * @s1: The first…
Leuel Asfaw
  • 316
  • 1
  • 14
2
votes
1 answer

Insert a string into a sorted linked list of strings in C

I'm struggling with this problem: I want to insert a string into a sorted linked list of strings but for some reason it doesn't work. Here is the code: void insert(node** head, const char* word){ node* newElem = malloc(sizeof(node)); …
2
votes
2 answers

what does it mean to have these kind of errors in C?

this is my solution #include #include char* deletemultiple(const char* str) { if (str == NULL) { return NULL; } size_t length = strlen(str); if (length == 0) { return str; } length =…
2
votes
2 answers

Does this count as an Insertion-Sort? And why does a random number show at the end?

I've been asked to create an insertion sort for an array. My program ended differently from the teacher's, but it sorts the array. However, I'm curious if it truly counts as a proper insertion sort. Also, at the end of the sorting, some random…
2
votes
2 answers

My function that reverses a string gives me "stack smashing detected"

I am trying to write a function that reverses a given string but it gives me "stack smashing detected". Here is my code: void reverse(char *str2) { int i, j; char temp; for (i = 0, j = str2[strlen(str2) - 1]; i < j; i++, j--) { …
2
votes
2 answers

Program that prints prime numbers

Program that prints the 2 largest prime numbers in a given range from the user using recursion. I want to get the 2 largest prime numbers in a range that is given by the user, but somehow it just prints the consecutive numbers in the range. The…
2
votes
2 answers

Strtok isn't returning as expected, am I using it wrong?

#include #include #include // fiter string to the first | char* filterstringfirst(char* command, int i){ char *tok = command; int x = 0; while ((tok = strtok(tok, "|")) != NULL && x <= i) { …
2
votes
1 answer

Can't print string after reverse in C

I'm writing this function to return a char pointer of reversed string. void * PreverseStr (char str[]) { int size = strlen (str); char *returnstr = (char *)malloc (size * sizeof(char)); for (int i = size - 1; i >= 0 ; i--) { …
2
votes
2 answers

Check if number is period of sequence in C++

I need to check if number is a period of sequence. EXAMPLE: { 1, 3, 1, 4, 2, 1, 3, 1, 4, 2, 1, 3 } Periods are 5 and 10. Base period is 5 because it is the smallest period. #include #include int p=0; int…
user17936920
2
votes
2 answers

How to pass values within functions in linked lists C++

I created this code to calculate the sum of the values in a linked list entered by the user, and the expected output is to print the sum but it gives the last value and prints the wrong number of records in linked list Enter a number : 5 Enter [Y]…
2
votes
3 answers

Assign values to a global struct from a function

I am studying C from a book and I try various stuff in order to understand the language better. I am attempting to pass a value from a function to a global struct. I am having trouble because I perform a printf to see if the values were passed…