Podcast
Questions and Answers
What is another term for call by need?
What is another term for call by need?
A recursive function must always call itself at least once.
A recursive function must always call itself at least once.
False (B)
What is the term for a case in a recursive function where the function does not call itself?
What is the term for a case in a recursive function where the function does not call itself?
recursion anchor
When a function calls itself, each invocation has its own local ________ and variables.
When a function calls itself, each invocation has its own local ________ and variables.
Signup and view all the answers
Match the following terms with their descriptions:
Match the following terms with their descriptions:
Signup and view all the answers
Which of the following describes the factorial problem?
Which of the following describes the factorial problem?
Signup and view all the answers
The following is an example of a recursion step: return max(list[0], maximum(list[1:]))
The following is an example of a recursion step: return max(list[0], maximum(list[1:]))
Signup and view all the answers
What is the output of the code snippet summe([1, 5, 2, 4, 9])
?
What is the output of the code snippet summe([1, 5, 2, 4, 9])
?
Signup and view all the answers
What happens when using call by reference in a swap function?
What happens when using call by reference in a swap function?
Signup and view all the answers
In Python, call by name causes actual parameters to be evaluated before executing the function.
In Python, call by name causes actual parameters to be evaluated before executing the function.
Signup and view all the answers
What is the output of the expression True or 1/0 in Python?
What is the output of the expression True or 1/0 in Python?
Signup and view all the answers
In call by ____, a parameter's value is stored after its first evaluation for subsequent use.
In call by ____, a parameter's value is stored after its first evaluation for subsequent use.
Signup and view all the answers
Which of the following languages supports call by reference?
Which of the following languages supports call by reference?
Signup and view all the answers
Match the following programming concepts with their definitions:
Match the following programming concepts with their definitions:
Signup and view all the answers
Call by name evaluates parameters before the function starts execution.
Call by name evaluates parameters before the function starts execution.
Signup and view all the answers
Describe a situation where using call by need would be beneficial.
Describe a situation where using call by need would be beneficial.
Signup and view all the answers
What is a key feature of subroutines?
What is a key feature of subroutines?
Signup and view all the answers
A subroutine can have no parameters.
A subroutine can have no parameters.
Signup and view all the answers
What is the purpose of the 'return' statement in a function?
What is the purpose of the 'return' statement in a function?
Signup and view all the answers
In Python, if a function does not explicitly return a value, it returns __________.
In Python, if a function does not explicitly return a value, it returns __________.
Signup and view all the answers
Match the programming concepts with their descriptions:
Match the programming concepts with their descriptions:
Signup and view all the answers
Which of the following statements about functions is correct?
Which of the following statements about functions is correct?
Signup and view all the answers
All functions in Python are considered subroutines, but not all subroutines are functions.
All functions in Python are considered subroutines, but not all subroutines are functions.
Signup and view all the answers
What happens if a return statement is used without a value in a function?
What happens if a return statement is used without a value in a function?
Signup and view all the answers
Study Notes
Functions and Algorithmic Problems
-
Subroutines and Functions: Imperative programs use subroutines/functions to organize instructions into named units, improving program structure and sophistication. Subroutines execute when called/invoked by name.
-
Subroutines and Parameters: Subroutines can accept parameters (variables) to pass information, which are declared in the subroutine definition. The subroutine call must match the definition's parameter declaration.
-
Functions and Return Values: Subroutines can also return values using a return statement. Return statements end function execution, even if instructions follow the return, and the return value can be used in expressions.
-
Function Scope: A function's scope defines where variables are valid in a program. Variables created in a function's local scope are not accessible outside that function. Nested scopes are possible.
-
Global Scope: The main program's scope is the global scope; global scope variables can be used in functions within the program. If a local variable shares a name with a global variable, the local variable takes precedence within its scope.
-
Global Keyword: Python's global keyword, in a function, instructs the function to use the variable from the global scope, rather than creating a new local variable with the same name.
-
The 5 Steps: To improve program structure, when implementing functions, specify precise behavior and include comments for further details.
-
Function Signature: The function signature includes the function's name, input parameter types, and return type, and it should be defined before the function is implemented.
-
Function Specification: The function's specification acts as a contract between the developer and the program user. It precisely defines preconditions, return values, and effects.
Evaluation Strategies
-
Formal and Actual Parameters: Formal parameters are those in a function's declaration, and actual parameters are used in function calls.
-
Strict Evaluation Order: In strict evaluation, parameters are evaluated first, then the function is invoked with the evaluated parameters; their relationship is determined by the call convention.
-
Call by Value: Formal parameters are assigned copies of the actual parameters. Changes to formal parameters inside the function do not affect the actual parameters outside the function.
-
Call by Reference: Formal parameters are treated as synonymous with actual parameters. Changes inside the function affect the actual parameters outside the function.
Recursion
-
Recursion: A function calls itself. Each recursive call has its own scope and variables.
-
Recursion Anchor: A part of a recursive function where the function does not call itself (basis of the recursion).
-
Recursion Steps: A part of a recursive function in which the function calls itself (the steps in the process that will reduce the problem into steps smaller than the original problem -- eventually to a point of having a problem small enough to trivially solve with the anchor.
Algorithmic Problems and Search Problems
-
Algorithmic problems: Formal descriptions of input and output suitable for computer processing. Examples include factorial, product, greatest common divisor (GCD) calculation, and finding the shortest path.
-
Search problems: Find an element or value within a data structure (e.g., a list or tree). Examples include linear search and binary search.
-
Linear Search: Searches sequentially through all elements until the target is found. Has a linear time complexity (O(n))
-
Binary Search: Searches an ordered data structure by repeatedly dividing the search interval in half; significantly faster for larger datasets than linear search; has logarithmic time complexity (O(log n)).
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Explore the essential concepts of functions and subroutines in programming. This quiz covers their definitions, parameters, return values, and scope. Test your understanding of how these concepts contribute to program structure and execution.