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. 如果想用此方法来交换两个数的值可能达不到目的