LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

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


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

}

I do not get the point. Why not without the void?
 
Old 08-01-2006, 08:24 AM   #2
dmail
Member
 
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
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 547

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

Rep: Reputation: Disabled
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.
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
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Code:
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.:
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.
 
Old 08-01-2006, 09:37 AM   #6
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,393
Blog Entries: 1

Rep: Reputation: 63
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.
 
  


Reply


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


All times are GMT -5. The time now is 09:09 AM.

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