While preparing a lecture today, I ran across the necessity of writing source code in a beamer presentation. Some searching brought me to this stackexchange question, that answered my question quite swiftly: you use the “verbatim” environment, but you also need to add the [fragile] option to the frame. Why is that? This blog post brought a very comprehensive answer to that question.