Functional programming Function Libraries: - libstg example - Interfaces are commonly defined by functions Example: math library Example: POSIX - It is possible to write functions in C and use them in Python Maybe I'll show you someday... Recursion, part #1: - We'll talk more about recursion near the end of the course - A function can call itself for productive purposes - Factorial example...but there will be another example later today First-Class Functions: - A function is a value, like a number or a string - A language has first-class functions if you can: + Pass a function to a function + Return a function from a function + Create a function with no name (anonymous function) - Demos... - Python mostly has first-class functions (no statements in anonymous functions) - This brings up some issues! + Closures Why does this look so natural in Python? - Let's look at it in C for a minute - Operating systems are written in C - They're full of function pointers! Java and first class functions: - No - You can sort of hack it with an object and methods... - May have changed in Java 8 A natural question: Is this useful? - Example: graphalign - There aren't any programs you can't write without them - Passing functions to functions is used for a lot of things + Example: start_new_thread - I didn't see it mentioned in the book - But I use first-class functions regularly! Ok, let's play recursion! - With the turtle! - How do you draw concentric circles?