uint64_t build_table ( int32_t myparameter ) { if ( myparameter == 7 ) { return BOOL_TRUE; } else if (myparameter == 29 ) { return BOOL_FALSE; } else { return 23; } return 5; }
Yes, your conversion error is that you have failed to convey what issue you think you are having.
You aren't telling us what is happening. You aren't telling us what you expected should happen. You aren't telling us what input parameters you use. You aren't even letting us see all constants in use in the posted code. Most interesting of all - what condition is expected to return the value 5?
BOOL_TRUE is 1 BOOL_FALSE is 0
when i send 7 i get BOOL_TRUE back when i send 29 i get BOOL_FALSE back when i send 1 or 2 or 3 or 4 or 5 or 6 or 8 or 9 or 10 i get 23 back but when i send "0" i don't get 5 back when i send 11 or 12 or 13 or 14 or 15 or 16 or 17 or 18 i get 23 back
So why should you get 5 back when you send in 0?
Maybe you could explain exactly how you think your program can print the value 5?
uint64_t build_table ( int32_t myparameter ) { if (myparameter == 7) { return BOOL_TRUE; } else if (myparameter == 29) { return BOOL_FALSE; } else { return 23; <== exactly why shouldn't input 0 reach this line? } return 5; <== exactly how do you expect your program to reach this line? }
Exactly why do you think 0 is magical and should ignore the "return 23" statement?
Kalim, You must analyze your statements line by line and must understand the scope of if...else statements.
its too very difficult i try all yesterday but it does not work now.
plz someone give me the code.
If you want a special return value for input value 0, then you need a conditional that matches input value 0.
Right now, you have two (!) different wild-card statements that catch any value not already explicitly tested. Obviously, the first of the two wild-card statements will be the winning one, since the processor will leave the function as soon as it reaches a "return" statement. So having one more "wild card" return statement at the end of the function is irrelevant - the processor can never ever reach that statement.
This should be 100% obvious if you do look at the code I did post. Anything not matching the first "if" will try to match the second "if". And anything that doesn't match that "if" either will then enter the "else" and return.
Most compilers would warn that the last "return" is unreachable, if you just remember to configure your project to compile with all warnings enabled.
now i told to only return BOOL_TRUE or BOOL_FALSE but i need to also return 27, 23 and 5. can i return more values if i make it a class?
uint64_t build_table ( int32_t myparameter ) { if ( myparameter == 7 ) { return BOOL_TRUE; } else if (myparameter == 29 ) { return BOOL_FALSE; } else { return 23; } else if (myparameter != "0") { return 27; } else { return 5; } }
also can you tell me what is different with pass by value and pass by reference cuz i now hear i must return value
and what is difference with argument and parameter
You can return more values if you simply code it properly. If the if/then/else logic escapes you consider a switch/case implementation.
At the moment you pass in a value, and return a value, not sure this situations calls for anything more complex until you grasp the concepts more thoroughly.
Do you understand pointers?
Please find a copy of the K&R C manual and read it.
One if can only have one else.
And anything that doesn't match the if will (!) enter the else.
Your code have an extra else that isn't matched by a previous if.
And you then suddenly compare your int-valued parameter with the address the compiler/linker used to store the text string "0".
A good programming book and some quality time reading it is the best investment - you will just waste time doing trial-by-error programming.
i will try more code but it is very difficult
will you tell me what is the same as parameter and argument?
Why do you want to try more code?
Isn't it better to understand the code instead of doing wild experiments?
This is your last code with extra braces added to show what the compiler will do with the code.
if (myparameter == 7) { return BOOL_TRUE; } else { if (myparameter == 29) { return BOOL_FALSE; } else { return 23; } } else { <=== this else doesn't belong to any if!!! // "0" isn't a number but an address to a text string... if (myparameter != "0") { return 27; } else { return 5; } }
Read books and learn to understand code. Don't guess.
Greeting Kalim Rohgash
Did you get your code fixed. I have a same problem and do not know what to fix.