|
Author |
Topic: function return and for()? (Read 264 times) |
|
lunetta
|
function return and for()?
« on: May 2nd, 2004, 2:18am » |
|
The most-boring-member-of-all-forum strikes again with another syntax question... regarding to functions with return() statements and for() - int test(){ int ret; for(int i=0;i<10;i++){ // check something here // after checking, I find a value: 5 ret = 5; } return (ret); } void setup(){ } void draw(){ } why this doesn't work? ? if I declare any initial value to ret, everything works fine. Is there a conceptual reason why it works like this? functions need a "safe value" return, despite of the fact that the return value anyway will be assigned anyway? again, this is not a problem I'm running into, but an "ideological doubt", so I'm sorry for bothering the board with these details... Thanks for all!!
|
« Last Edit: May 2nd, 2004, 2:30am by lunetta » |
|
|
|
|
arielm
|
Re: function return and for()?
« Reply #1 on: May 2nd, 2004, 2:34am » |
|
no you're not a programming abomination, your just pointing at one of the subtleties/headache-makers of java for your code to work, assigning a dummy value to "ret" should be enough, e.g: Code: otherwise, a friendly compiler like eclipse will tell you "the local variable ret may not have been initialized"... (btw, you don't need to put parenthesis when returning a value)
|
Ariel Malka | www.chronotext.org
|
|
|
lunetta
|
Re: function return and for()?
« Reply #2 on: May 2nd, 2004, 3:23am » |
|
Thanks again Ariel-Tylenol; this headache is gone
|
|
|
|
DW Tebriel Guest
|
Re: function return and for()?
« Reply #3 on: May 6th, 2004, 10:59pm » |
|
The only reason why you need to assign a value to ret at the beginning is that the compiler doesn't like it if there is potential for returning a null variable at the end. If the conditions allow for a possibility of not assigning a number in ret, then ret would return null and "crash." In these cases I usually assign -1 as the initial value. Just something to think about in future methods.
|
|
|
|
|