Search code examples
cfunctionparametersargumentsvoid

Error : Expected parameter declarator in C



Hello Everyone.

I have an error : Expected parameter declarator.

How can I fix this problem ? I am new in programming. Thanks in advance.

#include <stdio.h>



void sum (int n1,int n2)

{

  int result;

  result = n1 + n2;

  printf("%i",result);

}



int main ()
{

  void sum (2,4);

  return 0;

}

Solution

  • In this case, because your function has declared type of void, it does not return anything. And when you want to invoke that function, just use its name and pass the parameters in the parentheses.

    Another way is you can have your function return a sum of an integer, then you need to assign it to another variable or use it in place of an integer .

    Example below

    #include <stdio.h>
    
    void sum (int n1,int n2)
    {
      int result;
      result = n1 + n2;
      printf("%i",result);
    }
    
    int sum1(int m1, int m2)
    {
        return m1+m2;
    }
    
    int main ()
    {
      sum (2,4);
    
      int result = sum1(3,5);
      printf("\n\nThe result of sum of 3 and 5 is %i.\n", result);
      printf("The result of sum of 2 and 2 is %i.\n", sum1(2,2));
      return 0;
    }