What is a Python function?
A function is a named, reusable block of code that performs a specific task. It helps divide large problems, reduce repetition, and improve readability, testing, and maintenance.
Study 5 Functions and Modular Programming with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What is a Python function?
A function is a named, reusable block of code that performs a specific task. It helps divide large problems, reduce repetition, and improve readability, testing, and maintenance.
What is the difference between defining and calling a function?
The `def` statement defines a function and creates its function object; the function call, such as `greet()`, executes its indented body.
How does a parameter differ from an argument?
A parameter is the name listed in a function definition; an argument is the value supplied to that parameter in a function call.
How do positional and keyword arguments differ?
Positional arguments are matched by order, while keyword arguments are matched by parameter name. Positional arguments must come before keyword arguments.
When does Python use a default parameter value?
A default parameter value is used when the caller omits that argument. The default is evaluated when the function is defined, not on each call.
What do `*args` and `**kwargs` collect?
`*args` collects extra positional arguments into a tuple, while `**kwargs` collects extra keyword arguments into a dictionary.
What does `return` do?
`return` sends a value to the caller and immediately ends that function call. Unlike `print()`, it makes the result available for further computation.
What does a function return without `return`?
If execution reaches the end without a `return` statement, Python returns `None`. Printing a value does not automatically return it.
What does the LEGB rule describe?
LEGB is Python’s name-resolution order: Local, Enclosing, Global, then Built-in scope.
What does assignment inside a function usually create?
An assignment inside a function normally creates or updates a local variable, even when a module-level variable has the same name.
What is a function docstring?
A docstring is a string literal placed first in a function body. Python stores it in the function’s `__doc__` attribute for documentation tools and users.
What is modular problem-solving?
Modular problem-solving divides a program into focused components, designs their interfaces, tests them independently, composes them, and then refactors the result.