void as a function parameter
#1
Posted 27 May 2006 - 09:34 AM
int myFunc() {...}
and
int myFunc(void) {...}
?
I've always assumed that there is no difference but now I'm wondering why people would even bother using the second one.
Thanks in advance
#2
Posted 27 May 2006 - 10:05 AM
#3
Posted 27 May 2006 - 10:32 AM
#4
Posted 27 May 2006 - 10:33 AM
#5
Posted 27 May 2006 - 10:38 AM
poita said:
Because it looks complicated ...
In short: don't bother.
Bramz' warehouse | LiAR isn't a raytracer
#6
Posted 27 May 2006 - 10:51 AM
#7
Posted 27 May 2006 - 11:55 AM
Ok this requires some illustration. You can't write code like this:
void function(int x, void y, int z)
{
// ...
}
It serves no purpose whatsoever and just like you'd have to remove 'void y' from the above example, it's best not to write void for a completely empty parameter list.Unfortunately C++ isn't consistent with that for functions. You can't omit the void in front of the function in the example above. Yet it would be completely unambiguous and safe as long as the compiler would enforce that you can't return any value from such function. Note by the way that we don't write 'return void' either.
#8
Posted 27 May 2006 - 01:28 PM
Nick said:
In fact it is ambiguous.
int main()
{
bla(); // a function declaration or a function call expression?
}
Quote
void foo();
void bar()
{
return (void)23; // ok
return foo(); // ok
}
-
Currently working on: the 3D engine for Tomb Raider.
#9
Posted 27 May 2006 - 01:43 PM
.oisyn said:
In fact it is ambiguous.
int main()
{
bla(); // a function declaration or a function call expression?
}So what's the difference between specifying no arguments and having unspecified arguments. (I'm using C++ btw)
Also, I don't see any ambiguity there. As far as I'm aware, you can't declare a function inside another function so it's definitely a call.
.oisyn said:
void foo();
void bar()
{
return (void)23; // ok
return foo(); // ok
}What does casting to a void do? :blink:
#10
Posted 27 May 2006 - 01:46 PM
.oisyn said:
Quote
int main()
{
bla(); // a function declaration or a function call expression?
}What's this useful for anyway?
#11
Posted 27 May 2006 - 02:54 PM
poita said:
It's an old remnant in C, back in the days where people didn't care about safety
// function that has no params
void foo(void);
// function that may have params, but I don't know what they are at this point
void bar();
// function that has no params
void baz()
{
}
int main()
{
foo(); // ok, foo has no params
foo(34); // error
bar(); // ok, don't know what the params are so allow everything
bar(1, 2, 3); // ok
baz(); // ok, same as foo
baz(34); // error
}
In a sourcefile, you are allowed to define bar with a parameter list:
void foo(void);
void bar();
void foo(int i) { } // error, different signature
void bar(int i) { } // ok, signature was unspecified earlier
Quote
class MyType
{
MyType();
};
int main()
{
MyType myVar(); // variable definition with default constructor initialization? Guess again!
}
myVar is a function returning a MyTypeQuote
-
Currently working on: the 3D engine for Tomb Raider.
#12
Posted 27 May 2006 - 02:58 PM
Nick said:
Quote
What's this useful for anyway?
namespace
{
int someIdentifier = 0;
}
/* lot of code that uses someIdentifier
...
*/
/* then, you decide to include a library in your program
that has a function called 'someIdentifier' you need to use */
void someFunc()
{
someIdentifier(23); // error, someIdentifier is an int.
void someIdentifier(int);
someIdentifier(23); // it's ok now
}
of course, if someIdentifier() is in a namespace you're screwed as you aren't allowed to declare namespaces inside functions as well (they need to fix that imo)
-
Currently working on: the 3D engine for Tomb Raider.
#13
Posted 27 May 2006 - 03:17 PM
.oisyn said:
It's an old remnant in C, back in the days where people didn't care about safety ;)
Ah, I actually remember reading something about that back in the days of learning the language.
.oisyn said:
class MyType
{
MyType();
};
int main()
{
MyType myVar(); // variable definition with default constructor initialization? Guess again!
}
myVar is a function returning a MyTypeWow, I've been missing out. Could come in handy.
.oisyn said:
Is there any practical reason you'd want to do that?
#14
Posted 27 May 2006 - 06:33 PM
I find it quite confusing though. I can't find a practicaly use for it that can't be done in a clean way otherwise. I believe that allowing to omit void for functions that don't return anything would be more useful and easier to understand. But if you have arguments why it's a good thing I'd love to hear them!
#15
Posted 27 May 2006 - 07:26 PM
.oisyn said:
Then you would just write
void someFunc()
{
someNamespace::someIdentifier(23); // it's ok now
}
or maybe
void someFunc()
{
using someNamespace::someIndentifier;
someIdentifier(23); // it's ok now
}
I'm not sure if the latter works though ...
Bramz' warehouse | LiAR isn't a raytracer
#16
Posted 27 May 2006 - 07:30 PM
// foo.c
void foo(a, b)
float a;
float b;
{
// do something with a and b
}
// bar.c
// we need foo, so declare it
void foo();
void bar()
{
double a, b; // oops, I forgot the function takes floats
a = 1.0;
b = 2.0;
foo(a, b); // this compiles and does NOT cast the params to floats
}
Be happy you have type safety that avoids this in C++. :wallbash:@Nick: I think .oisyn aready answered your question. To support removal of void return types you would have to disallow declaration of functions within functions.
#17
Posted 27 May 2006 - 07:34 PM
Nick said:
The problem is that a certain popular compiler thinks the return type is int if you do so =)
Bramz' warehouse | LiAR isn't a raytracer
#18
Posted 27 May 2006 - 07:39 PM
// someIdentifier.cpp
namespace someNamespace {
void someIdentifier(int) { ... }
}
// main.cpp
int main()
{
someNamespace::someIdentifier(23); // this won't compile
return 0;
}
#19
Posted 27 May 2006 - 07:42 PM
monjardin said:
Does anyone know how C# handles this? I'm too lazy to check myself... :p
#20
Posted 27 May 2006 - 07:50 PM
It says:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
I don't know what C# does either and I don't have it on my laptop. :(
I'm too cheap to pay for Internet access at home when I can get it for free with a nice cup of coffee. :yes:
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users


This topic is locked









