Calling Procedures - AP Computer Science Principles
- What is a procedure in programming?
- A named section of code that performs a specific task. Code blocks enable reusability and modularity.
- What is a method in object-oriented programming?
- A procedure associated with a class/object. Function bound to a specific class instance.
- Which part of a procedure specifies its output?
- Return value. What the procedure sends back to the caller.
- 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.
- What is the scope of a parameter?
- Within the procedure where it is defined. Parameters only exist within their procedure's execution.
- How are procedures beneficial in programming?
- They promote code reuse and organization. Avoid code duplication and improve maintainability.
- Identify the part of a procedure that specifies input.
- Parameters. Parameters define what inputs the procedure expects.
- What does 'void' mean in procedure context?
- Procedure does not return a value. Procedures that perform actions without output.
- 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.
- 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.
- What is modularity in programming?
- Dividing a program into separate modules. Breaks complex problems into manageable components.
- Choose the correct term: 'void' procedures return a value?
- False. Void procedures perform actions without returning values.
- Find the error: 'def greet(name): return print('Hello', name)'
- Remove 'return' before 'print'. Cannot return a function call that returns None.
- What is the term for executing a procedure?
- Calling a procedure. Invokes the procedure to run its code.
- What does 'pass by reference' mean?
- Parameters refer to argument locations. Changes to parameters affect original variables.
- Identify the error: 'def func(x=1, y): return x + y'
- Non-default parameter 'y' after default 'x'. Required parameters must come before defaults.
- What is returned if a procedure lacks a return statement?
- Typically, 'None' or equivalent. Default return when no explicit return statement exists.
- What does 'pass by value' mean?
- Arguments are copied into parameters. Original values remain unchanged in calling code.
- What is recursion in procedures?
- Procedure calls itself. Enables solving problems by breaking them down.
- What is a function signature?
- Function name and parameter list. Identifies the procedure and its expected inputs.
- State whether procedures can have default parameter values.
- Yes. Default values provide flexibility in procedure calls.
- What is a parameter's role in a procedure?
- To receive arguments as input. Acts as placeholder for values passed during calls.
- 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.
- 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.