Nate Brunelle Today: Functions CS1110 Nate Brunelle Today: Functions
CS Majors Night What’s it like to be a CS major? How do I declare a CS major? Find out Tonight! Rice 130 5pm
Questions?
Last Time Strings, casting types
Function Like going to chipotle: Enter with Your chipotle order in mind Input Instructions for how to turn your order into a burrito Things can happen at chipotle that leave chipotle Side effects E.g. less money in your bank account Leave with your burrito/bowl output
Creating a function def name(“stuff we need to do the function”): Indent Things we are doing return result
Functions Vocab Expression – Code that can become a value Literal expression – an expression that cannot be simplified Evaluate – turn an expression into a value Argument – value/expression we send into a function Parameter – name we use for input within a function definition Attached to an argument when we use the function Call/invoke – Use a function Return – exit the function with a value Return Value – the value we returned Side Effect – things that a function does beyond returning something
Advice on functions You usually don’t want functions with side effects Don’t use print, instead return a str Don’t use input, instead use parameters Good names Usually an action phrase Use docstrings Define function in one file, use in another
Scope Scope- The are where a variable has meaning Usually: variable is in scope when it is created onward. Functions: variable is in scope when it is created until end of the function definition