# How to use Strings and Functions With Conditions

Very new to Mathematica so this might be really simple.

I have the function

pow[x_,r_]:= x^r


where r is rational due to

rationalQ[r_] := IntegerQ[Numerator[r]] && IntegerQ[Denominator[r]]


I'm trying to use conditions so that when I test pow[0,0] where x=0 and r=0, the output uses the string "undefined" instead of Indeterminate.

Any idea how it works?

• you can simply do pow[0,0]="undefined" – george2079 May 10 '18 at 18:27
• thanks for the help! so I have a second case where I'm still using the same function and r is still rational, except this time I have to set up a condition where I make it so that when x is any number except 0 and r=0, the output should be 1. How would that work? – user58133 May 11 '18 at 23:32

You want to do something like this,

pow[x_,r_?rationalQ]:= x^r
pow[___] := "Undefined"


These function definitions are called DownValues in Mahtematica, and they are applied in order of increasing generality.

So what is going on here, if you evaluate pow with two variables, and the second variable gives True when fed to rationalQ, then that definition is used. Otherwise the evaluator will continue to look for other matching patterns. The pow[___] is the most general pattern, and so anything that doesn't match the first pattern will match it.