function summary
Python functions are the building blocks of code organization, often serving predefined tasks within modules and scripts.
They enable reusability, modularity, and encapsulation.
Key Components of functions
Function Signature: Denoted by the def keyword, it includes the function name, parameters, and an optional return type.
Function Body: This section carries the core logic, often comprising conditional checks, loops, and method invocations.
Return Statement: The function’s output is determined by this statement. When None is specified, the function returns by default.
Local Variables: These variables are scoped to the function and are only accessible within it
function Execution Process
When a function is called:
function Local Variable Scope
Function Parameters: These are a precursor to local variables and are instantiated with the values passed during function invocation.
Local Variables: Created using an assignment statement inside the function and cease to exist when the function execution ends.
Nested Scopes: In functions within functions (closures), non-local variables - those defined in the enclosing function - are accessible but not modifiable by the inner function, without using the nonlocal keyword
What is Global Visibility?
Functions offer a level of encapsulation, potentially reducing side effects by ensuring that data and variables are managed within a controlled environment. Such containment can help enhance the robustness and predictability of a codebase. As a best practice, minimizing the reliance on global variables can lead to more maintainable, reusable, and testable code.