Home > Net >  Can I declare function without parameters and later define it with parameters?
Can I declare function without parameters and later define it with parameters?

Time:01-24

Can I declare a function without parameters and later define it with parameters?

I tried to define a function with no parameters inside, and later on declare it with parameters? Can I do it in C?

#include<stdio.h>
void change_order(); //Here Here
int main()
{
    int num[3];
    for(int i = 0; i<3; i  )
    {
        printf("Enter Number%i:", i 1);
        scanf("%i", &num[i]);
    }
    change_order(num, 0, 1);
     for(int i = 0; i<3; i  )
     {
         printf("%i\n", num[i]);
     }
}

void change_order(int array[], int index1, int index2 )
{
    if(array[index1]>array[index2])
    {
      int Temp = array[index1];
      array[index1] = array[index2];
      array[index2] = Temp;
    }
}

One More Question: Can I write void change_order(int array[int n])?

CodePudding user response:

This declaration:

void change_order();

States that the function change_order takes an unknown number of parameters and returns void. The definition of the function matches this.

However there is one additional requirement. Because the caller doesn't know the number or type of the parameters, the actual parameters cannot have a type that would be subject to promotion. This means parameters of type char and short (either signed or unsigned) which would get promoted to int or unsigned int, and parameters of type float which would get promoted to double, would not be allowed.

The definition of change_order does not use any parameter whose type would be subject to promotion, so the declaration is valid.

Using an empty parameter list in a declaration is considered an obsolescent feature, as it prevents compile type checking of parameters, and should be avoided in favor of a declaration which declares the types of its parameters.

CodePudding user response:

Up to and including C17 (C18), you can get away with this.

Starting with C23, when it is released, that will not be allowed.

In older versions of the standard, the declaration of void change_order(); says that the function exists and returns void (nothing), but says nothing about the number or types of its arguments, except that it is not a variadic function (similar to printf() — its prototype does not end with , ...). It does not provide a prototype for the function — only a declaration.

In C23, that declaration will say the same as void change_order(void); — a function that takes no arguments and returns no value. The definition with arguments will then clash with the declaration and be rejected, and the call with arguments will likewise be rejected.

Do not use the obsolescent notation — it has a useful lifetime measured in months at best.

You also ask whether you can write void change_order(int array[int n]).

The answer is No. It might be possible to write void change_order(int array[n]); if n is suitably defined. However, the normal way to write a function with a variable length array is void change_order(int n, int array[n]), or use size_t n for preference.

  • Related