-7

I was interview in company and I was asked a question, question was bit strange so wanted to ask with expert guys.

The question suppose I have function which returns bool type. Let us say this :

public bool func(int param)
{
  bool retVal;

 // here is some algorithm which as a result either set the retVal to false or true, 
 // It doesn't matter what is algo, the only thing important is it either do 
 // retVal=false or retVal=true

 // The question is i have to write the algo here which in case 
 // if the previous algo gives us retVal=false then it should 
 // return true and if retVal=true then return false      

}

What should be that algo ?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 7
    That function you show, that's not C. Please use the *correct* language tag. – Some programmer dude Feb 02 '16 at 06:53
  • A bit odd that a `C` interview (as you tagged it) would ask about `bool` (and not `_Bool`, see for example [Is bool a native C type?](http://stackoverflow.com/questions/1608318/is-bool-a-native-c-type)). See also @JoachimPileborg's comment above. Was it `C++` maybe? It's not the same thing, and some interviewers pay attention to such confusion. – dxiv Feb 02 '16 at 07:01
  • TIL: `!` is an *algorithm*. – j_random_hacker Feb 02 '16 at 07:20
  • 4
    If you don't know this, applying for jobs is a waste of everybody's time. You need an introductory programming course. – molbdnilo Feb 02 '16 at 07:24
  • @dxiv Nothing odd about it, `bool` is far more commonly used than `_Bool`. The use of `public` is mighty odd, though. – Lundin Feb 02 '16 at 07:42

3 Answers3

0

I think your code would be like this:

public bool func(int param)
{
   bool retVal;
   return !retVal;
}
ameyCU
  • 16,489
  • 2
  • 26
  • 41
ranjanm28
  • 89
  • 1
  • 7
0

!retVal is the opposite of retVal. if retVal is true the !retval is false and vice-versa

public bool func(int param)
{
 bool retVal
 //your algo;
 return !retVal;

}
Meer
  • 2,765
  • 2
  • 19
  • 28
-1

If the result of the algorithm is in retval,

return (!retval);
Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
  • 2
    Why the parenthesis? It adds nothing but clutter. – Lundin Feb 02 '16 at 07:43
  • Adding a bracket adds clarity in many cases. The return bracket is just a matter of convention. – Rishikesh Raje Feb 02 '16 at 08:54
  • 1
    It rather suggests that the programmer isn't aware of how the return statement works nor how sequence points work. – Lundin Feb 02 '16 at 08:58
  • As I said, it is a matter of convention. Please clarify your above statement. – Rishikesh Raje Feb 02 '16 at 09:10
  • What convention? You have absolutely no rationale for putting the parenthesis there, is my whole point. The C standard grammar is (6.8.6) `return expression_opt ;` where the expression is any plain C expression. So if you are consistent with your own "convention", you should write every single expression in your code inside a parenthesis. For example: `for((i=0); (i – Lundin Feb 02 '16 at 10:56
  • 1
    If you refer to MISRA C 2004 Section 10.2 in the example given it mentions `return (s32a);` Further below, in the same section it also mentions `return s16a;`. So that is why I say, it is a matter of convention. Both are perfectly accepted ways of writing C code. Writing code one way does not in any way give any other inferences on one's C ability. – Rishikesh Raje Feb 02 '16 at 11:14
  • If they randomly alternative between styles, that's even worse. It can only mean that the person who wrote the code is confused, period. – Lundin Feb 02 '16 at 12:03
  • The MISRA document would have taken input from multiple people. So there are multiple conventions or styles in evidence. Again goes to show that both are accepted forms. – Rishikesh Raje Feb 02 '16 at 12:43
  • 1
    MISRA-C is a bad example since it is explicitly not a style guide. I was one of those multiple people giving input: the coding style used in the examples is not relevant to remark upon during review, since MISRA-C only requires that there exists a style guide, but makes no demands on what it should contain. – Lundin Feb 02 '16 at 12:56
  • I agree that MISRA is not a style guide. However the C language does not have a definitive style guide. It just goes to show that there are multiple accepted forms. – Rishikesh Raje Feb 04 '16 at 05:39