You don’t have to define your functions at the outermost, global, level of your program. Anywhere you can have a statement such as if or while or a variable assignment, you can also define a function. A function definition is just another type of statement. This means that you can define a function inside the body of another function. This is a power tool; it enables you to write very flexible and elegant code, but as with all power tools, there is also a certain risk of self-harm. At least until you get comfortable ...
7. Inner Functions
Get Introduction to Computational Thinking: Problem Solving, Algorithms, Data Structures, and More now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.