Saute onion in butter. Whisk flour and milk together. Add salt & pepper. Once onions are sauteed, add flour mixture. Cook & stir until it thickens. (If you think mixture is too thick, add a little more milk). Put shredded cheese into mixture. Stir until melted and smooth. Take off heat. Put potato slices right into cheese mixture and coat the potatoes.
Grease a 13x9 baking pan. Layer 1/2 of cheesy potatoes, then ham, and then top it with rest of potatoes. Don't cover.
Bake at 375 degrees F for about an hour and a half. Let sit for at least 10 minutes.