ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Somebody will correct me if I am wrong, but I believe it's related to old c code. If there was not a void as a parameter and just empty parentheses it could take a number of arguments. (think I remember reading this, but it may be a lie lol).
Heres some info on the subject from The C programming language K&R
Quote:
...A note of history: the biggest change between ANSI C and earlier versions is how functions
are declared and defined. In the original definition of C, the power function would have been
written like this:
/* power: raise base to n-th power; n >= 0 */
/* (old-style version) */
power(base, n)
int base, n;
{
int i, p;
p = 1;
for (i = 1; i <= n; ++i)
p = p * base;
return p;
}
The parameters are named between the parentheses, and their types are declared before
opening the left brace; undeclared parameters are taken as int. (The body of the function is
the same as before.)
The declaration of power at the beginning of the program would have looked like this:
int power();
No parameter list was permitted, so the compiler could not readily check that power was being
called correctly. Indeed, since by default power would have been assumed to return an int, the
entire declaration might well have been omitted.
to declare a function then it can be passed any number of arguments. You can also do the same with function pointers
Don't do this as a regular thing it will bite you in the end.:
Code:
int foo1(int a, int b)
{
return a+b;
}
int foo2(int a)
{
return a;
}
int main(int argc, char *argv[])
{
typedef int (*F)();
F test;
test=foo1;
printf("%d\n",test(1,2));
test=foo2;
printf("%d\n",test(2));
return 0;
}
Last edited by jim mcnamara; 08-01-2006 at 09:09 AM.
The type void in function parameter is to make clear the function has no arguments and the type void in function type is to make clear the function returns nothing.
In old C, a function could be declared as
func();
In this declaration the implicit return type is integer and we could say nothing about its parameters (it could be declared with parameters later in the code). To avoid such indetermination the void type was introduced.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.