Appearance
3.12 Calling Procedures 调用过程
Programmers break down problems into smaller and more manageable pieces. By creating procedures and leveraging parameters, programmers generalize processes that can be reused. Procedures allow programmers to draw upon existing code that has already been tested, allowing them to write programs more quickly and with more confidence.
- 程序员将问题分解为更小和更易管理的部分。通过创建过程和利用参数,程序员泛化可以重用的过程。过程允许程序员利用已经测试过的现有代码,使他们能够更快、更有信心地编写程序。
核心要点 Core Points
A procedure is a named group of programming instructions that may have parameters and return values.
- 过程是具有参数和返回值的命名编程指令组。
Procedures are referred to by different names, such as method or function, depending on the programming language.
- 过程根据编程语言的不同而有不同的名称,如方法或函数。
Parameters are input variables of a procedure. Arguments specify the values of the parameters when a procedure is called.
- 参数是过程的输入变量。参数在调用过程时指定参数的值。
A procedure call interrupts the sequential execution of statements, causing the program to execute the statements within the procedure before continuing. Once the last statement in the procedure (or a return statement) has executed, flow of control is returned to the point immediately following where the procedure was called.
- 过程调用中断语句的顺序执行,导致程序在继续之前执行过程中的语句。一旦过程中的最后一条语句(或返回语句)执行完毕,控制流返回到调用过程后立即跟随的点。
The exam reference sheet provides
procName (arg1, arg2, ...)as a way to call a procedure.- 考试参考表提供
procName (arg1, arg2, ...)作为调用过程的方式。
- 考试参考表提供
The exam reference sheet provides the procedure
DISPLAY (expression)to display the value of expression, followed by a space.- 考试参考表提供过程
DISPLAY (expression)来显示表达式的值,后跟一个空格。
- 考试参考表提供过程
The exam reference sheet provides the
RETURN (expression)statement, which is used to return the flow of control to the point where the procedure was called and to return the value of expression.- 考试参考表提供
RETURN (expression)语句,用于将控制流返回到调用过程的点并返回表达式的值。
- 考试参考表提供
The exam reference sheet provides
result ← procName(arg1, arg2, ...)to assign to result the "value of the procedure" being returned by calling the procedure.- 考试参考表提供
result ← procName(arg1, arg2, ...)来将调用过程返回的"过程值"赋给result。
- 考试参考表提供
The exam reference sheet provides
INPUT()which accepts a value from the user and returns the input value.- 考试参考表提供
INPUT(),它接受来自用户的值并返回输入值。
- 考试参考表提供
The exam reference sheet provides the following syntax for defining procedures:
- 考试参考表提供以下定义过程的语法:
- Text:
- 文本:
PROCEDURE procName (parameter1, parameter2, ...) { <block of statements> RETURN (expression) } - Block: A visual representation of the procedure structure
- 块: 过程结构的视觉表示
- Which is used to define a procedure that takes zero or more arguments. The procedure contains block of statements and returns the value of expression. The RETURN statement may appear at any point inside the procedure and causes an immediate return from the procedure back to the calling statement.
- 用于定义接受零个或多个参数的过程。过程包含语句块并返回表达式的值。RETURN语句可能出现在过程内部的任何点,并导致从过程立即返回到调用语句。
学生活动 Student Activities
- For procedure calls:
- 对于过程调用:
- Write statements to call procedures.
- 编写调用过程的语句。
- Determine the result or effect of a procedure call.
- 确定过程调用的结果或效果。
