NO

Poll

doubt regarding void funct

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

Total Members Voted: 0

Author Topic: Doubt regarding void function  (Read 4226 times)

jakr13

  • Guest
Doubt regarding void function
« 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..?

dnaraG_1M

  • Guest
Re: Doubt regarding void function
« Reply #1 on: March 12, 2012, 10:25:48 PM »


Try:


void func(void)
{
// do something
}

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

cheers,
johnd

dnaraG_1M

  • Guest
Re: Doubt regarding void function
« Reply #2 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

CommonTater

  • Guest
Re: Doubt regarding void function
« Reply #3 on: March 13, 2012, 04:55:21 AM »

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...
 
Code: [Select]

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.
   }
« Last Edit: March 13, 2012, 05:00:35 AM by CommonTater »

CommonTater

  • Guest
Re: Doubt regarding void function
« Reply #4 on: March 13, 2012, 04:56:57 AM »
try:

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

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Doubt regarding void function
« Reply #5 on: March 13, 2012, 08:26:27 AM »
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