CS 1110 Nate Brunelle Today Functions Questions Last
CS 1110 Nate Brunelle Today: Functions
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(input(s)): Indent Things we are doing return output
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
- Slides: 8