Program Function and Purpose - AP Computer Science Principles

  1. Identify the error: def divide(a, b): return a/b; divide(5,0)
    Division by zero error. Cannot divide by zero in mathematics.
  2. Identify the output: f = lambda x: x + 1; print(f(5))
    6. Lambda function adds 1 to input 5.
  3. Find and correct: def square(x): return x**; print(square(4))
    Replace x with x2. Exponentiation operator requires complete expression.
  4. What is an anonymous function?
    A function defined without a name, often using lambda. Lambda creates functions without explicit naming.
  5. Find the error: def increment(x): x+=1; return x; increment()
    Missing argument in increment(). Function call requires one argument but none provided.
  6. What is the purpose of function documentation?
    To describe how the function works and its parameters. Documentation explains usage, parameters, and behavior.
  7. What is an argument in the context of functions?
    A value passed to a function when it's called. Arguments are actual values sent to functions.
  8. Which keyword is used to define a function in Python?
    def. Standard Python keyword for function declaration.
  9. What does 'modularity' mean in a programming context?
    Breaking down a program into separate functions. Organization strategy using independent, focused functions.
  10. What are side effects in a function?
    Changes made by a function that affect outside state. Functions modify variables or system state externally.
  11. Identify the error: def add(a, b): return a + b; add(1)
    Missing one argument in add(1). Function requires two parameters but only receives one.
  12. What does 'DRY' stand for in programming?
    Don't Repeat Yourself. Principle promoting code reusability and maintenance.
  13. What is a higher-order function?
    A function that takes another function as an argument. Functions that operate on other functions.
  14. What is a pure function?
    A function with no side effects and consistent output. Pure functions have predictable, isolated behavior.
  15. Identify the output: def f(x): return x*x; print(f(4))
    16. Function squares input value 4.
  16. Find the error: def multiply(x): return x*y; multiply(2,3)
    y is undefined in multiply. Variable y is not defined in function scope.
  17. What is the purpose of variable scope?
    To define where a variable can be accessed. Scope controls variable visibility and access.
  18. What does 'first-class functions' mean?
    Functions treated as first-class citizens, assignable to variables. Functions can be stored, passed, and manipulated.
  19. What is a global variable?
    A variable defined outside functions, accessible by all. Global variables exist throughout program execution.
  20. What is a local variable?
    A variable defined within a function, accessible only there. Scope restricts access to function's internal area.
  21. Identify the output: def f(x): return x-1; print(f(0))
    -1. Function subtracts 1 from input 0.
  22. What is the output: def g(): return 10; print(g())
    10. Function returns literal value 10.
  23. Differentiate between a function and a method.
    A method is a function that belongs to an object. Methods are functions bound to specific objects.
  24. What is the purpose of comments in a function?
    To explain code functionality for human readers. Comments improve code readability and maintenance.
  25. What is the purpose of a default parameter value?
    To provide a fallback value if no argument is passed. Defaults handle optional parameters gracefully.
  26. What is a parameter in a function?
    A variable used to pass information into a function. Parameters act as placeholders for function inputs.
  27. What is the purpose of a function prototype?
    To declare the function's signature before its definition. Prototypes declare function interface before implementation.
  28. Identify the output: def m(x=5): return x; print(m())
    5. Function uses default parameter value 5.
  29. What is function overloading?
    Defining multiple functions with the same name but different parameters. Same name functions with different parameter signatures.
  30. Identify the output: def h(): pass; print(h())
    None. Empty function returns None by default.
  31. What is the purpose of the return statement in a function?
    To send back a result from the function. Return transfers computed values back to caller.
  32. Identify the output: def f(x): return x+2; print(f(3))
    5. Function adds 2 to input 3, returning 5.
  33. What is recursion in programming?
    A function calling itself to solve a problem. Self-reference enables solving complex problems iteratively.
  34. Identify the output: def sum(a, b): return a+b; print(sum(3,4))
    7. Function adds two arguments together.
  35. Identify the output: def f(x): return x**3; print(f(2))
    8. Function cubes input value 2.
  36. What is the main purpose of a function in programming?
    To encapsulate reusable code logic. Functions group related code into reusable blocks.
  37. What is a function's signature?
    The function's name and its parameter list. Identifies function uniquely by name and parameters.
  38. Identify the output: def square(x): return x**2; print(square(3))
    9. Function squares input value 3.

Study these flashcards