Chapter 5 Procedure activations 过程活动要点 参数传递机伟 宏扩展
Chapter 5 Procedure Activations 过程活动要点 ◼参数传递机制 ◼宏扩展
PROCEDURES(过程) When a procedure is called, the body is executed. Each execution of the body is called an activation of the body. Two forms of procedures: functions Functions extend the built-in operators of a language. procedures Procedures extend the built-in actions or statements
PROCEDURES(过程) When a procedure is called, the body is executed. Each execution of the body is called an activation of the body. Two forms of procedures: • functions Functions extend the built-in operators of a language. • procedures Procedures extend the built-in actions or statements
ELEMENTS OF A PROCEDURE 过程的要素 A procedure declaration makes explicit the elements or parts of a procedure: procedure name(过程名), a name for the declared procedure foma/ parameters(形式参数) placeholders for actual parameters result type(返回值类型), which is optional ocedure body(过程体), consisting of local declarations and a statement
ELEMENTS OF A PROCEDURE 过程的要素 A procedure declaration makes explicit the elements or parts of a procedure: • procedure name(过程名), a name for the declared procedure • formal parameters(形式参数), placeholders for actual parameters • result type(返回值类型), which is optional • procedure body(过程体), consisting of local declarations and a statement
HE ELEMENTS OFA FUNCTION PROCEDURE FORMAL RESULT NAME PARAMIETER TYPE function square(x: integer): integer; PROCEDURE egll BODY sguare:II ell
THE ELEMENTS OF A FUNCTION
A COMPLETE PASCAL PROGRAM WITH TWO FUNCTIONS, SQUARE AND AREA program trap(input, output; var result: real function square(x: integer): integer: begin square.dx end function area( a, b: real function f(x: real): real): real; DegIn dre:=(b-a)*(f(a)+f(b)/2 en d begin result: area(2, 5, square) writen 2, 5, result en d 带函数参数
A COMPLETE PASCAL PROGRAM WITH TWO FUNCTIONS, SQUARE AND AREA 带函数参数
BENEFITS OF PROCEDURES The user of a procedure needs to know what a procedure does, not how the procedure works. The benefits of procedures include the following Procedure abstraction(过程抽象) Implementation hiding(实现隐藏) Modular programs(模块化编程) Libraries(库)
BENEFITS OF PROCEDURES The user of a procedure needs to know what a procedure does, not how the procedure works. The benefits of procedures include the following: – Procedure abstraction(过程抽象) – Implementation hiding(实现隐藏) – Modular programs(模块化编程) – Libraries(库)
PARAMETERPASSING METHODS 参数传递机制 Parameter passing refers to the matching of actuals(实际参数) with formals(形式参数) when a procedure call occurs
PARAMETER-PASSING METHODS 参数传递机制 Parameter passing refers to the matching of actuals(实际参数) with formals(形式参数) when a procedure call occurs
Possible interpretations of a procedure call like P(A[i]) include the following: ca∥- by-value(值传递) Pass the value of Ali Ca∥-by- reference(引用传递 Pass the location of A[i Ca∥-by- value- result(值结果传递) actuals are initially copied into the formals and the formals are eventually copied back thethe actuals Ca∥- by-name(名字传递) Pass the text Ali itself, while avoiding name clashes
Possible interpretations of a procedure call like P(A[i]) include the following: – Call-by-value(值传递) Pass the value of A[i]. – Call-by-reference(引用传递) Pass the location of A[i]. – Call-by-value-result(值结果传递) actuals are initially copied into the formals and the formals are eventually copied back the the actuals. – Call-by-name(名字传递) Pass the text A[i] itself, while avoiding ``name clashes
CALL-BY-VALUE(值传递) Under call-by-value, a formal parameter corresponds to the value of an actual parameter. call by-value is the primary parameter passing method in C and Pascal. 传递了实际参数的一个副本给形参
CALL-BY-VALUE(值传递) Under call-by-value, a formal parameter corresponds to the value of an actual parameter. Callby-value is the primary parameterpassing method in C and Pascal. 传递了实际参数的一个副本给形参
Example: procedure muchAddx, y: 7: varz:万 egin 2:XX:yy:=2 end A call muchAdo (a, b) has the following effect X: =a,i pass the value of a to Xy y:=b,i pass the value of b to y) Z:=XX: =Ny: =Z a and b are unchanged y The program segment does not change a or b, though the values of x and y are indeed exchanged 如果想用此方法来交换两个数的值可能达不到目的
Example: procedure muchAdo(x, y : T); var z : T; begin z := x; x := y; y := z; end A call muchAdo(a,b) has the following effect: x := a; { pass the value of a to x } y := b; { pass the value of b to y } z := x; x := y; y := z; { a and b are unchanged } The program segment does not change a or b, though the values of x and y are indeed exchanged. 如果想用此方法来交换两个数的值可能达不到目的