Is it necessary to eject external drives before logging out, restarting, or shutting down?
Solution 1:
No, it's only recommended when unplugging the cable.
The reason why you eject is so that any processes reading and writing from the disk will cause OS X to notify you of the disk being in use and prevent data corruption that can occur by just unplugging the disk while it's in use.
Additionally the OS might queue different write operations to a cache before actually writing them to disk, and ejecting will make sure these are flushed to the drive before it is disconnected.
Halting any processes from reading/writing to the disk and flushing the caches is implied when rebooting or shutting down the machine (unless you do a cold shutdown by pressing and holding the power button to turn it off). When logging out, the OS can flush the cache at its own discretion.
Solution 2:
There is nothing to gain from explicitly "ejecting" drives before logging pout, shutting down, or restarting in a normal fashion. MacOS X has a subsystem called "Disk Arbitration" (primarily the system daemon process "diskarbitrationd") which mounts drives other than the boot device when a user logs in and unmounts (a.k.a. "ejects") them when the user logs out. Because it operates outside of the context of a user login session, diskarbitrationd is able to assure that processes which are not directly managed by a user (Spotlight indexing, Time Machine, other users' login sessions, etc.) are dealt with correctly when unmounting devices. It also checks for (& usually can quietly repair) filesystem corruption issues before mounting drives. At a deeper level, the OS also has a daemon (update) which flushes the filesystem cache to disk every 30 seconds and a carefully scripted process for shutting down (i.e. the first half of restarting) which assures that all filesystem devices are properly synched and unmounted before resetting or powering down the system. MacOS X is unique in this respect only in the fine details: all significant OS's have matured beyond the need to have users manually assure that drives aren't unmounted in a "dirty" state.
The possible exceptions would be if you do something outside of the operating system to halt or reset the system, such as disconnecting the main power or forcing a hardware reset by a long-press of the power switch. Both could cause filesystem corruption, but no one should be taking such steps except in rare and severe cases where one likely won't have the luxury of being able to manually eject drives. (e.g. smoke, flames, kernel panics, etc.)