1. Hard to find bug in my app
- Posted by justaguy Nov 08, 2009
- 964 views
Short version:
Function calls don't actually *require* you to receive the returned value. Is this expected and intended behavior? It resulted in a hard to find logic error for this newbie.
- long boring version with background info and example code
I'm a casual programmer, decided I was going to relearn Eu, and was excited to find OpenEu was moving to 4.0. So I installed it, and downloaded a few things I thought looked interesting, one of which includes Raymond Smith's EuAllegro wrappers.
Was playing with one of his examples, decided to comment it to figure out how it worked. While doing that, I also decided to change some stuff that I thought should be organized differently. One of them was a collision check outside of the collision check function call. He had 3 collision checks, one for the left, one for the right, and one for straight ahead. The one for straight ahead was out of the collision check function.
So I moved the straight ahead collision check to the proper function, and replaced his old code with a call to the function. But the app no longer registered those collisions, although it registered the left and right collisions.
Tried to track the error down over a couple of days. Last night finally figured it out. I was returning a value to the function, but my call to that function did not have a variable to receive the returned value. For example:
function colcheck() fin = TRUE end function
... some stuff colcheck()
if fin = TRUE then exit() end if
2. Re: Hard to find bug in my app
- Posted by jimcbrown (admin) Nov 08, 2009
- 942 views
Short version:
Function calls don't actually *require* you to receive the returned value. Is this expected and intended behavior? It resulted in a hard to find logic error for this newbie.
<snip>
Tried to track the error down over a couple of days. Last night finally figured it out. I was returning a value to the function, but my call to that function did not have a variable to receive the returned value. For example:
function colcheck() fin = TRUE end function ... some stuff colcheck() if fin = TRUE then exit() end if
Your example code contradicts your statement. In your example code, colcheck() does not return a value from the function. That should not work and if it ran, that is a bug.
However, functions can return values but the caller is not required to retrieve the value returned by the function. It can ignore the return value.
3. Re: Hard to find bug in my app
- Posted by justaguy Nov 10, 2009
- 877 views
function colcheck() fin = TRUE end function ... some stuff colcheck() if fin = TRUE then exit() end if
Your example code contradicts your statement. In your example code, colcheck() does not return a value from the function. That should not work and if it ran, that is a bug.
However, functions can return values but the caller is not required to retrieve the value returned by the function. It can ignore the return value.
[/quote]
Yes, sorry, I got interrupted while I was typing it in.
I tried
function colcheck() fin = TRUE end function ... some stuff colcheck() if fin = TRUE then exit() end if
and the interpreter gave me an error.
when I changed
fin = TRUE
to
fin = TRUE return fin
The interpreter was happy.
I suspected nothing required you to actually assign the return value, but wasn't sure.
Thanks.
4. Re: Hard to find bug in my app
- Posted by DanM Nov 10, 2009
- 887 views
Yes, sorry, I got interrupted while I was typing it in.
I tried
function colcheck() fin = TRUE end function ... some stuff colcheck() if fin = TRUE then exit() end if
and the interpreter gave me an error.
when I changed
fin = TRUE
to
fin = TRUE return fin
The interpreter was happy.
I suspected nothing required you to actually assign the return value, but wasn't sure.
Thanks.
It looks like your fin var is NOT a local var to the function? Probably not a good idea, but if so, you wouldn't really have to return IT, just NEED (always!) to have SOME "return" statement in every function; so, in your example, "return 0" would have worked too, I think, since the var, after being assigned a value in the function, could be seen outside the function whether returned or not.
DanM