Parameters (OCR A-Level Computer Science): Revision Notes
Parameters
Overview
Parameters are variables used to pass data into functions or procedures, allowing them to work with different values. They are a crucial part of modular programming, as they enable functions and procedures to be more flexible and reusable.
Understanding how to define and use parameters, and the difference between passing by value and passing by reference, is essential for writing effective and efficient code.
What are Parameters?
- Definition: Parameters are variables used in function or procedure definitions to accept input values.
- Purpose: They allow functions and procedures to operate on different data without needing to rewrite the code.
Example in Python:
def greet(name):
print(f"Hello, {name}!")
Arguments vs. Parameters
- Parameters are placeholders defined in the function or procedure.
- Arguments are the actual values passed to the function or procedure when it is called.
Example:
def greet(name): # 'name' is a parameter
print(f"Hello, {name}!")
greet("Alice") # "Alice" is the argument
Types of Parameters
Positional Parameters:
Arguments are passed in the same order as the parameters are defined.
Example:
def add(a, b):
return a + b
result = add(5, 3) # a = 5, b = 3
Default Parameters:
Parameters can have default values if no argument is provided.
Example:
def greet(name="Guest"):
print(f"Hello, {name}!")
greet() # Outputs: Hello, Guest!
Keyword Parameters:
Arguments are passed using parameter names, allowing them to be out of order.
Example:
def add(a, b):
return a + b
result = add(b=3, a=5) # Still valid
Passing Parameters: By Value vs. By Reference
Passing by Value
- Definition: A copy of the argument's value is passed to the function or procedure.
- Effect: Changes made to the parameter inside the function do not affect the original argument.
- Languages: Common in languages like C (for primitive types) and Python (for immutable data types like integers and strings).
Example in Python (immutable data type):
def increment(x):
x = x + 1
print(f"Inside function: {x}")
num = 5
increment(num)
print(f"Outside function: {num}")
# Output:
# Inside function: 6
# Outside function: 5
Passing by Reference
- Definition: A reference to the original argument is passed to the function or procedure.
- Effect: Changes made to the parameter inside the function affect the original argument.
- Languages: Common in C++ and Python (for mutable data types like lists and dictionaries).
Example in Python (mutable data type):
def add_item(my_list):
my_list.append("New Item")
print(f"Inside function: {my_list}")
items = ["Item1", "Item2"]
add_item(items)
print(f"Outside function: {items}")
# Output:
# Inside function: ['Item1', 'Item2', 'New Item']
# Outside function: ['Item1', 'Item2', 'New Item']
Benefits and Drawbacks of Passing Methods
| Passing Method | Benefits | Drawbacks |
|---|---|---|
| By Value | • Safer as the original data is not modified. | • May require more memory for large data. |
| • Avoids unintended side effects. | • Changes made inside the function are not retained. | |
| By Reference | • Efficient as no additional memory is required for copies. | • Original data can be accidentally modified. |
| • Useful for working with large data structures. | • Harder to debug due to potential side effects. |
Choosing Between By Value and By Reference
- Use By Value When:
- You want to ensure the original data remains unchanged.
- The function only needs to work with a copy of the data.
- Use By Reference When:
- You want the function to modify the original data.
- Passing large data structures to avoid memory overhead.
Tracing Code with Parameters
Example: Tracing Passing by Value
FUNCTION squareValue(x)
x = x * x
RETURN x
END FUNCTION
MAIN
num = 5
result = squareValue(num)
PRINT num
PRINT result
END MAIN
Trace Table:
| Step | Variable | Value |
|---|---|---|
| 1 | num | 5 |
| 2 | x (in function) | 25 |
| 3 | result | 25 |
| 4 | num | 5 (unchanged) |
Example: Tracing Passing by Reference
FUNCTION addItem(list)
APPEND "New Item" TO list
END FUNCTION
MAIN
myList = ["A", "B"]
addItem(myList)
PRINT myList
END MAIN
Trace Table:
| Step | Variable | Value |
|---|---|---|
| 1 | myList | ["A", "B"] |
| 2 | list (in function) | ["A", "B", "New Item"] |
| 3 | myList | ["A", "B", "New Item"] |
Writing Functions and Procedures with Parameters
Function with Parameters:
def multiply(a, b):
return a * b
result = multiply(3, 4) # Passes values as arguments
print(result) # Output: 12
Procedure with Parameters:
def greet(name):
print(f"Hello, {name}!")
greet("Alice") # Output: Hello, Alice!
Note Summary
Common Mistakes
- Not Using Parameters Correctly: Failing to pass required arguments when calling functions.
- Confusing By Value and By Reference: Assuming changes inside the function will always reflect outside (or vice versa).
- Mutating Immutable Types: Attempting to modify immutable types like strings or tuples directly.
Key Takeaways
- Parameters allow functions and procedures to accept inputs, enhancing modularity and flexibility.
- Passing by Value creates a copy of the data, ensuring the original remains unchanged.
- Passing by Reference passes the actual data, allowing the function to modify it directly.
- Choosing the appropriate passing method depends on the problem's requirements and the need for data modification.