📚 Organised Programs and Subprograms | 结构化程序与子程序
In A-Level Computer Science, linking small blocks of code to create well-structured programs is a fundamental skill. Subprograms – also known as subroutines, procedures, or functions – allow programmers to break down complex tasks into manageable, reusable sections. Edexcel’s specification places a strong emphasis on the design, implementation, and evaluation of organised programs, ensuring students can write efficient, maintainable, and logically sound code.
在A-Level计算机科学中,将小的代码块组合成结构良好的程序是一项基本技能。子程序(也称为子例程、过程或函数)使程序员能够将复杂的任务分解为可管理、可复用的部分。Edexcel的教学大纲特别强调结构化程序的设计、实现和评估,确保学生能够编写高效、可维护且逻辑严谨的代码。
1. Program Structure | 程序结构
An organised program consists of a main body that coordinates calls to a collection of subprograms rather than a single monolithic block of code. This top-down design improves readability and reduces redundancy.
一个结构化的程序由一个主体构成,该主体协调对一系列子程序的调用,而不是单一的庞大代码块。这种自顶向下的设计提高了可读性并减少了冗余。
Edexcel expects you to recognise that the main program should be as concise as possible, delegating specific tasks to named subprograms. A typical layout includes global variable declarations, then subprogram definitions, followed by the executable main section.
Edexcel希望你能认识到主程序应尽可能简洁,将特定任务委托给命名的子程序。典型的布局包括全局变量声明、子程序定义,然后是执行的主体部分。
Using a clear structure also simplifies collaborative development, as different programmers can work on separate subprograms without interfering with the main logic.
使用清晰的结构还能简化协作开发,因为不同的程序员可以分别处理独立的子程序,而不会干扰主逻辑。
2. Subroutines, Procedures, and Functions | 子例程、过程与函数
A subroutine is a named block of code that performs a specific task. In pseudocode and Edexcel’s exam questions, you will encounter both procedures and functions. A procedure carries out an action but does not return a value, whereas a function returns a single computed result to the calling point.
子例程是一个命名的代码块,用于执行特定任务。在伪代码和Edexcel的考试题目中,你会遇到过程和函数。过程执行一个动作但不返回值,而函数将单个计算结果返回给调用点。
For example, a procedure displayMenu() simply prints options, while a function calculateDiscount(price) returns the discounted amount. The distinction matters when you are tracing code or writing algorithms.
例如,过程displayMenu()只是打印选项,而函数calculateDiscount(price)返回折扣后的金额。在追踪代码或编写算法时,这种区别很重要。
In Edexcel pseudocode, a function is defined with FUNCTION name(parameter) RETURNS datatype and a procedure with PROCEDURE name(parameter). The RETURN statement inside a function immediately exits back to the caller.
在Edexcel的伪代码中,函数使用FUNCTION name(parameter) RETURNS datatype定义,过程则使用PROCEDURE name(parameter)。函数内部的RETURN语句会立即返回到调用者。
3. Parameters and Arguments | 参数与实参
Parameters are the placeholders listed in a subprogram’s definition; arguments are the actual values supplied when the subprogram is called. Edexcel often distinguishes between ‘formal parameters’ and ‘actual parameters’.
参数是子程序定义中列出的占位符;实参是调用子程序时提供的实际值。Edexcel经常区分’形式参数’和’实际参数’。
Parameters can be passed by value or by reference. In by-value passing, a copy of the argument is given to the subprogram, so changes inside do not affect the original variable. By-reference passing uses the memory address, allowing the subprogram to modify the original data.
参数可以按值传递或按引用传递。在按值传递中,将实参的副本提供给子程序,因此内部的更改不会影响原始变量。按引用传递使用内存地址,允许子程序修改原始数据。
In Edexcel pseudocode, by-reference parameters are often indicated with a special symbol such as BYREF or an ampersand. It is crucial to track which parameters are read-only and which can overwrite variables in the calling environment when dry-running code.
在Edexcel的伪代码中,按引用传递的参数通常用特殊符号如BYREF或&符号表示。在手工执行代码时,追踪哪些参数是只读的、哪些可以覆盖调用环境中的变量至关重要。
4. Scope of Variables | 变量作用域
Scope defines where a variable can be accessed within a program. Local variables are declared within a subprogram and exist only during its execution; global variables are declared outside all subprograms and are accessible everywhere.
作用域定义了在程序中可以访问变量的位置。局部变量在子程序内部声明,并且仅在其执行期间存在;全局变量在所有子程序外部声明,并在任何地方都可以访问。
Programs with excessive global variables become harder to debug because any subprogram can alter them unintentionally. Edexcel encourages the use of local variables and parameter passing to reduce side effects and improve modularity.
包含过多全局变量的程序会变得更难调试,因为任何子程序都可能无意中改变它们。Edexcel鼓励使用局部变量和参数传递来减少副作用并提高模块化程度。
In an exam, you may be given a trace table and asked to show the values of local and global variables at different stages. Distinguishing between variables with the same name but different scope is a common trap.
在考试中,可能会给出一个追踪表,要求展示不同阶段局部变量和全局变量的值。区分名称相同但作用域不同的变量是一个常见的陷阱。
5. Local Variable Lifetime and Stack Frames | 局部变量的生命周期与栈帧
A local variable persists only for the duration of a subprogram’s call. When the subprogram is invoked, a stack frame is created to hold its local variables and parameters. Upon return, that frame is destroyed and memory is released.
局部变量仅在子程序调用的持续时间内存在。当子程序被调用时,会创建一个栈帧来保存其局部变量和参数。返回时,该帧被销毁,内存被释放。
Understanding this model helps explain why recursion can use significant memory if too many frames accumulate, and why a local variable cannot retain its value between separate calls unless declared as STATIC in some languages.
理解这个模型有助于解释为什么递归在累积过多帧时可能占用大量内存,以及为什么局部变量无法在两次独立调用之间保留其值,除非在某些语言中声明为STATIC。
Edexcel questions sometimes ask you to predict the output of a recursive function by drawing stack frames – a skill that deepens your comprehension of parameter passing and return addresses.
Edexcel有时会要求通过绘制栈帧来预测递归函数的输出——这个技能可以加深你对参数传递和返回地址的理解。
6. Recursion | 递归
Recursion occurs when a subprogram calls itself to solve a smaller instance of the same problem. Well-designed recursive solutions include a base case that stops the chain of calls and a recursive step that moves toward that base case.
递归发生在子程序调用自身以解决同一问题的较小实例时。设计良好的递归解决方案包括一个停止调用链的基准情形,以及一个使调用趋向该基准情形的递归步骤。
A classic example is factorial computation:
FUNCTION factorial(n: INTEGER) RETURNS INTEGER
IF n = 0 THEN
RETURN 1
ELSE
RETURN n * factorial(n-1)
ENDIF
ENDFUNCTION
一个经典的例子是阶乘计算:
FUNCTION factorial(n: INTEGER) RETURNS INTEGER
IF n = 0 THEN
RETURN 1
ELSE
RETURN n * factorial(n-1)
ENDIF
ENDFUNCTION
Recursion can make code more elegant, but iterative loops are often more memory-efficient. Edexcel values your ability to compare both approaches and convert a recursive algorithm into an iterative equivalent.
递归可以使代码更优雅,但迭代循环通常更节省内存。Edexcel看重你比较两种方法以及将递归算法转换为迭代等价形式的能力。
7. Modular Programming and Decomposition | 模块化编程与分解
Modular programming is the practice of dividing a system into distinct subprograms, each with a single responsibility. This decomposition mirrors top-down design and enables component reuse.
模块化编程是将系统划分为独立的子程序的做法,每个子程序承担单一的职责。这种分解反映了自顶向下的设计,并实现了组件的复用。
Benefits include easier testing, as you can verify each subprogram independently; improved maintainability, because changes in one module have limited impact on others; and better team collaboration, since interfaces between modules can be agreed upon early.
好处包括更容易测试,因为你可以独立验证每个子程序;提高了可维护性,因为某个模块的更改对其他模块的影响有限;以及更好的团队协作,因为模块之间的接口可以提前商定。
Edexcel encourages you to design programs using a structure chart to show the hierarchy of subprograms and the data flow between them. In exams, you may be asked to justify why a modular solution is more effective than a single, unstructured script.
Edexcel鼓励你使用结构图来设计程序,显示子程序的层次结构以及它们之间的数据流。在考试中,你可能会被要求论证模块化解决方案为何比单一、无结构的脚本更有效。
8. Built-in Functions, Library Subprograms, and IDEs | 内置函数、库子程序与集成开发环境
Most high-level languages provide a library of pre-written subprograms. In Edexcel pseudocode, you can assume the existence of common functions such as RANDOM(a,b), LEN(str), and STRING_TO_INT().
大多数高级语言都提供预编写的子程序库。在Edexcel的伪代码中,你可以假定存在常见的函数,如RANDOM(a,b)、LEN(str)和STRING_TO_INT()。
Using these built-in routines saves development time and reduces errors, as they have already been tested. However, you must be precise with syntax and understand the data types they expect and return.
使用这些内置例程可以节省开发时间并减少错误,因为它们已经被测试过。然而,你必须精确掌握语法,并理解它们期望和返回的数据类型。
A modern IDE supports the use of library subprograms through autocompletion and inline documentation. Edexcel’s paper 2 often includes scenarios where selecting the right library function is key to producing a concise solution.
现代IDE通过自动补全和内联文档支持库子程序的使用。Edexcel的试卷2经常包含这样的场景,选择合适的库函数是生成简洁解决方案的关键。
9. Tracing and Dry-Running Subprograms | 子程序的追踪与手工执行
Tracing is a vital skill for A-Level programmers. You create a table with columns for each variable and parameter, then step through the code line by line, recording how values change.
追踪是A-Level程序员的一项关键技能。你需要创建一个表格,为每个变量和参数设置列,然后逐行执行代码,记录数值如何变化。
When a subprogram is called, you must note the current state, move into the subprogram with its parameter mapping, and later return to the correct line. Common pitfalls include forgetting to update local copies and confusing variables that share names across different scopes.
当调用子程序时,必须记录当前状态,带着参数映射进入子程序,之后返回正确的行。常见的陷阱包括忘记更新局部副本,以及混淆在不同作用域中共享名称的变量。
Let’s trace a short example: PROCEDURE swap(BYREF a, BYREF b) . If the main program passes x=5, y=3, after the call swap(x,y) the values are exchanged. The trace table solidifies your understanding of by-reference behaviour.
让我们追踪一个简短的示例:PROCEDURE swap(BYREF a, BYREF b)。如果主程序传入x=5, y=3,在调用swap(x,y)后,值发生了交换。追踪表可以巩固你对按引用行为的理解。
10. Exam Tips for Organised Programs | 考试技巧:结构化程序
In Edexcel exams, questions on subprograms may appear in multiple forms: writing pseudocode, completing trace tables, identifying errors, or evaluating design choices. Always read the specification for the exact pseudocode syntax required.
在Edexcel考试中,关于子程序的题目可能以多种形式出现:编写伪代码、完成追踪表、识别错误或评估设计选择。请务必阅读规范,掌握所需的准确伪代码语法。
When writing a subprogram in an answer, include clear names, explicit parameter declarations, and comments outlining the purpose. Examiners look for well-structured logic, not obscure trickery.
在答案中编写子程序时,应使用清晰的名称、明确的参数声明以及概述目的的注释。考官看重的是结构良好的逻辑,而非晦涩的技巧。
Pay close attention to whether a question demands a procedure or a function. If a value must be sent back to the main program, a function with a return type is mandatory; a procedure will not earn full marks.
要特别注意题目要求的是过程还是函数。如果必须将值发送回主程序,则必须使用带有返回类型的函数;使用过程将无法得到满分。
For by-reference versus by-value scenarios, confirm the intended effect: should the original arguments be modified? A common trick is altering a global variable from inside a procedure, testing your scope and side-effect awareness.
对于按引用传递与按值传递的场景,要确认预期的效果:原始参数是否应被修改?一个常见的技巧是从过程内部改变全局变量,测试你对作用域和副作用的认识。
11. Testing and Corrective Maintenance | 测试与纠错性维护
An organised program is easier to test because you can isolate each subprogram with a driver routine that supplies sample inputs and checks outputs. This is known as unit testing.
结构化程序更容易测试,因为你可以使用驱动程序例程来隔离每个子程序,提供示例输入并检查输出。这称为单元测试。
When an error is found, corrective maintenance is straightforward: you find the faulty subprogram, examine its logic with trace tables, and fix it without rewriting the entire system. Edexcel supports this approach as part of the software development lifecycle.
当发现错误时,纠错性维护很简单:找到有问题的子程序、使用追踪表检查其逻辑,然后修复它,而无需重写整个系统。Edexcel支持将这种方法作为软件开发生命周期的一部分。
You should also be aware of testing strategies: normal data (expected values), boundary data (limiting cases), and erroneous data (out-of-range). Exam questions may ask you to design test plans for a given subprogram.
你还应该了解测试策略:正常数据(预期值)、边界数据(极限情况)和错误数据(超范围)。考试题目可能会要求为给定的子程序设计测试计划。
12. Comparing Recursive and Iterative Solutions | 递归与迭代解决方案的比较
Both recursion and iteration can solve repetitive problems, but they differ in resource usage. Recursion uses call stack memory and can cause stack overflow if base case is missing, while iteration relies on loop constructs with a finite counter.
递归和迭代都可以解决重复的问题,但它们在资源使用上有所不同。递归使用调用栈内存,如果缺少基准情形可能导致栈溢出;而迭代则依赖于带有有限计数器的循环构造。
Converting between the two is an Edexcel skill: a recursive factorial function can be transformed into a WHILE loop that accumulates the product. Similarly, tree traversal algorithms can be written iteratively using an explicit stack.
在两者之间进行转换是Edexcel要求的一项技能:递归阶乘函数可以转换为一个累积乘积的WHILE循环。类似地,树遍历算法可以使用显式栈以迭代方式编写。
Use the following decision heuristic: if the problem naturally breaks into smaller identical subproblems and space is not constrained, recursion is often clearer. When performance is critical and the call overhead is high, iteration is preferred.
使用以下决策启发式方法:如果问题自然地分解为更小的相同子问题且空间不受限制,递归通常更清晰。当性能至关重要且调用开销较大时,迭代更可取。
Published by TutorHao | Programming Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导