cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

use of return

DS_10314450
6-Contributor

use of return

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

1 ACCEPTED SOLUTION

Accepted Solutions

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..

Werner_E_0-1681133403390.png

 

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

Werner_E_0-1681135113993.png

 

 

View solution in original post

4 REPLIES 4

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..

Werner_E_0-1681133403390.png

 

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

Werner_E_0-1681135113993.png

 

 

DS_10314450
6-Contributor
(To:Werner_E)

Agree on programming semantics.

 

Thank you.  

Top Tags