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 766725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T17:05:42+00:00 2026-05-14T17:05:42+00:00

The following code returns 14 as you’d expect: Block[{expr}, expr = 2 z; f[z_]

  • 0

The following code returns 14 as you’d expect:

Block[{expr},
  expr = 2 z;
  f[z_] = expr;
  f[7]]

But if you change that Block to a Module then it returns 2*z.
It seems to not matter what other variables besides expr you localize.
I thought I understood Module, Block, and With in Mathematica but I can’t explain the difference in behavior between Module and Block in this example.

Related resources:

  • Tutorial on Modularity and the Naming of Things from the Mathematica documentation
  • Excerpt from a book by Paul R. Wellin, Richard J. Gaylord, and Samuel N. Kamin
  • Explanation from Dave Withoff on the Mathematica newsgroup

PS: Thanks to Michael Pilat, Davorak, and Bill White for following the scent-trail on this weirdness.
Davorak clarifies and gets to the heart of the issue here:
Why would Mathematica break normal scoping rules in Module?

  • 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-14T17:05:43+00:00Added an answer on May 14, 2026 at 5:05 pm

    I too was a bit surprised by this, but I don’t think it’s a bug. If you look deep in the examples in the reference page for Module, under the section labeled Possible Issues, there’s a little note that says “Variables are renamed in nested scopes” and gives the following example:

    In[1]:= Module[{e = Expand[(1 + x)^5]}, Function[x, e]]
    
    Out[1]= Function[x$, e$1194]
    
    In[2]:= %[10]
    
    Out[2]= 1 + 5 x + 10 x^2 + 10 x^3 + 5 x^4 + x^5 
    

    Function is another scoping construct like Module, so x is renamed internally to x$ in the scope of the Function, similar to what you discovered with Trace about z.

    In your Module defining f, Set is another such scoping construct, and therefore z is renamed when f is defined inside of a Module, but not when it’s inside a Block. Following the advice of that example from the Module documentation, you can build the RHS of your function from its parts to avoid the lexical renaming of the nested scope:

    In[3]:= Clear[f, z]
    
    In[4]:= Module[{expr},
      expr = 2 z;
      Set @@ {f[z_], expr};
      f[7]]
    
    Out[4]= 14
    

    HTH!

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

Sidebar

Related Questions

I am using the following code: <?php $stock = $_GET[s]; //returns stock ticker symbol
Is the following possible in SQL Server 2000? CREATE FUNCTION getItemType (@code varchar(18)) RETURNS
The following code returns a couple of numbers, identifying people who take part in
Level: Beginner In the following code my 'samePoint' function returns False where i am
So I have the following code: return from a in DBContext.Acts join artist in
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
Consider the following code: def CalcSomething(a): if CalcSomething._cache.has_key(a): return CalcSomething._cache[a] CalcSomething._cache[a] = ReallyCalc(a) return
Working with the following code, I need to return only records where the `point'
Suppose I have the following code: class some_class{}; some_class some_function() { return some_class(); }
I have the following code which works fine. However, I only want to return

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.