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