# How to define a function only for integers

I want to create a function which only accepts integers. if a value other than integers are given it should output zero. This is what I've tried so far. I'm trying to do some discrete time signal experiments like time shifting.

specifically i want to create a function y[n] = u[n] - u[n-5] and create another using it, x[n] = y[n/2] + 2 y[n/2-1/2] so that i can get the following graph,

Here's my copy paste-able code: https://www.wolframcloud.com/objects/a0b8ba05-a8cc-4cae-a075-6435e301d41a

• For example, use IntegerQ, for example: gamma[n_] := If[IntegerQ[n], (n - 1)!, 0]. You shouls also look at gamma[n_Integer] := (n - 1)!. – Moo Dec 10 '18 at 6:00
• Thank you! so I was able to define y[n_] := If[IntegerQ[n], UnitStep[n] - UnitStep[n - 5], 0] – Ramith Hettiarachchi Dec 10 '18 at 6:20
• y[_] = 0; y[n_Integer] := UnitStep[n] - UnitStep[n - 5] – Fred Simons Dec 10 '18 at 7:47
• What about y[n_ /; IntegerQ[n]] := ... – Ulrich Neumann Dec 10 '18 at 9:59

In your code y[n_Integer] := (UnitStep[n] - UnitStep[n - 5]);, y[n] is only defined when its variable is Integer. That means if the variable of y[n] is a fractional number it outputs nothing. So please refer to my following code:
y[n_] := If[FractionalPart[n] == 0, UnitStep[n] - UnitStep[n - 5], 0]