I have the following function before me:
$$f(x)=\left\{\begin{array}{ll}\phantom{-}x^2, &x\leq 0\\-x^2+2,&x>0\end{array} \right .$$
I have to find a function $g(x)$ such that $g'(x)=f(x)$. Clearly, $f(x)$ is not continuous at $x=0$, as there is a jump discontinuity at that point.
Do such functions admit any antiderivative. And if they do, is that antiderivative unique?
Will taking $g(x)=\dfrac{x^3}{3}$ when $x\leq0$ and $g(x)=-\dfrac{x^3}{3}+2x$ when $x>0$ work? Clearly $g(x)$ is continuous which is a pre-requisite for it to be differentiable.
For sufficiently nice functions $f$ like the one in this example (e.g., functions that are continuous on some interval except perhaps at a finite number of jump discontinuities) and any $a$ in the domain of $f$, $$F(x) := \int_a^x f(t) \,dt$$ (1) is continuous and (2) satisfies $$F'(x) = f(x)$$ everywhere except at those discontinuities, where $F'(x)$ is not defined. As usual, $F$ is not unique: For any constant $C$ we also have $(F(x) + C)' = f(x)$ everywhere except again at the discontinuities of $f$.
To compute an explicit formula for such an $F$, it's enough to compute an antiderivative of $f$ on each interval bounded by discontinuities and, roughly speaking, (by choosing constants of integration appropriately) patch them together into a single continuous function $F$.
In the case of our function, taking the lower bound of the above integral to be $a = 0$ yields the candidate you suggest: $$F(x) = \left\{\begin{array}{ll}\phantom{-}\frac{1}{3} x^3, &x \leq 0 \\ -\frac{1}{3} x^3 + 2 x, & x \geq 0\end{array}\right. .$$