Preheat the oven to 350°F and lightly grease an 8x8 inch baking dish.
In a saucepan over medium heat, melt the butter. Whisk in the flour and cook for one minute. Slowly pour in the milk while whisking to avoid lumps.
Add the salt and pepper to the sauce and stir until it thickens slightly, then remove from heat.
Spread one layer of sliced potatoes in the baking dish, followed by some onion slices and diced ham. Pour a portion of the sauce over the top.
Repeat the layers until all potatoes, ham, and onion are used, ending with a final layer of sauce. Sprinkle the shredded cheddar cheese evenly over the top.
Cover the dish with foil and bake for 45 minutes. Remove the foil and bake for another 20 minutes until the potatoes are tender and the cheese is lightly browned.