I want to define an indexed function $f$ with index $k$ over some arguments $m$. However, I only want to define the index for some set ${1,2,...,x}$ my approach was the following

f[k,m_]:= g[k,m]

However this seems not to define the function as




Is there another way to define $f$ as a function of $m$ but with index $k$ on the respective interval?

(Clarification: the original code has $k$ as an index-subscript, but I couldn't find how to do this here)

  • $\begingroup$ I don't understand. If you simply define f[k_, m_] := g[k, m] (without the loop) then whenever you ask for f[3,n] you will get g[3,n]. If you only want it defined for integers, then use f[k_IntegerQ, m_] := g[k, m] $\endgroup$
    – bill s
    Sep 9, 2016 at 14:33
  • 2
    $\begingroup$ Denoting by list the list of elements {1, 2, ..., x} (where x is given), you can do f[k_Integer, m_] /; MemberQ[list, k] := g[k, m], or MemberQ[Range[x], k] (where x is given) if you're only considering a set of consecutive integers up to x. $\endgroup$
    – user31159
    Sep 9, 2016 at 14:54
  • $\begingroup$ Welcome! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – user9660
    Sep 9, 2016 at 15:52

2 Answers 2


You issue is, that the right hand side of your definition - the g[k,m] part - is not evaluated and the value of k is not inserted. You can see this by looking at a simple Trace:


k = 1;
Trace[f[k, m_] := g[k, m]]
(* {f[k,m_]:=g[k,m],{{k,1},f[1,m_]},Null} *)

If you really want to follow your approach, you need to ensure, that the value of k is inserted, even if the rhs is not evaluated. This can be done using a pure function or With. I'm using Array or Range or Table to get rid of the ugly For loop. All three version should work, where the last one does indeed look funny:

(f[#, m_] := g[#, m]) & /@ Range[10];

Array[(f[#, m_] := g[#, m]) &, 10];

Table[With[{i = i}, f[i, m_] := g[i, m]], {i, 10}]

You can force a function to be defined for certain integer variable. For instance

f[x_,n_Integer]:= n Sin[x]

will be evaluated only if n is an integer

Try f[2.34,3] and f[2.34, 3.2] or even f[2.34,3.]

the two last function will not be evaluated


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.