Types of functions#
- Built-in:
len(),max(),input(). - Defined in modules:
math.sqrt(),random.randint(),statistics.mean(). - User-defined: created with
def.
Parameters vs arguments#
- Parameters (formal) appear in the function header; arguments (actual) are values passed in the call.
- Default parameters must come after non-default ones:
def f(a, b=2)✓,def f(a=1, b)✗. - A function can return multiple values as a tuple:
return mn, mx.
फ़ंक्शन की परिभाषा में लिखे नाम पैरामीटर (औपचारिक) कहलाते हैं और कॉल करते समय दिए गए मान आर्ग्युमेंट (वास्तविक)। डिफ़ॉल्ट पैरामीटर हमेशा बाकी पैरामीटरों के बाद लिखे जाते हैं।
Scope of variables#
- Local: created inside a function - exists only there.
- Global: created outside all functions. To change a global inside a function, declare
global x.
Mutable arguments#
Lists passed to a function can be changed inside it (the function receives a reference); integers and strings cannot.
Exam tips#
- Output questions on scope and default arguments are very common - trace line by line.
- A function without
returnreturnsNone.