Programming concepts (3)
Resources |
Revision Questions |
Computer Science
Login to see all questions
Click on a question to view the answer
1.
Explain why using comments and consistent naming conventions is important when writing programs. Consider the benefits for yourself and for other programmers who may need to understand or modify the code later.
Using comments and consistent naming conventions is crucial for creating maintainable programs. Comments provide explanations of the code's purpose, logic, and assumptions. This helps both the original programmer and others understand the code's intent, especially when it's complex or not immediately obvious. Without comments, code can become difficult to decipher, leading to errors during modification.
Consistent naming conventions (e.g., using descriptive variable names, following a specific case style like snakecase or CamelCase) significantly improve readability. Well-chosen names make it easier to understand what a variable or function does without having to analyze the entire code. This reduces cognitive load and makes debugging and maintenance much faster. For example, using 'numstudents' instead of 'n' makes the code self-documenting.
For other programmers, clear comments and consistent naming are essential for collaboration. They allow others to quickly grasp the code's functionality and make changes with confidence, reducing the risk of introducing errors. It also makes it easier to onboard new team members.
2.
Consider the following Python code:
x = 10 # Global variable
def my_function():
x = 5 # Local variable
print("Inside function: x =", x)
my_function()
print("Outside function: x =", x)
What output will this code produce? Explain why.
The output of this code will be:
Inside function: x = 5
Outside function: x = 10
Explanation:
The variable x is declared globally. Inside the my_function function, a new variable also named x is declared. This is a local variable. When print("Inside function: x =", x) is executed, it refers to the local variable x within the function, which has the value 5. However, the global variable x retains its original value of 10. The global variable is not modified by the local variable with the same name. Therefore, when print("Outside function: x =", x) is executed, it refers to the global variable x, which still has the value 10.
3.
Define the terms 'procedure' and 'function' in the context of computer programming. Explain the key difference between them.
Procedure: A procedure is a sequence of instructions designed to perform a specific task. It's a block of code that can be called or executed directly. Procedures do not necessarily return a value. They simply carry out the actions within their code block.
Function: A function is a specialized type of procedure that does return a value. It takes input (parameters), performs a calculation or operation on that input, and then returns the result. Functions are designed to encapsulate a specific calculation or task and provide a reusable result.
Key Difference: The crucial difference is the return value. Procedures do not return a value, while functions always return a value. Functions are often used to simplify code and make it more modular by encapsulating reusable calculations.