# Persist a module variable value between two calls to a function?

**URL:** <https://community.wolfram.com/t/persist-a-module-variable-value-between-two-calls-to-a-function/13036>\
**Category:** Questions\
**Tags:** graphics-and-visualization\
**Created:** [August 30, 2018, 11:37pm UTC](https://community.wolfram.com/t/persist-a-module-variable-value-between-two-calls-to-a-function/13036 "2018-08-30T23:37:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vemparalan](https://avatars.discourse-cdn.com/v4/letter/v/7ea924/32.png) [@vemparalan](https://community.wolfram.com/u/vemparalan)\
**Post date:** [August 30, 2018, 11:37pm UTC](https://community.wolfram.com/t/persist-a-module-variable-value-between-two-calls-to-a-function/13036/1 "2018-08-30T23:37:35Z")

</div>

Hi  
I have just started using Mathematica,  
I am trying to plot a bar graph with two data sets, resulting graph has two bar graphs with one appearing on top of the other.

We have a custom Mathematica package to do this job.  
which has a function, with a module variable say (x)  
this function gets called twice for the two data sets, and each time (x) is getting set to a different value by some calculation in this function.  
is it possible to retain the value of (x) which gets set first time until it gets set to a different value second time

say in first time call x =1, I would like to some how persist this value of x when I call this function second time, so I can use it then

I have been going through Mathematica Language documentation online, and came to know about  
PERSISTVALUE, which I understand is like a session variable, is this something I can use,

I did try to use PERSISTVALUE in our Mathematica code but I am unable to get it working,

when I first set PersistValue[‘myvar’] = 1, and then try to read it back in the same call it doesn’t show the value,  
with this problem I cannot even expect it to be retained until the next call.

Firstly can you let me know if I can use PersistValue for retaining variable values between calls to a function.

If not then is there any other way I can do this.

Thanks for your help in advance  
Nalina

---

<div class="post-metadata">

**Author:** ![mitch](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@mitch](https://community.wolfram.com/u/mitch)\
**Post date:** [August 31, 2018, 1:01am UTC](https://community.wolfram.com/t/persist-a-module-variable-value-between-two-calls-to-a-function/13036/2 "2018-08-31T01:01:19Z")

</div>

I’m not at all sure what your goal is but as you are new to Mathematica I’m going to make some assumptions and offer the following. Make a function

```
f[x_] := x

```

So when the variable x = 1:

```
f[1]

```

> 1

```
and when the variable x = 2:

f[2]

```

> 2

Notice the function f is still f, not 1 or 2, and the Global definition of f is…

```
?f

```

> Global`f

f[x\_]:=x

If you want to persist a unique result of f you should set it to a unique variable name.

```
f1 = f[1]
f2 = f[2]

```

> 1 and 2

these persist, unless you later change them, or Quit the kernel.

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [August 31, 2018, 7:36am UTC](https://community.wolfram.com/t/persist-a-module-variable-value-between-two-calls-to-a-function/13036/3 "2018-08-31T07:36:43Z")

</div>

Hi Nalina,

I am not sure whether I am understanding you question correctly. Maybe this is what you need:

```
Module[{oldArg = 0},
 f[x_] := With[{xx = oldArg}, oldArg = x; {xx, x}]
 ]

```

Whenever you call `f[x]` it “knows” its former argument. This variable (`oldArg`) itself cannot be seen from outside, but from the functions (`f[]`) point of view it is global. This is a nice trick I learnt from [@Leonid Shifrin](http://community.wolfram.com/web/leonids) (if I remember correctly).

Regards – Henrik  
[at0]: [http://community.wolfram.com/web/leonids](http://community.wolfram.com/web/leonids)

---

<div class="post-metadata">

**Author:** ![vemparalan](https://avatars.discourse-cdn.com/v4/letter/v/7ea924/32.png) [@vemparalan](https://community.wolfram.com/u/vemparalan)\
**Post date:** [September 1, 2018, 1:23am UTC](https://community.wolfram.com/t/persist-a-module-variable-value-between-two-calls-to-a-function/13036/4 "2018-09-01T01:23:43Z")

</div>

Hi Henrik  
Thanks, I will try this out. That is what I want, I want to remember the oldArg between two function calls,  
supposing in first function call, we set oldArg =2, in the second function call I want to remember oldArg=2.

Thanks a lot  
Nalina

---

<div class="post-metadata">

**Author:** ![vemparalan](https://avatars.discourse-cdn.com/v4/letter/v/7ea924/32.png) [@vemparalan](https://community.wolfram.com/u/vemparalan)\
**Post date:** [September 1, 2018, 1:29am UTC](https://community.wolfram.com/t/persist-a-module-variable-value-between-two-calls-to-a-function/13036/5 "2018-09-01T01:29:08Z")

</div>

Hi Mitch  
Thanks a lot for taking time to reply to my query.

I will try your suggestion,

Thanks  
Nalina
