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

  • Home
  • SEARCH
  • 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 6134811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:24:09+00:00 2026-05-23T17:24:09+00:00

Under what circumstances does the following example return a local x versus a global

  • 0

Under what circumstances does the following example return a local x versus a global x?

The xi’an blog wrote the following at http://xianblog.wordpress.com/2010/09/13/simply-start-over-and-build-something-better/

One of the worst problems is scoping. Consider the following little gem.

f =function() {
if (runif(1) > .5)
x = 10
x
}

The x being returned by this function is randomly local or global. There are other examples where variables alternate between local and non-local throughout the body of a function. No sensible language would allow this. It’s ugly and it makes optimisation really difficult. This isn’t the only problem, even weirder things happen because of interactions between scoping and lazy evaluation.

PS – Is this xi’an blog post written by Ross Ihaka?

Edit – Follow up question.

Is this the remedy?

f = function() {
  x = NA
  if (runif(1) > .5)
    x = 10
  x
}
  • 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-05-23T17:24:10+00:00Added an answer on May 23, 2026 at 5:24 pm

    The ‘x’ is only declared in the function if the ‘if’ condition is true, so if ‘runif(1)>.5’ then the second mentioning of the x will make the function return your local x (10), otherwise it will return a globally defined ‘x’ (and if ‘x’ is not defined globally then it will fail)

        > f =function() {
        + if (T)
        + x = 10
        + x
        + }
        > f()
        [1] 10
    
        > f =function() {
        + if (F)
        + x = 10
        + x
        + }
        > f()
        Error in f() : Object 'x' not found
    
        > x<-77
        > f()
        [1] 77
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Under what circumstances - if any - does adding programmers to a team actually
In .NET, under which circumstances should I use GC.SuppressFinalize() ? What advantage(s) does using
Under what circumstances is an enum more appropriate than, for example, a Collection that
What does the expression Fail Early mean, and under what circumstances is this approach
Under what circumstances might you want to use multiple indirection (that is, a chain
Under what circumstances would this or would this not be safe? I have a
Under what circumstances should I expect memcpys to outperform assignments on modern INTEL/AMD hardware?
Under normal circumstances, a VB.NET application of mine can check the ClientName environmental variable
Under what circumstances would you use MicrosoftAjax over jQuery in an ASP.NET MVC application?
Under normal circumstances you cannot move a workflow designed in SharePoint Designer to another

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.