Lecture 7 - Bisection Search Flashcards
Decomposition, Abstraction, and Functions
Why None
and False
are printed to the console as a result from calling the div_by()
function?
function does not return any value
When div_by()
function is called with arguments 10 and 3, Python evaluates the expression 3%10 == 0
, which is False
, resulting in the block inside the else statement being executed.
Then, Python will execute the print(div_by(10,3))
line. Since the function does not return any value, the function call div_by(10,3)
does not return any value. Therefore, when this is printed to the console, it yields the None
type, which is the absence of any value.
What is a function in Python?
A function in Python is a reusable block of code that performs a specific task. Functions are important because they allow programmers to generalise code, reducing redundancy and making programs more modular and easier to maintain.
What is the difference between formal parameters and actual parameters in a function?
Formal parameters are the names used in the function definition to represent the inputs the function expects. Actual parameters (or arguments) are the specific values passed to the function when it is called; these values are then bound to the formal parameters.
What happens during a function call?
During a function call, the actual parameters are evaluated and bound to the formal parameters. The point of execution moves to the function body, which is executed until a return statement is encountered or the end of the body is reached. Finally, a value is returned to the calling code.
What are keyword arguments?
Keyword arguments bind formals to actuals using the name of the formal parameter. Unlike positional arguments, which rely on order, keyword arguments explicitly identify which formal parameter receives which actual parameter.
Give an example of using keyword arguments.
print_name(last_name='Smith', first_name='John')
How do default parameter values work in Python functions?
Default parameter values allow a function to be called with fewer arguments than specified, in which case the default values are used for the missing parameters. They are useful for providing sensible defaults, reducing the burden on the caller, and making function calls more flexible.
How can a Python function accept a variable number of arguments?
A Python function can accept a variable number of positional arguments using the unpacking operator * in the parameter list. The *args syntax gathers all positional arguments into a tuple named args, which can then be iterated over within the function.
What is scoping in Python?
Scoping in Python refers to the region of a program where a name (variable) is visible and can be accessed. When a function is called, a new symbol table (stack frame) is created to track names defined within the function.
What is a function specification?
A function specification is a contract between the implementer of a function and its clients, defining what the function does and how it should be used. It includes assumptions about the input parameters and guarantees about the function’s behaviour and output.
What are the two main components of a function specification?
The two main components of a function specification are:
* Assumptions about the input parameters
* Guarantees about the function’s behaviour and output.
Explain the concept of decomposition in programming.
Decomposition is breaking a program into self-contained, reusable parts, like functions, to create structure. It helps manage complexity and improve code organization.
What is abstraction in programming?
Abstraction hides the inner details of a piece of code so it can be used as a black box. It is essential in managing complexity and promoting code reuse.
What is a docstring in Python?
A docstring is a string literal that occurs as the first statement in a function definition. It is used to document the function’s purpose, arguments, and return value.
How can a docstring be accessed?
A docstring can be accessed using the help() function or IDE tools.
What is a formal parameter?
A formal parameter is a variable name used in a function definition to represent an input value.
What is an actual parameter (argument)?
An actual parameter (argument) is the value passed to a function when it is called, which is then bound to the corresponding formal parameter.
What is a positional argument?
A positional argument is an argument passed to a function based on its position in the function call, matching the order of formal parameters.
What is a keyword argument?
A keyword argument is an argument passed to a function using the name of the formal parameter, allowing arguments to be specified out of order and improving code readability.
What is a default parameter value?
A default parameter value is a value assigned to a formal parameter in a function definition, which is used if the corresponding argument is not provided in the function call.
What is the unpacking operator (*) in Python?
The unpacking operator (*) is used in function definitions to accept a variable number of positional arguments, which are collected into a tuple.
What is a scope in programming?
A scope is the region of a program where a name (variable) is visible and can be accessed.
What is a symbol table (stack frame)?
A symbol table (stack frame) is a data structure used to store the names and bindings of variables within a particular scope, such as a function.
What is static/lexical scoping?
Static/Lexical scoping is a scoping rule in which the scope of a name is determined by its position in the program text.