Lecture 8 - Functions: environments, scope, functions as objects Flashcards
What is the purpose of a function’s docstring?
It serves as documentation, explaining what the function does, its parameters, and what it returns.
A docstring is considered a ‘contract’ as it outlines the guarantees made by the function writer regarding its behavior with specific inputs.
What happens when a return statement is executed in a Python function?
The function immediately stops, and the specified value is passed back to the part of the code that called the function.
If no explicit return statement exists, the function implicitly returns None.
Differentiate between formal parameters and actual arguments.
Formal parameters are the variables in the function definition; actual arguments are the values passed during a function call.
Example: In def greet(name):, ‘name’ is a formal parameter. In greet(‘Alice’), ‘Alice’ is the actual argument.
What is local scope in Python?
It refers to the visibility of variables within the function where they are defined.
A local variable is created when the function is called and ceases to exist when the function finishes executing.
Can a function access a variable defined in the global scope?
Yes, if a local variable with the same name is not defined within the function.
What does it mean for functions to be ‘first-class objects’ in Python?
It means functions can be treated like any other data type.
Examples include assigning a function to a variable or passing it as an argument to another function.
Describe what happens when a function call is made in terms of execution flow.
Python pauses the current code execution, creates a new local environment for the function, and the arguments are bound to the function’s parameters.
These environments are separate, meaning variables in one do not affect those in another unless specified.
What is the purpose of the global
keyword in Python?
It declares that a variable name refers to a variable in the global scope, allowing modification of global variables.
Extensive use of global variables can complicate code and lead to unexpected side effects.
What is a lambda expression?
A concise way to create anonymous functions in Python, in the form lambda arguments: expression.
Example: lambda x: x * 2 defines a function that takes x and returns its value multiplied by 2.
What is a Function Definition?
The process of creating a reusable block of code with a name, parameters, and a body containing statements to be executed. The def keyword is used.
What does Function Call (Invocation) refer to?
The act of executing a defined function by using its name followed by parentheses, possibly including arguments.
What are Parameters in the context of functions?
Variables declared in the function definition that act as placeholders for the values the function will receive.
Define Arguments in relation to functions.
The values passed to a function during a function call, assigned to the corresponding formal parameters.
What is a Return Statement?
A statement within a function that specifies the value to be passed back to the caller.
What happens during an Implicit Return?
If a function reaches the end of its body without an explicit return statement, it implicitly returns None.
What is a Docstring?
A multiline string literal used to document a function, describing its purpose, arguments, and return value.
What does it mean that a Function is treated as an Object in Python?
Functions can be assigned to variables, passed as arguments, and returned as values from other functions.
Define Scope in programming.
The region of a program where a particular name (variable, function) is accessible.
What is Global Scope?
Variables defined outside of any function that are potentially accessible from anywhere in the program.
What is Local Scope?
Variables defined inside a function that are only accessible within that function.
What are Environments (Frames) in the context of function calls?
Each function call creates a new local environment containing local variables and parameters of that specific call.
What is Name Binding?
The association of a name (variable) with an object (value, function).
What is the purpose of the global Keyword?
Indicates that a name refers to a global variable, allowing modification of the global variable from within the function.
What does the nonlocal Keyword do?
Indicates that a name refers to a variable in the nearest enclosing function’s scope.