Message Boards Message Boards

0
|
5135 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:

[?] Convert a constant to a variable?

Posted 7 years ago

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.

POSTED BY: deimos1990
3 Replies
Posted 7 years ago

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
POSTED BY: Hans Milton
Posted 7 years ago

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

POSTED BY: deimos1990

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 (https://reference.wolfram.com/language/tutorial/OptionalAndDefaultArguments.html)

f[x_, a_:constantValue] := a x
POSTED BY: Sean Clarke
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract