How can I build a functional gearbox?
there are some lego-models that have a working gearbox built-in like the super street sensation (8448). just take a loot at the building instruction* to see how it works.

*page 18, 26, 72-81