# Tagged Questions

Questions on Mathematica's scoping constructs (Block, Module, With, etc.)

139 views

### Strange behaviour of InternalInheritedBlock

I was going to post an answer for fast way to replace all zeros in the matrix. I was even quite happy because timings were the same order of magnitude as others. The idea was to overwrite ...
357 views

### Using With to scope over pure functions

Consider this example: given an option for a function func as "x" :> (a&), how can one assign different values to ...
262 views

### Namespaces in Mathematica

In many programming languages one can define a namespace with namespace abc How can the similar be done in Mathematica? The ...
427 views

### How to declare a function that uses local variables of a module outside this module?

I have the following code Module[{f,x=0}, f[t_] := x + t; ..........; ...f[1]...; ..........; ] My aim is to put the declaration of the function ...
100 views

### Creating a function after working out an example calculation

I have created this setup, it is giving me the correct answer, however I am interested in a function that does all this calculations and creates a output of mean and standard deviation. The function ...
133 views

### Module Inside ToBoxes or MakeBoxes

Does someone mind explaining why Module doesn't seem to work while inside ToBoxes or MakeBoxes. ...
167 views

I'm grading for a class using Mathematica right now, and one of the students ran into a weird issue. When he ran code like this: ...
198 views

### Creating a Block from a list of rules

How can I create a function which works like Block but takes a list of rules as its first argument, rather than a list of assignments? The function needs to be ...
599 views

### Variable scope similar to other languages

Mathematica, unlike many other languages, defines the variables in the global scope unless it is explicitly asked to define a variable in a local scope, e.g., using ...
580 views

### Compile recursive function modifying global variables

How to compile recursive formula when it relies on more than a few global variables (global to the topmost compiled function)? It is unreasonable to pass on all such variables to each recursive ...
126 views

### Arbitrary number of variables in Module, Block, etc

How do I specify an arbitrary number of variable names and initialize them inside of a Module or Block? For example, I want to do something like: ...
114 views

### Assign variables from list inside With or Module

I have found that it is possible to assign variables from lists, as follows. r = {1, 2, 3}; {a, b, c} = r; b 2 However, ...
82 views

### Scope of expression in a user-defined function

I have the following code: fbasic = A + R + Z*Z *Z*Z; f[A_, Z_, R_] := fbasic + R; f[0, 2, 3] The answer I am getting is ...
935 views

### What is the right way to construct DynamicModule

When I'm creating DynamicModules for CDF purposes I'm usualy basing on my experience with that, particular goal and content rather than on deep insight. I want to ...
313 views

### Call Functions From File Without Modifying Context (Sandbox)

Let's say I have file named test.m containing test[arg1_] := ( Print[arg1];) What is the best technique for calling test like ...
218 views

### Variable scope in nested NMaximize, NMaxValue

I'm trying to use NMaxValue inside other statements (FindRoot and NMinimize). Those statements should bind variable names, but I'm getting weird warnings which seem to indicate that the variables ...
140 views

### What are the scoping rules for function parameters shadowing System symbols?

Here are some very contrived code snippets, highly unlikely to appear in real code, but still I am curious why they behave like this: ...
127 views

### Are there any videos that show how different Mathematica expressions are evaluated?

Are there any videos that show how different Mathematica expressions are represented internally as object graphs and demonstrate how they are transformed during an evaluation? I am asking because I ...
158 views

### Is it possible to write a function immune to symbol redefinitions in any Block surrounding its invocation?

Normally, semantics of symbols used in a function definition can be changed by their redefinitions in Blocks surrounding the function invocation: ...
168 views

### Issue with scope of variables

For the definitions, t = 1; f[x_, y_] := t (x + y); v[3] := {1, 1}; g[u_] := f[v[u][[1]], v[u][[2]]]; the following two expressions yield different results, ...
141 views

### Is it possible to use the global definition of a symbol as part of its new local definition set in a Block?

Consider the following expression: Block[{Print = CopyToClipboard}, doSomething[]] Now all calls to Print from ...
234 views

### Effect of Block[] on iterators in Manipulate[]

The following is a toy example that hopefully shows the problem clearly enough. Why does the following code create a nice, manipulable output: ...
237 views

### FullDefinition and Module local variable

I have a question about FullDefinition and how it interacts with the local variables of Module. I've created a few functions ...
133 views

### Odd edge-case behavior of Block

In this example, Block is used to localize the variable cache as used in the function g when called from the function ...
254 views

### How can I set multiple values to local variables in a Module?

Multiple assignment is supported by Set. How can do something similar in Module expressions? If I could do it, whould there be ...
373 views

### What does InternalInheritedBlock do?

What does the function InternalInheritedBlock do? How is it different from the regular Block?
310 views

### Modify this code using Module and While

I have written a recursive function and would like to re-write the code using Module AND While to compare the timings. Here is my recursive function for f[n], where 6 n f[n] = f[n-1] + n! for n>0 and ...
263 views

### Variable scoping problem when mapping over delayed replacement

This is something that got me curious while I was playing around with Mathematica. Consider the following (contrived) example: ...
260 views

### Compile not correctly initializing a variable defined inside Module

Bug introduced in 8.0 and persisting through 10.3.1 or later In the following example, inside the minimumX's Module, ...
65 views

### Don't pollute outside context when solving equation from the outside

This code works fine only if a is not defined in the outside context: ...
100 views

### How to make a code line dynamic in a Module?

The code line If[StreamPosition>0,Read[str,Record]] should be executed in a Module at first. The Module consists of a Slider and a Pane. The Slider ...
576 views

### How do I avoid scoping collisions of iterator variables?

Yesterday I had an issue that kept for quite a while. My code wasn't producing the right results. Now I know why and I am wondering how to avoid the issue in future. Consider the following code. ...
773 views

### Defining a new Function using RegionPlot

I'm trying to make images like this: to illustrate 2D integrals to a calculus class. I used the code ...
126 views

...
170 views

### Possible to scope an anonymous function within Module?

This is my first question. I'm fairly new to Mathematica and completely new to SE, so I apologize if this question is trivial. I'm working on a problem that requires a function to be redefined ...
902 views

### Enforcing correct variable bindings and avoiding renamings for conflicting variables in nested scoping constructs

Using global variables the following turns an "expression" into a Function: expr = 2 x; Function[x, Evaluate[expr]] Of course ...
3k views

### Module with “local functions”

I am trying to use a Module having "local functions", i.e., those which I need to define only inside this module. So I tried this: ...
167 views

### Downvalues vs. Scoping for Functions

Regarding my recent question on using a default value for a function argument when a pattern was not met yielded some interesting answers, but the general consensus was "Yes this can be done, but ...
400 views

### Why does the syntax highlighter color a local symbol red inside DynamicModule?

I am adding a Module inside DynamicModule here is an example (thanks to @rm-rf for this simple example) ...
414 views

### Is the sharing of variables in Module/Block within Compile documented behavior?

Today I noticed something, I think for the first time. When used inside Compile variable values within Module (and ...
571 views

### How to set Block local variables by code?

I need to create a user-defined Block function where the Block variables' values are defined by code. For example, imagine I ...
226 views

### “With” as part of the “Dynamic Interactivity Language”

I noticed that in Mathematica's Function Navigator With is listed as being part of the "Dynamic Interactivity Language". It is not the first (or even the fourth, ...
204 views

### How to define UpValues in a different context from within a package

I'm trying to create a function (in a package) to import a lot of data and create a symbol with UpValues to return specific results about the data. I've got it working fine in a regular notebook, but ...
68 views

### What's the best practice for nested local constants? [duplicate]

Possible Duplicate: How to avoid nested With[]? I have many situations where I have a constant that is local to a function, with other constants that are computed from it. I see that I can ...
355 views

### Localizing variables within a Manipulate

I have a notebook with several examples of very similar computations, each involving the same variable/parameter names of the ingredients used in a final ...
363 views

### Modules that initialize themselves on first call

I use a lot of functions that extract a specific data item from a file with many data items. I want these functions to load data (slow) and return the item (fast) on first call, but just return the ...
1k views

### What are some advanced uses for Block?

I read the answers to this question (What are the use cases for different scoping constructs?) and this one (Condition, Block, Module - which way is the most memory and computationally efficient?). ...
313 views

### Comparing LetL and Module efficiency

I was recently introduced to the LetL macro thanks to Leonid's answer to one of my prior questions. I was, needless to say, impressed by the simplicity of its ...