Search code examples
cvoid

Is it legal to declare a function that doesn't return anything and doesn't have any arguments?


I've seen functions that don't return anything and they are declared as:

void foo()

I've also seen functions that don't have any arguments as:

int foo(void)

Is it legal and good practice to declare both at the same time? as in:

void foo(void)

Solution

  • As mentioned in comments, good practice depends on context. Take following function which does not take any argument and does not return anything as an example:

    void drawSpecialSeparator(void) {
        int i = 0;
        for (; i < 10; i++) {
            printf("%s", (i % 2) ? "--" : "++");
        }
    
        printf("\n");
    }
    

    can be a simple good example that modularize your code. In addition, It increases your code readability and you can reuse it later easily.

    So, it is good practice to use such functions in correct context.

    Side note: I personally never use these types of function to change global variable. Maybe it is just my personal taste, but I believe changes in global variables using these function hugely increase code ambiguity.