Python Foundations · Names & data
Functions, arguments & scope
Functions package logic you reuse. Learn positional/keyword args, return values, and where names live (LEGB).
Step 1 of 5 · 1. Anatomy of a function15 min read ·
1. Anatomy of a function
def notional(qty, px, fx=1.0):
"""Trade value in USD."""
return qty * px * fx
notional(100, 99.5) # positional
notional(qty=100, px=99.5, fx=1.36) # keyworddef creates a function object and binds a name to it. return sends a value back; without it the function returns None. Defaults like fx=1.0 are evaluated once (recall the mutable-default trap from Lesson 1).
Next: 2. Scope: LEGB