Doubt regarding void function

Started by jakr13, March 12, 2012, 08:38:33 PM

Previous topic - Next topic

doubt regarding void funct

basic
0 (0%)
but help me
0 (0%)

Total Members Voted: 0

jakr13

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..?

dnaraG_1M



Try:


void func(void)
{
// do something
}

clues the compiler that there is no return value.....

cheers,
johnd

dnaraG_1M


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

CommonTater

#3
Quote from: dnaraG_1M on March 12, 2012, 10:33:42 PM

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.
   }


CommonTater

Quote from: dnaraG_1M on March 12, 2012, 10:25:48 PM
try:

Nope... you're still leaving the if() statement with nothing to evaluate.

Bitbeisser

Quote from: 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..?
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