# \[?\] Convert a constant to a variable?

**URL:** <https://community.wolfram.com/t/convert-a-constant-to-a-variable/10714>\
**Category:** Questions\
**Created:** [June 17, 2017, 5:38pm UTC](https://community.wolfram.com/t/convert-a-constant-to-a-variable/10714 "2017-06-17T17:38:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![deimos1990](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@deimos1990](https://community.wolfram.com/u/deimos1990)\
**Post date:** [June 17, 2017, 5:38pm UTC](https://community.wolfram.com/t/convert-a-constant-to-a-variable/10714/1 "2017-06-17T17:38:29Z")

</div>

Hi. I have an expression like

```
f:=ax

```

or`

```
f[x_]:=ax

```

and next i have to put

```
f[x_,a_]:=f

```

or

```
f[x_,a_]:=f

```

but it doesn’t work.

---

<div class="post-metadata">

**Author:** ![seanc](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/seanc/32/34005_2.png) [@seanc](https://community.wolfram.com/u/seanc)\
**Post date:** [June 19, 2017, 2:37pm UTC](https://community.wolfram.com/t/convert-a-constant-to-a-variable/10714/2 "2017-06-19T14:37:50Z")

</div>

Yes. That shouldn’t work. You can’t define something as a constant value and as a function at the same time. At very least that would be very confusing and I’m not sure I know any programming language where it’s possible.

You want to write something like:

```
f[x_] := a x
f[x_,a_] := a x

```

Or if you want to be more clever, you can use default values ([Patterns—Wolfram Documentation](https://reference.wolfram.com/language/tutorial/OptionalAndDefaultArguments.html))

```
f[x_, a_:constantValue] := a x

```

---

<div class="post-metadata">

**Author:** ![deimos1990](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@deimos1990](https://community.wolfram.com/u/deimos1990)\
**Post date:** [June 19, 2017, 2:58pm UTC](https://community.wolfram.com/t/convert-a-constant-to-a-variable/10714/3 "2017-06-19T14:58:54Z")

</div>

Thank you for your reply. I explained better the problem i would to solve in the post called “Can I treat indexed objects as variables?”. I’ll be glad if you can help me

---

<div class="post-metadata">

**Author:** ![hansmilton](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hansmilton/32/8656_2.png) [@hansmilton](https://community.wolfram.com/u/hansmilton)\
**Post date:** [June 23, 2017, 5:05pm UTC](https://community.wolfram.com/t/convert-a-constant-to-a-variable/10714/4 "2017-06-23T17:05:33Z")

</div>

Does this help?

```
In[1]:= f[] = 3;
In[2]:= f[a_] := a^7

In[3]:= f[]
Out[3]= 3

In[4]:= f@5
Out[4]= 78125

```

Another take:

```
In[1]:= f[] := a x    
In[2]:= f[x_, a_] := f[]

In[3]:= f[p, q]
Out[3]= a x

```
