What are Declaring Functions With No Arguments- Best C Programming Course

🚀 Learn BLE on Linux (BlueZ) – Programming Part 1

Hands-on Bluetooth Low Energy course. Special price: ₹399

Enroll Now on Udemy →
💬 Need help or career guidance? Ask us!Contact Us →

Declaring Functions With No Arguments

Lecture 16 of our free embedded C course: why void in the parameter list matters, and how empty parentheses can hide bugs in embedded C programming.

In this lecture of our free embedded C course we study a tiny detail that causes surprisingly large bugs: how to declare a function that takes no arguments. The two spellings, empty parentheses and the word void, look equivalent but behave differently in classic C. Anyone serious about embedded C programming and the wider free C programming course path should learn this early, because firmware is full of functions such as led_toggle, timer_start and system_reset that take nothing.

What You Will Learn

  • The two ways to write a parameterless function declaration
  • Why empty parentheses mean unknown arguments in classic C
  • How void in the parameter list lets the compiler check calls
  • Using void as a return type to prevent misuse in expressions
  • How newer C standards changed the meaning of empty parentheses
  • Typical firmware examples and the mistakes to avoid

Prerequisites

Know what a prototype is and why it appears before the first call, as covered in the previous lecture. Familiarity with return types and with printf is enough for the examples that follow.

Two Spellings for the Same Idea

Suppose you have a function that blinks a status LED and needs no input. You can write its declaration in two ways:

void led_blink();        /* empty parentheses */
void led_blink(void);    /* explicit void     */

A beginner could assume these are identical, and in a modern compiler set to the latest standard they are. But for decades the two forms meant different things, and a huge amount of existing firmware was written under the older meaning. Understanding the difference helps you read legacy code and write safe code today.

What Empty Parentheses Really Say

In classic C, a declaration with empty parentheses tells the compiler only that a function exists and what it returns. It says nothing about the parameters. The function might take zero arguments, one, or ten. Since the compiler has no information, it cannot compare a call against anything, so every call is accepted.

What the Compiler Knows From Each Form
void f(); → return type known parameters unknown
void f(void); → return type known parameters: none, checked

Experiment: Calling With Extra Arguments

#include <stdio.h>

void ping_a();
void ping_b(void);

int main(void)
{
    ping_a(7, 'x');      /* accepted silently in classic C   */
    ping_b(7, 'x');      /* compile time error               */
    return 0;
}

void ping_a()
{
    printf("ping A\n");
}

void ping_b(void)
{
    printf("ping B\n");
}

If you compile the file with a classic standard such as C99 or C11, the call to ping_a passes without complaint, while the call to ping_b stops the build with a message about too many arguments. That asymmetry is the whole lesson. Let us see the error text and then a corrected program.

gcc -std=c11 -Wall cp6_16_demo.c
cp6_16_demo.c:9:5: error: too many arguments to function 'ping_b'
#include <stdio.h>

void ping_b(void);

int main(void)
{
    ping_b();
    return 0;
}

void ping_b(void)
{
    printf("ping B\n");
}
ping B

Why the Silent Version Is Dangerous

A call with unexpected arguments to a function that takes none is almost always a programmer mistake. Maybe a function was refactored and lost a parameter, or maybe a call was copied from somewhere else. With empty parentheses the compiler lets the mistake through, and the extra values are pushed and ignored, or on some architectures disturb the stack or registers in unpredictable ways. Such bugs show up only on hardware, weeks later, and take long to find.

Rule of thumb: A bug that the compiler can catch for you is the cheapest bug you will ever fix. Writing void turns an invisible mistake into an immediate compile error.

Using void as the Return Type

The same keyword protects you in the other direction. If a function produces no value, declare its return type as void. The compiler then refuses to let you use the call inside an expression, which prevents accidental use of a meaningless result.

#include <stdio.h>

void log_boot(void);
int  read_status(void);

int main(void)
{
    int s = read_status();
    log_boot();
    /* int bad = log_boot();   error: void value not ignored */
    printf("status=%d\n", s);
    return 0;
}

void log_boot(void)
{
    printf("system booting\n");
}

int read_status(void)
{
    return 3;
}
system booting
status=3
DeclarationTakes input?Gives output?Checked by compiler?
void f(void);NoNoBoth sides checked
int f(void);NoYes, intArguments rejected, result usable
void f(int);YesNoArgument checked, result unusable
int f();UnknownYes, intArguments unchecked in classic C

What Changed in Newer C Standards

The empty parentheses style was marked obsolescent for a long time. In the C23 standard, an empty parameter list in a declaration now means the same as void, so the difference disappears for code compiled with that standard. However, plenty of toolchains used for microcontrollers still default to older standards, and your code may be built with different compilers during its life. Writing void explicitly works correctly everywhere, which is why professional style guides and standards such as MISRA C insist on it.

Practical advice

Always write (void) for functions with no parameters, in both the declaration and the definition. Then your intent is obvious and your code is portable across every standard.

Real Firmware Examples

Here is a small pattern you will see again and again in embedded C programming: a set of parameterless hardware helpers declared in a header.

/* board.h */
#ifndef BOARD_H
#define BOARD_H

void board_init(void);
void led_on(void);
void led_off(void);
void delay_tick(void);
int  button_pressed(void);

#endif
/* main.c */
#include "board.h"

int main(void)
{
    board_init();
    for (;;) {
        if (button_pressed()) {
            led_on();
        } else {
            led_off();
        }
        delay_tick();
    }
}

These functions get their data from hardware registers or global state, so they need no parameters. Declaring each with void documents that fact and lets the compiler guard every call site. The same style is used for interrupt service routines and reset handlers, where the signature is fixed by the architecture.

Typical Parameterless Calls in a Firmware Loop
main loop → button_pressed() → led_on() or led_off() → delay_tick() → repeat

Common Mistakes and Troubleshooting

Writing void in the declaration but empty parentheses in the definition: Some compilers accept it, others warn. Keep them identical.

Putting void next to a name: Writing void f(void x) is an error, because void alone is allowed only as the sole parameter.

Using the result of a void function: The compiler will reject it. Change the return type if you really need a value.

Ignoring warnings about strict prototypes: Turn on the flag below and fix every hit.

gcc -Wall -Wextra -Wstrict-prototypes -Wold-style-definition main.c

Best Practices

  • Always write void for an empty parameter list
  • Use void as the return type when a function produces no value
  • Keep declaration and definition in agreement
  • Enable strict prototype warnings in your build system
  • Follow a coding standard such as MISRA C if you work on safety related firmware

Summary and Key Takeaways

  • Empty parentheses in classic C mean the parameters are unspecified
  • Explicit void tells the compiler that no arguments are allowed
  • Void as a return type blocks accidental use in expressions
  • C23 changes the meaning of empty parentheses, but explicit void works everywhere
  • Strict warnings help the compiler find mistakes for you

With this lecture you have taken another step in our free embedded C course. Next, we look at what happens if you call a function that has no declaration at all, and how mismatched arguments behave.

Frequently Asked Questions

What is the difference between f() and f(void) in C?

In classic C, f() leaves the parameter list unspecified, so the compiler cannot check calls, while f(void) states clearly that there are no parameters.

Should I write void in the function definition too?

Yes. Matching the declaration keeps things consistent and avoids old style definition warnings.

Does C23 make this topic irrelevant?

For code built with that standard, empty parentheses mean no parameters. Older toolchains remain common, so explicit void is still best.

Can a function with void return type use a return statement?

Yes, a bare return is allowed to exit early, but it cannot carry a value.

Why do embedded coding standards require void?

They want every call checked by the compiler, since firmware bugs are expensive to find on hardware.

Is this part of a free C programming course for embedded systems?

Yes, it belongs to the Functions chapter of our free embedded C course.

Learn C for Embedded Systems Free

Explore the full free C programming course and practise every concept with runnable examples.

💬 Need help or career guidance? Ask us!Contact Us →

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

×

🚀 BLE on Linux (BlueZ)

Hands-on course. Only ₹399

Enroll Now →