This has to be the stupidest question for the whole week but,

I watched this video: http://www.khanacademy.org/video/introduction-to-limits--hd?playlist=Calculus

and the guy defined a function

$$f(x) =\left\{ \begin{array}{ll} x^2, & \hbox{if }x\neq 2; \\ 1, & \hbox{if }x=2. \end{array} \right.$$

I believe that's the proper notation, please check the video at 5:52 if I messed it up.

My question is, is there a way to figure out the function's "body", just by making up rules like this?

For example, can I say that I have a function $f(x)$ that for

$$f(x) =\left\{ \begin{array}{ll} \sin(x), & \hbox{if }-1 < x < 1; \\ 0, & \hbox{if }x < -1\mbox{ or }x > 1. \end{array} \right.$$

How would I figure out the function's body just by using those preset rules?

Just pointing me in the right direction would be more than enough, please use the catalogue at khanacademy.org and tell me what to watch. Thank you for your time!

P.S. This might not make a ton of sense at first, but it's interesting for me because I'm a front-end web developer and I'm doing lots of graphic or motion related programming, which involves math.

  • 2
    $\begingroup$ What do you mean by "body"? Do you mean the graph of the function, or something else? $\endgroup$
    – NKS
    Commented Jan 2, 2012 at 22:38
  • 4
    $\begingroup$ The "$\cases{blah &\text{if this}\\ blah &\text{if that}}$" is the the definition of the function -- it's a perfectly good way to define a function and there no rule that says a function definition has to fit on one line. If you have a description that's unambiguous enough to allow you to figure out what the function value for each particular argument is, then by definition that description constitutes a function. It doesn't need to have any particular syntactic form. $\endgroup$ Commented Jan 2, 2012 at 23:31
  • 1
    $\begingroup$ It may be possible to twist the straightforward definition by cases into to something horrible involving improper integrals and whatnot that looks uniform at first sight -- but what if that twisted definition takes twenty times as long to compute than either of the plain cases in the straightforward definition? There'll be plenty of time left over for a conditional jump. $\endgroup$ Commented Jan 3, 2012 at 0:14
  • 2
    $\begingroup$ ... Excluding horrible integrals and limits (neither of which are particularly cpu-friendly), the best you can hope for is to hide the conditional jump with prettier notation such as Iverson brackets or step functions, but all that will buy you is at best to add a function call to your expenses, relative to the straightforward definition. $\endgroup$ Commented Jan 3, 2012 at 0:16
  • 1
    $\begingroup$ What language are you using? A number of languages allow for a construction like the Iverson bracket; that is, a factor that is equal to 1 when a condition is true, and 0 if a condition is false... $\endgroup$ Commented Jan 3, 2012 at 1:01

1 Answer 1


This is a piecewise-defined function. To write this in a programming language setting, you can take three routes:

If/Then: By far the simplest (here's pseudo-Python code)

def f(x):
    if (x > 1 || x < -1):
        return 0
        return math.sin(x)

Ternary Operator: Works in some languages, others it doesn't work. For Java:

public static double f(double x) {
    return (x > 1 || x < -1) ? 0 : Math.sin(x)

Cast Boolean to Numeric Also language dependent. In some languages, a false value is equivalent to $0$, and a true value is equivalent to $1$. This is true of Python:

def f(x):
    return (x > 1)*(x < -1)*math.sin(x)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .