Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X
I have tried several speed tests, using return and not using return in a function. It is hard to detect any difference.
Is there any reason to use return for a function that does not have any conditional breaks, that is, executes top to bottom and exits at the bottom?
Regards, DS
Solved! Go to Solution.
The difference between writing the return value of a program with or without "return" is, that using return immediately exits the program. So when the return statement is just the last line in your program I won't expect any differences in behaviour whether "return" is explicitly used or just the value/variable you want to return. I could imagine that using "return" might very slightly slow down execution, but we can't say this for sure as we don't know how its implemented and as its used just once for each program run its sure hard to notice a difference in execution time.
Its sometimes said that using "return" is good programming habit, but ...
The screenshot may indicate that using "return" costs a very tiny bit of time, but I won't bother..
EDIT: Here is a second run which seconds what you had written about it being hard to decide if "return" really slows down the program execution
Hi,
If you do not use a return to explicitly define what is returned the function returns the last thing calculated at the end of the function.
Sometimes the last thing calculated is exactly what you want to return. In this case a return is not required.
Sorry, I was not entirely clear in question. Is there any speed advantage to using the keyword return versus just closing with the last line?
The difference between writing the return value of a program with or without "return" is, that using return immediately exits the program. So when the return statement is just the last line in your program I won't expect any differences in behaviour whether "return" is explicitly used or just the value/variable you want to return. I could imagine that using "return" might very slightly slow down execution, but we can't say this for sure as we don't know how its implemented and as its used just once for each program run its sure hard to notice a difference in execution time.
Its sometimes said that using "return" is good programming habit, but ...
The screenshot may indicate that using "return" costs a very tiny bit of time, but I won't bother..
EDIT: Here is a second run which seconds what you had written about it being hard to decide if "return" really slows down the program execution
Agree on programming semantics.
Thank you.