# Global and local variables

**URL:** <https://community.wolfram.com/t/global-and-local-variables/1976>\
**Category:** Questions\
**Tags:** mathematica, symbolic-computations\
**Created:** [April 14, 2014, 10:52pm UTC](https://community.wolfram.com/t/global-and-local-variables/1976 "2014-04-14T22:52:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casperyc](https://avatars.discourse-cdn.com/v4/letter/c/5f8ce5/32.png) [@casperyc](https://community.wolfram.com/u/casperyc)\
**Post date:** [April 14, 2014, 10:52pm UTC](https://community.wolfram.com/t/global-and-local-variables/1976/1 "2014-04-14T22:52:31Z")

</div>

```auto
(* Capture Recapture History *)

myCHcc[K_] := Table[IntegerDigits[n, 2, K], {n, 1, 2^K - 1}]

(* Capture Recapture Probability *)

myindprobCC[CH_]:=Module[{K,ind,fi,li,ans},

	(* Get $K$ *)
	K = Length[CH];
	
	(* First and Last Capture times *)
	fi = First[Position[CH, 1]][[1]];
	li = Last[Position[CH, 1]][[1]];
	
	(* Get probabilities *)
	
	ans =
  Sum[
   Product[ 
    Subscript[\[Phi],j]
    *
    Subscript[p,j+1]^CH[[j+1]]
    *
    (1-Subscript[p,j+1])^(1-CH[[j+1]]),
    {j,fi,d-1}
   ]*(1-Subscript[\[Phi],d]),
   {d, li, K}
  ];
	
	ans /. Subscript[\[Phi], K]-> 0
]

K = 3
xij = myCHcc[K]
ans = Map[myindprobCC, xij]

```

Runs perfectly.

There variables involved are

```auto
pars =Variables[ans]

```

However, say by accident, I have done

```auto
Subscript[p, 3] = "Dummy"

```

BEFORE, I define my function “myindprobCC”, then we have a problem.  
Is there a way to “automatically” put ALL “possible” variables, to the Module as local variables, like what i did for “{K,ind,fi,li,ans}”.

The problem is that, with the input arguments K\_ and C\_, the variables vary.  
And this is just a simple example.

One way I can think of is to use Table[] to creat them and declare as local variables by hand, but is there a simpler way?

Thanks

---

<div class="post-metadata">

**Author:** ![brucem](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/brucem/32/26319_2.png) [@brucem](https://community.wolfram.com/u/brucem)\
**Post date:** [April 15, 2014, 5:30pm UTC](https://community.wolfram.com/t/global-and-local-variables/1976/2 "2014-04-15T17:30:25Z")

</div>

Making a “subscripted variable” a local symbol in a Module also has difficulties.

```auto
In[1]:= Module[{Subscript[a, 1]},                                                       
         Subscript[a, 1] = 1;                                                           
         Print[Subscript[a, 1]]                                                         
         ]                                                                              

Module::lvsym: Local variable specification {a } contains a , 
                                              1 1
     which is not a symbol or an assignment to a symbol.

Out[1]= Module[{a }, a = 1; Print[a]]
                 1 1 1

```

This is because Subscript[a, 1] is a function call, not a Mathematica symbol.

The attached notebook has suggestions for making subscripted objects robust enough for executable code.  
(If you can postpone subscripts until you are making static output for presentation, that might save some work.)
