Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is it not possible to use implicitly typed function arguments with explicitly typed function arguments?

If i were to compile the below code:

// x is implicitly typed as int
void foo(x, char y) {
    return;
}

a syntax error would occur (expected identifier), but im not sure why the compiler can’t handle this syntax.

however if i do:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

void foo(int x, char y) {
    return;
}

it compiles successfully.

So why can’t the compiler handle such syntax?
Im no expert at C so im not sure if this is a silly question or not.

>Solution :

Implicit argument types, or more accurately a function definition with an identifier list, was part of the original K&R design of C. In those days, you could either have a definition like this:

int foo(x, y) 
{
    return 0;
}

In which case all parameters default to type int, or a set of declarations before the body:

int foo(x, y) 
   int x;
   char *y;
{
    return 0;
}

To specify the types of the arguments. In neither case were the types of the parameters known to the caller.

Specifying the types directly in the definition was added to the language later, which also added the ability to specify the types of a function’s arguments in a declaration, allowing callers to know the types of the parameters.

So there’s wasn’t much reason to have a hybrid of the two, as one was made to improve on the other.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading