Pelles C forum
C language => Beginner questions => Topic started by: jakr13 on March 12, 2012, 08:38:33 PM
-
i am declaring a function with void and have to call the function inside main loop and check the function as below:
void func()
{
// do something
}
int main()
{
func();
if(func()) // error here
{
//do something
}
}
but i am getting error like
error: void value not ignored as it ought to be
can anyone tell me what i made mistake..?
-
Try:
void func(void)
{
// do something
}
clues the compiler that there is no return value.....
cheers,
johnd
-
Forgot to say you'll still get an error trying to use
a return value which is void.......not returned....
Need to return something for the "if" to test or
not try to use a return value.
The previous line - which used no return value - gave
no error.......
Don't know much about this particular compiler (still learning), but all
others that I've used would barf on trying to use a return
value that was not there.
cheers,
johnd
-
Forgot to say you'll still get an error trying to use
a return value which is void.......not returned....
Need to return something for the "if" to test or
not try to use a return value.
The previous line - which used no return value - gave
no error.......
Don't know much about this particular compiler (still learning), but all
others that I've used would barf on trying to use a return
value that was not there.
cheers,
johnd
An if() statement requires something it can resolve to a boolean TRUE (!0) or FALSE (0)... If the expression evaluates to TRUE, the part in the braces is executed. If it evaluates to false the braced part is not executed. If the function returns no value the if() statement is going to error off since it has nothing to evaluate.
The correct way is this...
int func( void )
{ int result;
// do something
return result; // true for success, false for failure
}
int main( void )
{
func(); // here you can ignore the returned value
if(func()) // now the if() has something to work on
{
//do something
}
return 0; // main returns 0 for succes or an error value.
}
-
try:
Nope... you're still leaving the if() statement with nothing to evaluate.
-
i am declaring a function with void and have to call the function inside main loop and check the function as below:
void func()
{
// do something
}
int main()
{
func();
if(func()) // error here
{
//do something
}
}
but i am getting error like
error: void value not ignored as it ought to be
can anyone tell me what i made mistake..?
Well, very simple, declaring the function as "void" if you expect to act on the result of this function...
This is one of the idiocies of the C programming language. "void foo ()" is pretty much what "procedure foo ()" would be for example in Pascal. Typcasting a function as "void" simply means that there is no result value of that function, it is just a subroutine that "does something". Period.
How do you think the if-statement is to be evaluated if you explicitly declare the function not to return a value? :-\
Ralf