Calling Procedures - AP Computer Science Principles

  1. What is a procedure in programming?
    A named section of code that performs a specific task. Code blocks enable reusability and modularity.
  2. What is a method in object-oriented programming?
    A procedure associated with a class/object. Function bound to a specific class instance.
  3. Which part of a procedure specifies its output?
    Return value. What the procedure sends back to the caller.
  4. Identify the error in: 'def add(x, y): return x + y; print(add(3))'
    Missing one argument in 'add(3)'. Function expects two arguments but only receives one.
  5. What is the scope of a parameter?
    Within the procedure where it is defined. Parameters only exist within their procedure's execution.
  6. How are procedures beneficial in programming?
    They promote code reuse and organization. Avoid code duplication and improve maintainability.
  7. Identify the part of a procedure that specifies input.
    Parameters. Parameters define what inputs the procedure expects.
  8. What does 'void' mean in procedure context?
    Procedure does not return a value. Procedures that perform actions without output.
  9. Find and correct the error: 'def myFunc(x, y) return x + y'
    Correct: 'def myFunc(x, y): return x + y'. Missing colon after parameter list in function definition.
  10. Identify the error: 'def add(a, b): print(a + b)' when expecting return.
    Missing return statement. Function prints but doesn't return the sum value.
  11. What is modularity in programming?
    Dividing a program into separate modules. Breaks complex problems into manageable components.
  12. Choose the correct term: 'void' procedures return a value?
    False. Void procedures perform actions without returning values.
  13. Find the error: 'def greet(name): return print('Hello', name)'
    Remove 'return' before 'print'. Cannot return a function call that returns None.
  14. What is the term for executing a procedure?
    Calling a procedure. Invokes the procedure to run its code.
  15. What does 'pass by reference' mean?
    Parameters refer to argument locations. Changes to parameters affect original variables.
  16. Identify the error: 'def func(x=1, y): return x + y'
    Non-default parameter 'y' after default 'x'. Required parameters must come before defaults.
  17. What is returned if a procedure lacks a return statement?
    Typically, 'None' or equivalent. Default return when no explicit return statement exists.
  18. What does 'pass by value' mean?
    Arguments are copied into parameters. Original values remain unchanged in calling code.
  19. What is recursion in procedures?
    Procedure calls itself. Enables solving problems by breaking them down.
  20. What is a function signature?
    Function name and parameter list. Identifies the procedure and its expected inputs.
  21. State whether procedures can have default parameter values.
    Yes. Default values provide flexibility in procedure calls.
  22. What is a parameter's role in a procedure?
    To receive arguments as input. Acts as placeholder for values passed during calls.
  23. Identify the error: 'def foo(x, y=3, z): return x + y + z'
    Non-default parameter 'z' after default 'y'. Required parameters cannot follow optional ones.
  24. Identify the error: 'def example(a, b): return a * b; example(2)'
    Missing one argument in 'example(2)'. Two parameters required but only one provided.

Study these flashcards