Functions and handling bad input
Why functions
A function is a named, reusable block of code. It takes inputs (parameters) and gives back an output (return).
Parts of a function:
def gst(...)gives it a name. Use verbs:add_expense,load_data.amount: floatis a parameter with a type hint. Python doesn't enforce hints, but they document your intent, and from Week 13 onwards LLMs read them.rate: float = 0.18is a default value, so callers may leave it out.-> floatsays what the function returns.- The
"""docstring"""is a one-line explanation that shows up when you hover over the name in VS Code. returnsends a value back. A function withoutreturnreturnsNone.
print vs return. Beginners often print inside functions. print shows text to a human, while return gives a value to the rest of your code. Prefer return and let the caller decide whether to print.
Scope. Variables created inside a function exist only inside it. Pass data in through parameters and get it out through return, rather than reaching for global variables.
*args and **kwargs (you'll see them everywhere)
Library code, including LangChain, often accepts **kwargs and passes them on to something else.