Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-01-2006, 08:21 AM   #1
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
int * function(void)???

why does some functions have void declared as this:
int * function(void)


I do not get the point. Why not without the void?
Old 08-01-2006, 08:24 AM   #2
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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).
Old 08-01-2006, 08:33 AM   #3
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
sounds reasonable.
Old 08-01-2006, 08:41 AM   #4
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Heres some info on the subject from The C programming language K&R
...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.
Hmm so I think I was sort of correct.

Last edited by dmail; 08-01-2006 at 08:42 AM.
Old 08-01-2006, 09:07 AM   #5
jim mcnamara
Registered: May 2002
Posts: 964

Rep: Reputation: 36
int foo()......
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.:
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;
	return 0;

Last edited by jim mcnamara; 08-01-2006 at 09:09 AM.
Old 08-01-2006, 09:37 AM   #6
Senior Member
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,402
Blog Entries: 1

Rep: Reputation: 64
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


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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between function declaration: func() and func(void) koyi Programming 4 11-19-2005 10:19 AM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 09:34 PM
Hash-Function (string to int) Hady Programming 5 04-05-2004 01:53 AM
void * pointer in function xailer Programming 23 01-16-2004 02:14 PM
void main(void) linuxanswer Programming 4 10-26-2003 12:37 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:52 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration