FUNCTION FAILS TO RETURN VALUE
Rich Abel
Posts: 104
I've spent the last several hours trying to get a function I've written to work. The function is defined to return an integer to the calling code. It also is recursive, and my first attempt at recursion. I focused most of my troubleshooting on the recursive nature, but now after reading posts on the behavior of Local_Vars vs. Static_Vars, I've concluded it's a variable scope issue.
Here's a outline of the original function and a button_event that called it. This approach failed in that the return value always turned out to be 0 so that nLOC_ZNE was 0.
I tried changing local_var's to stack_vars among other things, but never got the function to return the correct (and non-zero, in this case) value.
I did get it work by creating a global variable and having the function act on that variable, as in the following:
This defeats one of the reasons to use a function.
Maybe some sharp eyes and more experienced ones can help me understand why the return approach failed.
Thanks
Rich Abel
Cello Technologies
Here's a outline of the original function and a button_event that called it. This approach failed in that the return value always turned out to be 0 so that nLOC_ZNE was 0.
DEFINE_FUNCTION INTEGER fnDO_THINGS(INTEGER nNUM) { STACK_VAR INTEGER nRET LOCAL_VAR INTEGER nREC SWITCH(nNUM) { CASE 1: CASE 2: CASE 3: { RETURN nNUM } CASE 21: CASE 22: CASE 23: { nREC = nNUM -20 fnDO_THINGS(nREC) } CASE 41: CASE 42: CASE 51: CASE 52: { RETURN nNUM } } } BUTTON_EVENT[dvTP,nSRC_BTNS] { PUSH: { LOCAL_VAR INTEGER nINDX LOCAL_VAR INTEGER nLOC_ZNE nINDX = GET_LAST(nSRC_BTNS) nLOC_ZNE = fnDO_THINGS(nINDX) } }
I tried changing local_var's to stack_vars among other things, but never got the function to return the correct (and non-zero, in this case) value.
I did get it work by creating a global variable and having the function act on that variable, as in the following:
DEFINE_VARIABLE INTEGER nNUMBER DEFINE_FUNCTION INTEGER fnDO_THINGS(INTEGER nNUM) { STACK_VAR INTEGER nRET LOCAL_VAR INTEGER nREC SWITCH(nNUM) { CASE 1: CASE 2: CASE 3: { nNUMBER = nNUM } CASE 21: CASE 22: CASE 23: { nREC = nNUM -20 fnDO_THINGS(nREC) } CASE 41: CASE 42: CASE 51: CASE 52: { nNUMBER = nNUM } } } BUTTON_EVENT[dvTP,nSRC_BTNS] { PUSH: { LOCAL_VAR INTEGER nINDX LOCAL_VAR INTEGER nLOC_ZNE nINDX = GET_LAST(nSRC_BTNS) fnDO_THINGS(nINDX) nLOC_ZNE = nNUMBER } }So in the above, the function modifies the global variable nNUMBER rather than returning the value.
This defeats one of the reasons to use a function.
Maybe some sharp eyes and more experienced ones can help me understand why the return approach failed.
Thanks
Rich Abel
Cello Technologies
0
Comments
I'm not sure that will do what you want, but it may solve your problem of not getting a return value.
Jeff
Note brackets.
Jeff nailed it.
I guess I niavely thought that the return value would 'find its way' back to the calling code.
It did cause me to read the various threads on scope of variables, stack vs. local variables, etc, which will cause me to pay more attention to something I hadn't really given much thought to in the past.
As always, thanks. I'd be lost without information and assistance from the forum....
Brackets aren't necessary for a case. Technically, the BREAKs aren't either, but they are needed in other languages to keep from dropping down into the next group of cases, so I use them anyway.
I guess ya learn something new everyday!