Functions and Algorithmic Problems
24 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is another term for call by need?

  • Lazy evaluation (correct)
  • Eager evaluation
  • Recursive evaluation
  • Strict evaluation
  • 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?

    recursion anchor

    When a function calls itself, each invocation has its own local ________ and variables.

    <p>scope</p> Signup and view all the answers

    Match the following terms with their descriptions:

    <p>Recursion = A function calling itself. Recursion Anchor = The case in a recursive function where it does not call itself. Recursion Step = The case in a recursive function where it calls itself. Iterative = Using loops instead of recursion.</p> Signup and view all the answers

    Which of the following describes the factorial problem?

    <p>Input: n ∈ ℕ₀; Output: n! (C)</p> Signup and view all the answers

    The following is an example of a recursion step: return max(list[0], maximum(list[1:]))

    <p>True (A)</p> Signup and view all the answers

    What is the output of the code snippet summe([1, 5, 2, 4, 9])?

    <p>21</p> Signup and view all the answers

    What happens when using call by reference in a swap function?

    <p>The formal parameters are updated outside the function. (D)</p> Signup and view all the answers

    In Python, call by name causes actual parameters to be evaluated before executing the function.

    <p>False (B)</p> Signup and view all the answers

    What is the output of the expression True or 1/0 in Python?

    <p>True</p> Signup and view all the answers

    In call by ____, a parameter's value is stored after its first evaluation for subsequent use.

    <p>need</p> Signup and view all the answers

    Which of the following languages supports call by reference?

    <p>C++ (A)</p> Signup and view all the answers

    Match the following programming concepts with their definitions:

    <p>Call by reference = Changes in parameters affect actual arguments outside the function. Call by name = Parameters are replaced with their actual arguments at a textual level. Call by need = Parameters are evaluated once and stored for later use. Non-strict evaluation = Function may return before all arguments are evaluated.</p> Signup and view all the answers

    Call by name evaluates parameters before the function starts execution.

    <p>False (B)</p> Signup and view all the answers

    Describe a situation where using call by need would be beneficial.

    <p>When evaluating expressions that are computationally expensive, to avoid unnecessary repeated evaluations.</p> Signup and view all the answers

    What is a key feature of subroutines?

    <p>They allow for organized code by grouping instructions. (D)</p> Signup and view all the answers

    A subroutine can have no parameters.

    <p>True (A)</p> Signup and view all the answers

    What is the purpose of the 'return' statement in a function?

    <p>To return a value to the caller and end the function execution.</p> Signup and view all the answers

    In Python, if a function does not explicitly return a value, it returns __________.

    <p>None</p> Signup and view all the answers

    Match the programming concepts with their descriptions:

    <p>Subroutine = A named sequence of instructions that can be called. Function = A type of subroutine that returns a value. Parameter = A variable passed to a subroutine. Return Statement = Used to pass a value back to the caller.</p> Signup and view all the answers

    Which of the following statements about functions is correct?

    <p>Functions can return values or None if no value is returned. (D)</p> Signup and view all the answers

    All functions in Python are considered subroutines, but not all subroutines are functions.

    <p>False (B)</p> Signup and view all the answers

    What happens if a return statement is used without a value in a function?

    <p>It returns None.</p> 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.

    Quiz Team

    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.

    More Like This

    C Programming Functions Quiz
    5 questions
    Programming Language Subroutines Quiz
    18 questions
    Functions and Importance of Proteins
    18 questions
    Functions and Relations Quiz
    10 questions
    Use Quizgecko on...
    Browser
    Browser