Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8336027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:51:37+00:00 2026-06-09T03:51:37+00:00

A while back I got rebuked by Simon Urbanek from the R core team

  • 0

A while back I got rebuked by Simon Urbanek from the R core team (I believe) for recommending a user to explicitly calling return at the end of a function (his comment was deleted though):

foo = function() {
  return(value)
}

instead he recommended:

foo = function() {
  value
}

Probably in a situation like this it is required:

foo = function() {
 if(a) {
   return(a)
 } else {
   return(b)
 }
}

His comment shed some light on why not calling return unless strictly needed is a good thing, but this was deleted.

My question is: Why is not calling return faster or better, and thus preferable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:51:39+00:00Added an answer on June 9, 2026 at 3:51 am

    Question was: Why is not (explicitly) calling return faster or better, and thus preferable?

    There is no statement in R documentation making such an assumption.
    The main page ?’function’ says:

    function( arglist ) expr
    return(value)
    

    Is it faster without calling return?

    Both function() and return() are primitive functions and the function() itself returns last evaluated value even without including return() function.

    Calling return() as .Primitive('return') with that last value as an argument will do the same job but needs one call more. So that this (often) unnecessary .Primitive('return') call can draw additional resources.
    Simple measurement however shows that the resulting difference is very small and thus can not be the reason for not using explicit return. The following plot is created from data selected this way:

    bench_nor2 <- function(x,repeats) { system.time(rep(
    # without explicit return
    (function(x) vector(length=x,mode="numeric"))(x)
    ,repeats)) }
    
    bench_ret2 <- function(x,repeats) { system.time(rep(
    # with explicit return
    (function(x) return(vector(length=x,mode="numeric")))(x)
    ,repeats)) }
    
    maxlen <- 1000
    reps <- 10000
    along <- seq(from=1,to=maxlen,by=5)
    ret <- sapply(along,FUN=bench_ret2,repeats=reps)
    nor <- sapply(along,FUN=bench_nor2,repeats=reps)
    res <- data.frame(N=along,ELAPSED_RET=ret["elapsed",],ELAPSED_NOR=nor["elapsed",])
    
    # res object is then visualized
    # R version 2.15
    

    Function elapsed time comparison

    The picture above may slightly difffer on your platform.
    Based on measured data, the size of returned object is not causing any difference, the number of repeats (even if scaled up) makes just a very small difference, which in real word with real data and real algorithm could not be counted or make your script run faster.

    Is it better without calling return?

    Return is good tool for clearly designing “leaves” of code where the routine should end, jump out of the function and return value.

    # here without calling .Primitive('return')
    > (function() {10;20;30;40})()
    [1] 40
    # here with .Primitive('return')
    > (function() {10;20;30;40;return(40)})()
    [1] 40
    # here return terminates flow
    > (function() {10;20;return();30;40})()
    NULL
    > (function() {10;20;return(25);30;40})()
    [1] 25
    > 
    

    It depends on strategy and programming style of the programmer what style he use, he can use no return() as it is not required.

    R core programmers uses both approaches ie. with and without explicit return() as it is possible to find in sources of ‘base’ functions.

    Many times only return() is used (no argument) returning NULL in cases to conditially stop the function.

    It is not clear if it is better or not as standard user or analyst using R can not see the real difference.

    My opinion is that the question should be: Is there any danger in using explicit return coming from R implementation?

    Or, maybe better, user writing function code should always ask: What is the effect in not using explicit return (or placing object to be returned as last leaf of code branch) in the function code?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got into a discussion a while back. The company I work at develop
A while back, I got a LEGO Mindstorms NXT set for Christmas, and now
I previously asked a similar question on this topic a while back and got
A while back I asked link text and I got some great answers. What
I was given this code a while back. I finally got around to testing
Hello every one, While taking back up of the database, i got the above
I got a strange problem while used luabind to return a stl::vector::iterator to lua
I'm learning queues from a book. I've got into a problem while learning circular
A while back, I remember using a piece of code (4-5 lines), added to
A while back i read somewhere about how to improve upon the MVC pattern

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.